学习CSS之清除浮动

一、clear:both

1 概念

根据CSS规范,clear:both要求:该盒子必须出现在所有左浮动和右浮动盒子的下方
如果前方有浮动盒子,浏览器会将该盒子下推到浮动盒子的最底部。进而强制父盒子重新计算高度,直到高度能包裹所有浮动盒。

2 例如

设置浮动后,父盒没有设置高度,其高度由子盒撑开,若子盒全部浮走,则高度为0

会造成高度塌陷:

<!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>清除浮动练习</title>
    <style>
        nav{
            background-color: bisque;
            border: black 5px solid;
        }
        #hezi1{
            width: 300px;
            height: 50px;
            background-color: aqua;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
            
        }
        #hezi2{
            width: 300px;
            height: 50px;
            background-color: aquamarine;
            border: black 5px solid;
            margin-bottom: 5px;
            float: right;
        }
        #hezi3{
            width: 300px;
            height: 50px;
            background-color: blue;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
        }
        #hezi4{
            width: 300px;
            height: 50px;
            background-color: brown;
            border: black 5px solid;
            margin-bottom: 5px;
            float: right;
        }
        #hezi5{
            width: 300px;
            height: 50px;
            background-color: blueviolet;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
        }
        nav::after{
            content: "";
            clear: both;
        }
        footer{
            height: 50px;
            background-color: yellow;
            border: black 3px solid;
        }
    </style>
</head>
<body>
    <nav>
        <div id="hezi1">盒子1</div>
        <div id="hezi2">盒子2</div>
        <div id="hezi3">盒子3</div>
        <div id="hezi4">盒子4</div>
        <div id="hezi5">盒子5</div>
    </nav>
    <footer id="footer1">footer1</footer>
    <footer id="footer2">footer2</footer>
    <footer id="footer3">footer3</footer>

</body>
</html>

 用clear:both清除浮动

 解决这一问题:

<!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>清除浮动练习</title>
    <style>
        nav{
            background-color: bisque;
            border: black 5px solid;
        }
        #hezi1{
            width: 300px;
            height: 50px;
            background-color: aqua;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
            
        }
        #hezi2{
            width: 300px;
            height: 50px;
            background-color: aquamarine;
            border: black 5px solid;
            margin-bottom: 5px;
            float: right;
        }
        #hezi3{
            width: 300px;
            height: 50px;
            background-color: blue;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
        }
        #hezi4{
            width: 300px;
            height: 50px;
            background-color: brown;
            border: black 5px solid;
            margin-bottom: 5px;
            float: right;
        }
        #hezi5{
            width: 300px;
            height: 50px;
            background-color: blueviolet;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
        }
        nav::after{
            content: "";
            clear: both;
        }
        footer{
            height: 50px;
            background-color: yellow;
            border: black 3px solid;
        }
    </style>
</head>
<body>
    <nav>
        <div id="hezi1">盒子1</div>
        <div id="hezi2">盒子2</div>
        <div id="hezi3">盒子3</div>
        <div id="hezi4">盒子4</div>
        <div id="hezi5">盒子5</div>
        <!-- 下面这个块元素,无实际显示效果,只是为了撑开父盒,
        解决父盒高度塌陷问题,避免影响父盒的兄弟盒子 -->
        <div style="clear: both;"></div>
    </nav>
    <footer id="footer1">footer1</footer>
    <footer id="footer2">footer2</footer>
    <footer id="footer3">footer3</footer>

</body>
</html>

 二、clear:left

1 概念

根据 CSS 规范,clear:left要求:该盒子必须出现在所有左浮动盒子下方
如果前方有左浮动盒子,浏览器会将该盒子下推到所有左浮动盒子的最底部。进而强制父盒子重新计算高度,直到高度能包裹所有左浮动盒。

2 例如

设置clear:left只会撑开左浮动的盒子,右浮动的盒子不能撑开

<!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>清除左浮动</title>
    <style>
        nav{
            background-color: bisque;
            border: black 5px solid;
        }
        #hezi1{
            width: 300px;
            height: 400px;
            background-color: aqua;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
            
        }
        #hezi2{
            width: 300px;
            height: 500px;
            background-color: aquamarine;
            border: black 5px solid;
            margin-bottom: 5px;
            float: right;
        }
        #hezi3{
            width: 300px;
            height: 50px;
            background-color: blue;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
        }
        #hezi4{
            width: 300px;
            height: 500px;
            background-color: brown;
            border: black 5px solid;
            margin-bottom: 5px;
            float: right;
        }
        #hezi5{
            width: 300px;
            height: 50px;
            background-color: blueviolet;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
        }
        nav::after{
            content: "";
            clear: both;
        }
        footer{
            height: 50px;
            background-color: yellow;
            border: black 3px solid;
        }
    </style>
</head>
<body>
    <nav>
        <div id="hezi1">盒子1</div>
        <div id="hezi2">盒子2</div>
        <div id="hezi3">盒子3</div>
        <div id="hezi4">盒子4</div>
        <div id="hezi5">盒子5</div>
        <!-- 下面这个块元素,无实际显示效果,只是为了撑开父盒,
        解决父盒高度塌陷问题,避免影响父盒的兄弟盒子 -->
        <div style="clear: left;"></div>
    </nav>
    <footer id="footer1">footer1</footer>
    <footer id="footer2">footer2</footer>
    <footer id="footer3">footer3</footer>

</body>
</html>

 三、clear:right

1 概念

根据 CSS 规范,clear:right要求:该盒子必须出现在所有右浮动盒子下方
如果前方有右浮动盒子,浏览器会将该盒子下推到所有右浮动盒子的最底部。进而强制父盒子重新计算高度,直到高度能包裹所有右浮动盒。

2 例如

设置clear:left只会撑开右浮动的盒子,左浮动的盒子不能撑开

<!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>清除右浮动</title>
    <style>
        nav{
            background-color: bisque;
            border: black 5px solid;
        }
        #hezi1{
            width: 300px;
            height: 500px;
            background-color: aqua;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
            
        }
        #hezi2{
            width: 300px;
            height: 400px;
            background-color: aquamarine;
            border: black 5px solid;
            margin-bottom: 5px;
            float: right;
        }
        #hezi3{
            width: 300px;
            height: 50px;
            background-color: blue;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
        }
        #hezi4{
            width: 300px;
            height: 400px;
            background-color: brown;
            border: black 5px solid;
            margin-bottom: 5px;
            float: right;
        }
        #hezi5{
            width: 300px;
            height: 50px;
            background-color: blueviolet;
            border: black 5px solid;
            margin-bottom: 5px;
            float: left;
        }
        nav::after{
            content: "";
            clear: both;
        }
        footer{
            height: 50px;
            background-color: yellow;
            border: black 3px solid;
        }
    </style>
</head>
<body>
    <nav>
        <div id="hezi1">盒子1</div>
        <div id="hezi2">盒子2</div>
        <div id="hezi3">盒子3</div>
        <div id="hezi4">盒子4</div>
        <div id="hezi5">盒子5</div>
        <!-- 下面这个块元素,无实际显示效果,只是为了撑开父盒,
        解决父盒高度塌陷问题,避免影响父盒的兄弟盒子 -->
        <div style="clear: right;"></div>
    </nav>
    <footer id="footer1">footer1</footer>
    <footer id="footer2">footer2</footer>
    <footer id="footer3">footer3</footer>

</body>
</html>

四、盒子的展示

display属性(用于元素性质的转换)

inline 以行内元素展示

block 以块元素展示

none 隐藏

块元素 可以设置宽高属性,独占一行,例如:h1,p

行内元素 不可以设置宽高属性,不独占一行,例如:b,ins

行内块元素 可以设置宽高属性,不独占一行,例如:img

隐藏none 整个盒子隐藏(并释放文档流位置)

Fighting!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值