清除浮动

使用带有clear属性的空元素

在浮动元素后面使用一个空元素来清除浮动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .content {
            width: 400px;
            background: #1bff18;
        }

        .item1 {
            background: #f71d1a;
            width: 100px;
            height: 100px;
            float: left;

        }

        .item2 {
            background: #ff944f;
            width: 100px;
            height: 100px;
            float: right;

        }

        .item3 {
            background: #e9fd29;
            width: 200px;
            height: 200px;
        }

        .clear {
            clear: both;
        }

    </style>
</head>
<body>
<div class="content">
    <div class="item1"></div>
    <div class="item2"></div>
    <div class="clear"></div>
    <div class="item3"></div>
</div>
</body>
</html>

overflow: hiden或auto

为浮动元素的容器添加overflow:hiden或者overflow: auto

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .content {
            width: 400px;
            background: #1bff18;
            overflow: hidden;
            zoom:1

        }

        .item1 {
            background: #f71d1a;
            width: 100px;
            height: 100px;
            float: left;

        }

        .item2 {
            background: #ff944f;
            width: 100px;
            height: 100px;
            float: right;

        }

        .item3 {
            background: #e9fd29;
            width: 200px;
            height: 200px;
        }


    </style>
</head>
<body>
<div class="content">
    <div class="item1"></div>
    <div class="item2"></div>
    <div class="item3"></div>
</div>
</body>
</html>

与使用空的元素来清除浮动相比,但是item1会覆盖了item3

给浮动元素容器添加浮动

给浮动元素添加浮动,这样可以清除内部的浮动,但是还会有外部浮动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .content {
            width: 400px;
            background: #1bff18;
            float: left;
        }

        .item1 {
            background: #f71d1a;
            width: 100px;
            height: 100px;
            float: left;

        }

        .item2 {
            background: #ff944f;
            width: 100px;
            height: 100px;
            float: right;

        }

        .content1 {
            background: #e9fd29;
            width: 200px;
            height: 200px;
        }

    </style>
</head>
<body>
<div class="content">
    <div class="item1"></div>
    <div class="item2"></div>
</div>
<div class="content1"></div>
</body>
</html>

使用:after的伪元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .content {
            width: 400px;
            background: #1bff18;
        }

        .item1 {
            background: #f71d1a;
            width: 100px;
            height: 100px;
            float: left;

        }

        .item2 {
            background: #ff944f;
            width: 100px;
            height: 100px;
            float: right;

        }

        .item3 {
            background: #e9fd29;
            width: 200px;
            height: 200px;
        }

        .clearfix:after {
            content: '';
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }

    </style>
</head>
<body>
<div class="content clearfix">
    <div class="item1"></div>
    <div class="item2"></div>
    <div class="item3"></div>
</div>
</body>
</html>

与使用空的元素来清除浮动相比,但是item1会覆盖了item3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值