## 清除浮动 ``` <div> <div class="one"> <div class="two"></div> </div> <div class="three"> <div class="four"></div> </div> <div class="five"> <div class="six"></div> </div> <div class="seven"> <div class="eight"></div> <div></div> </div> </div> /* 清除浮动 */ /* 1. 父元素float,但是不可取,因为会影响到父元素这一层,不稳定 */ .one{ float: left; background: red; padding: 10px; } .two{ float: left; width: 100px; height: 20px; background-color: gainsboro; } /* 2.父元素设置overflow:hidden */ .three{ overflow: hidden; padding: 10px; background-color: black; } .four{ background-color: green; width: 100px; height: 20px; float: left; } /* 3. 设置父元素伪元素after清除浮动 clear:both */ .five{ background-color: blue; margin-top: 10px; padding: 10px; } .six{ width: 100px; height: 20px; background-color: antiquewhite; float: left; } .five::after{ clear: both; display: block; content: ''; } /* 4.给子元素后面加一个兄弟元素,该兄弟元素设置clear:both */ .seven{ padding: 10px; background-color: gray; width: 300px; margin-top: 10px; } .eight{ float: left; width: 100px; height: 20px; background-color: yellow; } .eight +div{ clear: both; } ``` * `注意,如果是让浮动元素的父元素的兄弟元素不被浮动影响,那么就给兄弟元素添加clear:both;`
hidden | 内容会被修剪,并且其余内容是不可见的。 |
scroll | 内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。 |