清除浮动的几种方法

一、在列表最后加一个子元素列表项,然后清除浮动。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .left {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .right {
            width: 100px;
            height: 100px;
            background-color: orange;
            float: right;
        }
        .clearfix {
            clear: both;
        }
    </style>
</head>

<body>
    <div>
        <div class="left"></div>
        <div class="right"></div>
        <div class="clearfix"></div>
    </div>
    <p>哈喽你好</p>
</body>

</html>

二、对父元素使用伪元素

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .left {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .right {
            width: 100px;
            height: 100px;
            background-color: orange;
            float: right;
        }
        .parent::after {
            display: block;
            content: "";
            clear: both;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="left"></div>
        <div class="right"></div>
    </div>
    <p>哈喽你好</p>
</body>

</html>

三、给列表加一个高度

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .left {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .right {
            width: 100px;
            height: 100px;
            background-color: orange;
            float: right;
        }
        .parent {
            height: 200px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="left"></div>
        <div class="right"></div>
    </div>
    <p>哈喽你好</p>
</body>

</html>

四、给父元素添加overflow: hidden;

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .left {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .right {
            width: 100px;
            height: 100px;
            background-color: orange;
            float: right;
        }
        .parent {
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="left"></div>
        <div class="right"></div>
    </div>
    <p>哈喽你好</p>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值