CSS
中float
属性会使元素浮动,使元素向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
float
意味着使用块布局,其会在display
非块级元素情况下修改display
值的计算值。<!DOCTYPE html> <html> <head> <title>Float</title> <style type="text/css"> body > div{ border: 1px solid #eee; padding: 5px 0; margin: 5px 0; } .t1{ margin: 0 5px; float: left; background-color: #EEE; } </style> </head> <body> <div> <div class="t1">Float</div> <div class="t1">Float</div> <div class="t1">Float</div> <div style="height: 10px;background-color: blue;"></div> </div> </body> </html>
可以认为文档流与文字流是分层结构而浮动元素盒子与文字元素处于同一层,当元素浮动时,其脱离文档流不脱离文本流,所以浮动元素的渲染与文字的渲染是一并渲染的,浮动元素会将文字元素挤开,呈现文字环绕浮动元素的效果。
<!DOCTYPE html> <html> <head> <title>Float</title> <style type="text/css"> body > div{ border: 1px solid #eee; padding: 5px 0; margin: 5px 0; } .t1{ margin: 0 5px; float: left; background-color: #EEE; height: 100px; width: 100px; } </style> </head> <body> <div> <div class="t1">Float</div> <div>123</div> <div>123</div> <div>123</div> <div>123</div> <div>123</div> <div>123</div> <div>123</div> </div> </body> </html>
虽然float
最初的设计就是用来实现文字环绕效果的,在某些使用float
的布局下若是不想触发文字环绕效果,可以通过触发BFC
来避免文字环绕。
<!DOCTYPE html> <html> <head> <title>Float</title> <style type="text/css"> body > div{ border: 1px solid #eee; padding: 5px 0; margin: 5px 0; } .t1{ margin: 0 5px; float: left; background-color: #EEE; height: 100px; width: 100px; } </style> </head> <body> <div> <div class="t1">Float</div> <div style="overflow: auto;"> <div>123</div> <div>123</div> <div>123</div> <div>123</div> <div>123</div> <div>123</div> <div>123</div> </div> </div> </body> </html>
使用浮动可能会导致一些负面影响,由于脱离文档流,无法撑起父元素导致背景以及边框无法正常显示、与其他元素重叠、下层浮动依旧会在上层浮动元素的基础上进行浮动等问题,此时就需要清除浮动。
通过CSS
的clear: both;
清除浮动。
<!DOCTYPE html> <html> <head> <title>Float</title> <style type="text/css"> .container{ border: 1px solid #eee; padding: 5px 0; margin: 5px 0; } .t1{ margin: 0 5px; float: left; background-color: #EEE; height: 100px; width: 100px; } .clear{ clear: both; } </style> </head> <body> <div class="container"> <div class="t1">Float</div> <div class="t1">Float</div> <div class="t1">Float</div> <div style="height: 10px;background-color: blue;"></div> <!-- 此处不清除浮动会产生负面效果 --> </div> <div class="container"> <div class="t1">Float</div> <div class="t1">Float</div> <div class="t1">Float</div> <div style="height: 10px;background-color: blue;"></div> <!-- 清除浮动 --> <div class="clear"></div> </div> <!-- 若是在此处清除浮动也是可以的 但是会无法撑起容器高度 --> <div class="container"> <div class="t1">Float</div> <div class="t1">Float</div> <div class="t1">Float</div> <div style="height: 10px;background-color: blue;"></div> <!-- 清除浮动 --> <div class="clear"></div> </div> </body> </html>
通过使用伪元素::after
配合clear
属性进行浮动清除。
<!DOCTYPE html> <html> <head> <title>Float</title> <style type="text/css"> .container{ border: 1px solid #eee; padding: 5px 0; margin: 5px 0; } .t1{ margin: 0 5px; float: left; background-color: #EEE; height: 100px; width: 100px; } .container::after{ clear: both; content:""; display: block; } </style> </head> <body> <div class="container"> <div class="t1">Float</div> <div class="t1">Float</div> <div class="t1">Float</div> <div style="height: 10px;background-color: blue;"></div> </div> <div class="container"> <div class="t1">Float</div> <div class="t1">Float</div> <div class="t1">Float</div> <div style="height: 10px;background-color: blue;"></div> </div> </body> </html>
触发浮动容器的BFC
来清除浮动。
<!DOCTYPE html> <html> <head> <title>Float</title> <style type="text/css"> .container{ border: 1px solid #eee; padding: 5px 0; margin: 5px 0; overflow: auto; /* 触发BFC */ } /* BFC 块级格式化上下文 https://github.com/WindrunnerMax/EveryDay/blob/master/CSS/%E5%9D%97%E7%BA%A7%E6%A0%BC%E5%BC%8F%E5%8C%96%E4%B8%8A%E4%B8%8B%E6%96%87.md */ .t1{ margin: 0 5px; float: left; background-color: #EEE; height: 100px; width: 100px; } </style> </head> <body> <div class="container"> <div class="t1">Float</div> <div class="t1">Float</div> <div class="t1">Float</div> <div style="height: 10px;background-color: blue;"></div> </div> <div class="container"> <div class="t1">Float</div> <div class="t1">Float</div> <div class="t1">Float</div> <div style="height: 10px;background-color: blue;"></div> </div> </body> </html>
https://github.com/WindrunnerMax/EveryDay
https://www.cnblogs.com/lingdu87/p/7770752.html https://developer.mozilla.org/zh-CN/docs/CSS/float https://www.w3school.com.cn/css/css_positioning_floating.asp
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句