Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
動的目的:把多個盒子放在一行上
清除浮動的目的:解決父盒子高度為0的問題
清除浮動,也稱閉合浮動
注:本文不兼容IE6
未清除浮動實現情況:
圖1
清除后:
圖2
原代碼:
復制代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>清除浮動</title>
<style type="text/css">
.content{
width:960px;
margin:100px auto;
border: 1px solid #ccc;
}
.left{
width:400px;
height: 200px;
background-color: green;
float: left;
}
.right{
width: 400px;
height: 200px;
background-color: red;
float: right;
}
</style>
</head>
<body>
<div class="content">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
復制代碼
具體方法:
1.額外標簽法
在含浮動標簽后加兄弟盒子清除浮動
例:
復制代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>清除浮動</title>
<style type="text/css">
.content{
width:960px;
margin:100px auto;
border: 1px solid #ccc;
}
.left{
width:400px;
height: 200px;
background-color: green;
float: left;
}
.right{
width: 400px;
height: 200px;
background-color: red;
float: right;
}
.clearbox{
clear:both;
}
</style>
</head>
<body>
<div class="content">
<div class="left"></div>
<div class="right"></div>
<div class="clearbox"></div>
</div>
</body>
</html>
復制代碼
缺點:添加了許多空div
2.給父盒子overflow:hidden
觸發bfc模式(該名詞不懂請谷歌/百度,初學者可暫時略過),直接清除浮動
復制代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>清除浮動</title>
<style type="text/css">
.content{
width:960px;
margin:100px auto;
border: 1px solid #ccc;
overflow: hidden;
}
.left{
width:400px;
height: 200px;
background-color: green;
float: left;
}
.right{
width: 400px;
height: 200px;
background-color: red;
float: right;
}
.clearbox{
clear:both;
}
</style>
</head>
<body>
<div class="content">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
復制代碼
缺點:不可與position屬性配合使用
3.偽元素法
給父元素定義偽類:after(此處使用的是公共類clearfix)
復制代碼
.clearfix:after{
content:"";/*內容為空*/
visibility:hidden;/*將元素隱藏,但是在網頁中該占的位置還是占著*/
display:block;/*變成塊級元素*/
height:0;
clear:both;/*清除浮動*/
}
復制代碼
具體代碼:
復制代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>清除浮動</title>
<style type="text/css">
.clearfix:after{
content:"";/*內容為空*/
visibility:hidden;/*將元素隱藏,但是在網頁中該占的位置還是占著*/
display:block;/*變成塊級元素*/
height:0;
clear:both;/*清除浮動*/
}
.content{
width:960px;
margin:100px auto;
border: 1px solid #ccc;
}
.left{
width:400px;
height: 200px;
background-color: green;
float: left;
}
.right{
width: 400px;
height: 200px;
background-color: red;
float: right;
}
.clearbox{
clear:both;
}
</style>
</head>
<body>
<div class="content clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
復制代碼
缺點:IE8以上和非IE瀏覽器才支持
4.雙偽元素法
給父類加上偽類:before和:after
復制代碼
.clearfix:before,.clearfix:after{
content:"";
display:table;
}
.clearfix:after{
clear:both;
}
復制代碼
具體代碼:
復制代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>清除浮動</title>
<style type="text/css">
.clearfix:before,.clearfix:after{
content:"";
display:table;
}
.clearfix:after{
clear:both;
}
.content{
width:960px;
margin:100px auto;
border: 1px solid #ccc;
}
.left{
width:400px;
height: 200px;
background-color: green;
float: left;
}
.right{
width: 400px;
height: 200px;
background-color: red;
float: right;
}
.clearbox{
clear:both;
}
</style>
</head>
<body>
<div class="content clearfix">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
復制代碼
附:
對于上述4種方法,優先推薦方法3和4,當然1和2也可,可根據具體情況使用。
還有幾種亂七八糟的清除浮動方法,但是缺點多,故不提起.
最后你覺得我們的文章對你有幫助,歡迎關注我,可以私信我:久伴,領取學習資料,在評論下方可以關注我的學習群,你可以隨時在上面向我們提問,把你在學習前端過程中所遇到的問題發給我們。我們每天都會按時回復大家的每一個問題,希望久伴可以伴隨你從入門到專家。
浮動是為了元素標簽的并排顯示問題。
我們在瀏覽網頁的時候,經常會看到 幾個 div 塊是可以并排顯示的, 浮動就是解決這樣問題的方法之一。
float屬性有以下的值
浮動的特點
一個浮動的例子
<!--一個浮動的例子-->
<style>
.box1 {
width: 600px;
height: 200px;
border: 1px solid #000;
}
.box1 .con1 {
width: 200px;
height: 200px;
background-color: orange;
float: left;
}
.box1 .con2 {
width: 200px;
height: 200px;
background-color: blue;
float: left;
}
.box1 .con3 {
width: 201px;
height: 200px;
background-color: yellowgreen;
float: left;
}
</style>
<div class="box1">
<div class="con1"></div>
<div class="con2"></div>
<div class="con3"></div>
</div>
一個順序貼靠的例子
<!-- 一個順序貼靠的例 子-->
<!-- 以下代碼中 兄弟元素 con1, con2, con3 之間會進行順序貼靠-->
<!-- con3 在貼靠 con2 的時候,發現父容器的寬度只能是400px, 分別被con1 和 con2 占據, 所以con3 會找 cont1 貼靠,最終con3 位于 con1右邊, con2下邊 -->
<style>
.box1 {
width: 400px;
height: 200px;
border: 1px solid #000;
}
.box1 .con1 {
width: 200px;
height: 200px;
background-color: orange;
float: left;
}
.box1 .con2 {
width: 200px;
height: 100px;
background-color: blue;
float: left;
}
.box1 .con3 {
width: 200px;
height: 100px;
background-color: yellowgreen;
float: left;
}
</style>
<div class="box1">
<div class="con1"></div>
<div class="con2"></div>
<div class="con3"></div>
</div>
清除浮動是為了 不影響設置浮動標簽的后續的標簽的布局展示。
因為,在一個父容器中,子元素浮動了,脫離了標準文檔流,不在占用之前的位置,會導致 無法撐開沒有設置高度的父元素;從而導致后續的結構擠壓,造成整個頁面布局的混亂。
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 100px;
}
p {
float: left;
width: 100px;
height: 100px;
margin-right: 20px;
background-color: red;
}
</style>
<body>
<div>
<p></p>
<p></p>
</div>
<div>
<p></p>
<p></p>
<p></p>
</div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
.box2 {
margin-top: 20px;
clear: both;
}
p {
float: left;
width: 100px;
height: 100px;
margin-right: 20px;
background-color: red;
}
</style>
<body>
<div>
<p></p>
<p></p>
</div>
<div class="box2">
<p></p>
<p></p>
<p></p>
</div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
.cleafix::after {
content: '';
clear: both;
display: block
}
p {
float: left;
width: 100px;
height: 100px;
margin-right: 20px;
background-color: red;
}
</style>
<body>
<div class="cleafix">
<p></p>
<p></p>
</div>
<div class="cleafix">
<p></p>
<p></p>
<p></p>
</div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
.clearboth {
clear: both;
}
p {
float: left;
width: 100px;
height: 100px;
margin-right: 20px;
background-color: red;
}
</style>
<body>
<div>
<p></p>
<p></p>
</div>
<div class="clearboth"></div>
<div>
<p></p>
<p></p>
<p></p>
</div>
</body>
上期跟大家分享了浮動,這些咱們講清除浮動
為什么要清除浮動呢,
因為浮動導致父元素的塌陷,所以要清除浮動
清除浮動 CSS代碼 clear:left right both none;
子級辦法
子級最后添加空標簽
父級辦法
加高問題:擴展性不好
inline-block 清浮動方法問題:margin:auto;失效
overflow:hidden 清浮動方法;問題:要配合寬度
after偽元素內部末尾添加內容 時下主流
.clearfix{zoom:1;}.
clearfix:after{content:'';display:block;clear:both;}
min-width 設置元素的最小寬度
max-width 設置元素的最大寬度
獲取詳細的講解可以關注私信我,我免費分享一套網站前端的視頻教程給大家。
最后歡迎大神在留言區吐槽
*請認真填寫需求信息,我們會在24小時內與您取得聯系。