美文网首页
清除浮动

清除浮动

作者: Jackson_yee_ | 来源:发表于2017-06-14 21:10 被阅读0次

    清除浮动

    • 盒子高度问题

    • 在标准流中内容的高度可以撑起盒子的高度

    <style>
            div{
                background-color: red;
            }
            p{
                width: 200px;
                height: 100px;
                background-color: blue;
            }
    </style>
    <div>
        <p></p>
    </div>
    
    image.png
    • 在浮动流中浮动元素内容的高不可以撑起盒子的高
    <style>
    
            div{
                background-color: red;
            }
            p{
                float: left;
                width: 200px;
                height: 100px;
                background-color: blue;
            }
    </style>
    <div>
        <p></p>
    </div>
    
    image.png

    清除浮动方式一

    • 给前面的父盒子添加高度
    <!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>
    <!--
    1.清除浮动的第一种方式
    给前面一个父元素设置高度
    注意点:
    在企业开发中, 我们能不写高度就不写高度, 所以这种方式用得很少
    -->
    <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>
    

    清除浮动方式二

    • 给后面的盒子添加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;
            }
            /*
            .dd{
                width: 500px;
                height: 500px;
                background-color: red;
                border: 1px solid #000;
            }
            .ddd{
                width: 200px;
                height: 200px;
                background-color: blue;
                margin-top: 100px;
            }
            */
        </style>
    </head>
    <body>
    <!--
    1.清除浮动的第二种方式
    给后面的盒子添加clear属性
    
    clear属性取值:
    none: 默认取值, 按照浮动元素的排序规则来排序(左浮动找左浮动, 右浮动找右浮动)
    left: 不要找前面的左浮动元素
    right: 不要找前面的右浮动元素
    both: 不要找前面的左浮动元素和右浮动元素
    
    注意点:
    当我们给某个元素添加clear属性之后, 那么这个属性的margin属性就会失效
    -->
    
    <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="dd">
        <div class="ddd"></div>
    </div>
    -->
    </body>
    </html>
    

    清除浮动的第三种方式

    • 1.隔墙法

    • 2.外墙法

    • 2.1在两个盒子中间添加一个额外的块级元素

    • 2.2给这个额外添加的块级元素设置clear: both;属性

    • 注意点:

    • 外墙法它可以让第二个盒子使用margin-top属性

    • 外墙法不可以让第一个盒子使用margin-bottom属性

    • 3.内墙法

    • 3.1在第一个盒子中所有子元素最后添加一个额外的块级元素

    • 3.2给这个额外添加的块级元素设置clear: both;属性

    • 注意点:

    • 内墙法它可以让第二个盒子使用margin-top属性

    • 内墙法它可以让第一个盒子使用margin-bottom属性

    • 4.外墙法和内墙法区别?

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

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

    <!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>
    <!--
    1.清除浮动的第三种方式
    隔墙法
    
    2.外墙法
    2.1在两个盒子中间添加一个额外的块级元素
    2.2给这个额外添加的块级元素设置clear: both;属性
    
    注意点:
    外墙法它可以让第二个盒子使用margin-top属性
    外墙法不可以让第一个盒子使用margin-bottom属性
    
    3.内墙法
    3.1在第一个盒子中所有子元素最后添加一个额外的块级元素
    3.2给这个额外添加的块级元素设置clear: both;属性
    
    注意点:
    内墙法它可以让第二个盒子使用margin-top属性
    内墙法它可以让第一个盒子使用margin-bottom属性
    
    4.外墙法和内墙法区别?
    外墙法不能撑起第一个盒子的高度, 而内墙法可以撑起第一个盒子的高度
    
    5.在企业开发中不常用隔墙法来清除浮动
    -->
    <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>
    
    • 1.什么是伪元素选择器?

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

    • 2.格式:
      标签名称::before{
      属性名称:值;
      }
      给指定标签的内容前面添加一个子元素

    标签名称::after{
    属性名称:值;
    }
    给指定标签的内容后面添加一个子元素

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>68-伪元素选择器</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            div{
                width: 200px;
                height: 200px;
                background-color: red;
            }
            /*
            p{
                width: 50px;
                height: 50px;
                background-color: pink;
            }
            */
    
            div::before{
                content: "爱你";
                width: 50px;
                height: 50px;
                background-color: pink;
                display: block;
            }
            div::after{
                /*指定添加的子元素中存储的内容*/
                content: "么么哒";
                /*指定添加的子元素的宽度和高度*/
                width: 50px;
                /*height: 50px;*/
                /*内容是可以超出标签的范围的, 所以高度为0依然可以看见内容*/
                height:0;
                background-color: pink;
                /*指定添加的子元素的显示模式*/
                display: block;
                /*隐藏添加的子元素*/
                visibility: hidden;
            }
    
        </style>
    </head>
    <body>
    <!--
    1.什么是伪元素选择器?
    伪元素选择器作用就是给指定标签的内容前面添加一个子元素或者给指定标签的内容后面添加一个子元素
    
    2.格式:
    标签名称::before{
        属性名称:值;
    }
    给指定标签的内容前面添加一个子元素
    
    标签名称::after{
        属性名称:值;
    }
    给指定标签的内容后面添加一个子元素
    -->
    <div>
        <!--<p>爱你</p>-->
        我是文字
        <!--<p>么么哒</p>-->
    </div>
    </body>
    </html>
    

    清除浮动的第四种方式

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

    • 本质上就是内墙法, 只不过是直接通过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>
    <!--
    1.清除浮动的第四种方式
    利用伪元素选择器清除浮动
    本质上就是内墙法, 只不过是直接通过CSS代码添加了内墙, 其它特性和内墙法都一样
    
    注意点:
    IE6中不支持这种方式, 为了兼容IE6必须给前面的盒子添加*zoom:1;属性
    -->
    <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>
    

    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>
    <!--
    1.overflow: hidden;作用
    1.1可以将超出标签范围的内容裁剪掉
    1.2清除浮动
    1.3可以通过overflow: hidden;让里面的盒子设置margin-top之后, 外面的盒子不被顶下来
    --><!--
    <div>我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字我是文字</div>
    -->
    <!--
    <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>
    

    注意点:

    添加伪元素后可以撑起盒子的高度, 所以可以直接设置margin属性
    先知道有这些方式, 原理需要学习到BFC和hasLayout才能明白
    支持BFC的浏览器(IE8+,firefox,chrome,safari)通过创建新的BFC闭合浮动;
    不支持 BFC的浏览器 (IE5-7),通过触发 hasLayout 闭合浮动。

    相关文章

      网友评论

          本文标题:清除浮动

          本文链接:https://www.haomeiwen.com/subject/cujzxxtx.html