日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

各种清浮动方法总结

發布時間:2025/4/9 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 各种清浮动方法总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一:(1)clear清浮動

   ??

<!doctype html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{float: left;width:200px;height: 200px;background-color: red;}.box2{float:left;clear:left;width:300px;height:300px;background-color: green;}</style> </head> <body><div class="box1"></div><div class="box2"></div> </body> </html>

? ? (2)在浮動元素后添加空標簽,設置clear:both

<!doctype html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{float: left;width:200px;height: 200px;background-color: red;}.box2{float:left;width:300px;height:300px;background-color: green;}.clear{clear: both;display: block;float: none;font-size:0;margin:0;padding:0;overflow: hidden;visibility:hidden;height:0;width:0;background: none;}</style> </head> <body><div class="box1"></div><div class="clear"></div><div class="box2"></div> </body> </html>

二:overflow清浮動

? ??

<!doctype html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{float: left;width:200px;height:200px;background: green;}.box2{float:left;width:200px;height:200px;background: navy;}.box3{height: 200px;width:400px;background: olive;}.main{overflow: hidden;}</style> </head> <body><div class="main"><div class="box1"></div><div class="box2"></div></div><div class="box3"></div> </body> </html>

這里我們為box1,box2的父元素設置overflow:hidden,同樣可以達到清浮動的效果。

三:為父級元素定義高度

<!doctype html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{float: left;width:200px;height:200px;background: green;}.box2{float:left;width:200px;height:200px;background: navy;}.box3{height: 200px;width:400px;background: olive;}.main{height:200px;}</style> </head> <body><div class="main"><div class="box1"></div><div class="box2"></div></div><div class="box3"></div> </body> </html>

當我們使用浮動后,浮動元素的父級元素無法自動獲取高度,這時手動為其設置高度后可解決問題。

四:after偽類清浮動

<!doctype html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style type="text/css">.box1{float: left;width:200px;height:200px;background: green;}.box2{float:left;width:200px;height:200px;background: navy;}.box3{height: 200px;width:400px;background: olive;}.clear:after{visibility:hidden;display:block;font-size:0;content:".";clear:both;height:0;}.clear{zoom:1;}</style> </head> <body><div class="main clear"><div class="box1"></div><div class="box2"></div></div><div class="box3"></div> </body> </html>

說明:給容器添加clear類即可達到清除浮動的目的,因為清浮動需要在浮動元素后面,所以不用:before。首先用content屬性插入了一個句點,默認情況下content屬性插入的內容是行內元素,需要將其display:block,才能使clear:both生效,為了不讓插入的內容影響原來的布局,需要height:0將其高度設為0。

優化后的清除浮動的樣式:

.clear:after{content:".";display: block;height:0;clear:both;}.clear{zoom:1;}

先總結到這。。。

轉載于:https://www.cnblogs.com/yingu/p/5322789.html

總結

以上是生活随笔為你收集整理的各种清浮动方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。