关于css清除浮动的几种方法

    技术2022-07-10  150

    清除浮动的方式:

    给前面一个父元素设置高度给后面的盒子添加clear属性隔墙法:其又分为外墙法和内墙法利用伪元素选择器清除浮动利用overflow: hidden清除浮动

    下面一一介绍: 

    1、给前面一个父元素设置高度

    注意点:在企业开发中, 我们能不写高度就不写高度, 所以这种方式用得很少

    代码如下:

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>65-清除浮动方式一</title> <style> *{ margin: 0; padding: 0; } .box1{ height: 20px; background-color: red; } .box2{ background-color: green; } .box1 p{ width: 100px; background-color: blue; } .box2 p{ width: 100px; background-color: yellow; } p{ float: left; } </style> </head> <body> <div class="box1"> <p>我是文字1</p> <p>我是文字1</p> <p>我是文字1</p> </div> <div class="box2"> <p>我是文字2</p> <p>我是文字2</p> <p>我是文字2</p> </div> </body> </html>

     2、给后面的盒子添加clear属性

    clear属性取值: none: 默认取值, 按照浮动元素的排序规则来排序(左浮动找左浮动, 右浮动找右浮动) left: 不要找前面的左浮动元素 right: 不要找前面的右浮动元素 both: 不要找前面的左浮动元素和右浮动元素

    注意点:当我们给某个元素添加clear属性之后, 那么这个属性的margin属性就会失效

     代码如下:

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>66-清除浮动方式二</title> <style> *{ margin: 0; padding: 0; } body{ border: 1px solid #000; } .box1{ background-color: red; } .box2{ background-color: green; clear: both; margin-top: 28px; } .box1 p{ width: 100px; background-color: blue; } .box2 p{ width: 100px; background-color: yellow; } p{ float: left; } </style> </head> <body> <div class="box1"> <p>我是文字1</p> <p>我是文字1</p> <p>我是文字1</p> </div> <div class="box2"> <p>我是文字2</p> <p>我是文字2</p> <p>我是文字2</p> </div> </body> </html>

    3、 隔墙法:其又分为外墙法和内墙法

    外墙法   在两个盒子中间添加一个额外的块级元素   给这个额外添加的块级元素设置clear: both;属性

    注意点:  外墙法它可以让第二个盒子使用margin-top属性   外墙法不可以让第一个盒子使用margin-bottom属性


     

    内墙法   在第一个盒子中所有子元素最后添加一个额外的块级元素   给这个额外添加的块级元素设置clear: both;属性

    注意点:  内墙法它可以让第二个盒子使用margin-top属性   内墙法它可以让第一个盒子使用margin-bottom属性


     

    外墙法和内墙法区别?   外墙法不能撑起第一个盒子的高度, 而内墙法可以撑起第一个盒子的高度

    注意点: 

      在企业开发中不常用隔墙法来清除浮动

    代码如下:

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>67-清除浮动方式三</title> <style> *{ margin: 0; padding: 0; } .box1{ background-color: red; /*margin-bottom: 10px;*/ } .box2{ background-color: green; /*margin-top: 10px;*/ } .box1 p{ width: 100px; background-color: blue; } .box2 p{ width: 100px; background-color: yellow; } p{ float: left; } .wall{ clear: both; } .h20{ height: 20px; background-color: skyblue; } </style> </head> <body> <div class="box1"> <p>我是文字1</p> <p>我是文字1</p> <p>我是文字1</p> <div class="wall h20"></div> </div> <!--<div class="wall h20"></div>--> <div class="box2"> <p>我是文字2</p> <p>我是文字2</p> <p>我是文字2</p> </div> </body> </html>

    4、利用伪元素选择器清除浮动

    其中关于伪类选择器的解释:

    什么是伪元素选择器?    伪元素选择器作用就是给指定标签的内容前面添加一个子元素或者给指定标签的内容后面添加一个子元素

    利用伪元素选择器清除浮动 本质上就是内墙法, 只不过是直接通过CSS代码添加了内墙, 其它特性和内墙法都一样

    注意点: IE6中不支持这种方式, 为了兼容IE6必须给前面的盒子添加*zoom:1;属性

    代码如下:

     

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>69-清除浮动方式四</title> <style> *{ margin: 0; padding: 0; } .box1{ background-color: red; /*margin-bottom: 10px;*/ } .box2{ background-color: green; /*margin-top: 10px;*/ } .box1 p{ width: 100px; background-color: blue; } .box2 p{ width: 100px; background-color: yellow; } p{ float: left; } .box1::after{ /*设置添加的子元素的内容为空*/ content: ""; /*设置添加的子元素为块级元素*/ display: block; /*设置添加的子元素的高度为0*/ height: 0; /*设置添加的子元素看不见*/ visibility: hidden; /*给添加的子元素设置clear: both;*/ clear: both; } .box1{ /*兼容IE6*/ *zoom:1; } </style> </head> <body> <div class="box1"> <p>我是文字1</p> <p>我是文字1</p> <p>我是文字1</p> </div> <div class="box2"> <p>我是文字2</p> <p>我是文字2</p> <p>我是文字2</p> </div> </body> </html>

    5、 利用overflow: hidden清除浮动

    其中关于overflow: hidden的作用介绍:

      1.1 可以将超出标签范围的内容裁剪掉   1.2 清除浮动   1.3 可以通过overflow: hidden;让里面的盒子设置margin-top之后, 外面的盒子不被顶下来

    代码如下:

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>70-清除浮动方式五</title> <style> *{ margin: 0; padding: 0; } /* div{ width: 100px; height: 100px; background-color: red; overflow: hidden; } */ /* .box1{ background-color: red; overflow: hidden; *zoom:1; } .box2{ background-color: green; } .box1 p{ width: 100px; background-color: blue; } .box2 p{ width: 100px; background-color: yellow; } p{ float: left; } */ .box1{ width: 200px; height: 200px; background-color: red; /*border: 1px solid #000;*/ overflow: hidden; } .box2{ width: 100px; height: 100px; background-color: blue; margin-top: 20px; } </style> </head> <body> <!-- <div class="box1"> <p>我是文字1</p> <p>我是文字1</p> <p>我是文字1</p> </div> <div class="box2"> <p>我是文字2</p> <p>我是文字2</p> <p>我是文字2</p> </div> --> <div class="box1"> <div class="box2"></div> </div> </body> </html>

     

    Processed: 0.010, SQL: 9