CSS-浮动,清除浮动

浮动
浮动流:块级元素无法识别浮动流元素的位置

<!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;
            float: left;
            background-color: green;
        }

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

<body>
    <div class="left">
    </div>
    <div class="right"></div>
</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;
            float: left;
            background-color: green;
        }

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

<body>
    <div class="left">
    </div>
    <span>123</span>
</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>
        ul::after,
        div::after {
            content: "";
            display: block;
            clear: both;
        }

        .box {
            width: 200px;
            border: 10px solid black;
        }

        .box1 {
            float: left;
            width: 100px;
            height: 100px;
            background-color: green;
        }

        .box2 {
            float: left;
            width: 100px;
            height: 100px;
            background-color: orange;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
</body>

</html>

效果如下:
在这里插入图片描述
使用伪元素清除浮动
伪元素::before
清除浮动小案例


```css
<!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>
        .slider{
            width: 590px;
            height: 470px;
            position: relative;
        }
        .slider .indicator{
            width: 152px;
            height: 18px;
            position: absolute;
            bottom: 36px;
            left: 18px;
        }
        .slider .indicator .indicator-dot{
            display: block;
            float: left;
            width: 18px;
            height: 18px;
            position: relative;
            /* border: 1px solid black; */
            box-sizing: border-box;
        }
        .slider .indicator .indicator-dot::after{
            content: "";
            display: block;
            width: 6px;
            height: 6px;
            border: 2px solid white;
            position: absolute;
            top: 4px;
            left: 4px;
            border-radius: 50%;
            opacity: .4;
        }
        .slider .indicator .indicator-dot.active::before{
            display: block;
            content: "";
            width: 18px;
            height: 18px;
            background-color: white;
            opacity: .2;
            border-radius: 50%;
        }
        .slider .indicator .indicator-dot.active::after{
            opacity: 1;
            background-color: #fff;
        }
    </style>
</head>

<body>
    <div class="slider">
        <div class="indicator">
            <i class="indicator-dot active"></i>
            <i class="indicator-dot"></i>
            <i class="indicator-dot"></i>
            <i class="indicator-dot"></i>
            <i class="indicator-dot"></i>
            <i class="indicator-dot"></i>
            <i class="indicator-dot"></i>
            <i class="indicator-dot"></i>
        </div>
        <img src="img/q.jpg" alt=""/>
    </div>
</body>

</html>

实现如下效果:
京东小案例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值