CSS复习(1)——清除浮动

1. 为什么需要清除浮动?
​ 由于父级盒子在很多情况下,不方便给高度,但是子盒子浮动又不占有位置,就会影响下面的标准流盒子(标准流盒子上移)。
2. 清除浮动的本质
清除浮动的本质是清除浮动元素造成的影响:浮动的子标签无法撑开父盒子的高度

  • 如果父盒子本身有高度,则不需要清除浮动
  • 清除浮动之后,父级就会根据浮动的子盒子自动检测高度
  • 父级有了高度,就不会影响下面的标准流了

3.实现

  1. 给父元素添加高度
.father {
            width: 200px;
            height: 300px;
            border: 1px solid red;
        }
        
        .big {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }
        
        .small {
            width: 50px;
            height: 50px;
            background-color: yellow;
            float: left;
        }
        
        .footer {
            width: 200px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="big">big</div>
        <div class="small">small</div>
    </div>
    <div class="footer"></div>
</body>

在这里插入图片描述
2. 给父级元素设置overflow:hidden不推荐

内容增多的时候容易造成不会自动换行导致内容被隐藏掉,无法显示要溢出的元素

 <style>
        .father {
            width: 200px;
            height: 250px;
            border: 1px solid red;
            overflow: hidden;
        }
        
        .big {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }
        
        .small {
            width: 50px;
            height: 50px;
            background-color: yellow;
            float: left;
        }
        
        .footer {
            width: 200px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="big">big</div>
        <div class="small">small</div>
    </div>
    <div class="footer"></div>
</body>
  1. 额外标签法(在子元素后面追加一个空白标签)不推荐

添加无意义标签,语义化差

.father {
            border: 1px solid red;
        }
        
        .big {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }
        
        .small {
            width: 50px;
            height: 50px;
            background-color: yellow;
            float: left;
        }
        
        .footer {
            width: 200px;
            height: 100px;
            background-color: blue;
        }
         .clear {
            clear: both;
        } 
    </style>
</head>

<body>
    <div class="father">
        <div class="big">big</div>
        <div class="small">small</div>
        <div class="clear">额外标签法</div>
    </div>
    <div class="footer"></div>
</body>

在这里插入图片描述
4. 父级添加after伪元素推荐

伪元素是行内元素 正常浏览器清除浮动方法

 <style>
        .father {
            width: 200px;
            height: 250px;
            border: 1px solid red;
        }
        
        .clear:after {
            content: "";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }
        
        .clear {
            zoom: 1;
            /* 兼容ie6-7 */
        }
        
        .father .big {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }
        
        .small {
            width: 50px;
            height: 50px;
            background-color: yellow;
            float: left;
        }
        
        .footer {
            width: 200px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>

<body>
    <div class="father clear">
        <div class="big">big</div>
        <div class="small">small</div>
    </div>
    <div class="footer"></div>
</body>
  1. 父级添加双伪元素推荐
<style>
        .father {
            width: 200px;
            height: 250px;
            border: 1px solid red;
        }
        
        .clear:before,
        .clear:after {
            content: "";
            display: table;
        }
        
        .clear:after {
            clear: both;
        }
        
        .clear {
            zoom: 1;  /* 兼容ie6-7 */
        }
        
        .father .big {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }
        
        .small {
            width: 50px;
            height: 50px;
            background-color: yellow;
            float: left;
        }
        
        .footer {
            width: 200px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>

<body>
    <div class="father clear">
        <div class="big">big</div>
        <div class="small">small</div>
    </div>
    <div class="footer"></div>
</body>

总结

  1. 给父级设置高度height
  2. 结尾处添加一个空白div标签设置clear:both
  3. 父级定义伪类:after和zoom
  4. 父级定义overflow:hidden
  5. 父级也浮动,但是需要定义宽度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值