清除float浮动

清除float浮动
clear:指定不允许元素周围有浮动元素。

left
right
both
none
inherit

请添加图片描述

<!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>
        #box1{width: 100px;height: 100px;background-color: red;float: left;}
        #box2{width: 200px;height: 200px;background-color: blue;/* clear: left */clear: both;}
    </style>
</head>
<body>
    <div id="box1"></div>
    <div id="box2"></div>
</body>
</html>

在这里插入图片描述
使用clear

<!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>
        .box1,.box2{
            width: 200px;
            height: 200px;
            float: left;
        }
        .box1{
            background-color: yellow;
        }
        .box2{
            background-color: blue;
        }
        .box{
            width: 300px;
            height: 300px;
            background-color: red;
            clear: left;
        }
    </style>
</head>
<body>
    <div>
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
    <div class="box"></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>
        .box1,.box2{
            width: 200px;
            height: 200px;
            float: left;
        }
        .box1{
            background-color: yellow;
        }
        .box2{
            background-color: blue;
        }
        .box{
            width: 300px;
            height: 300px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div>
        <div class="box1"></div>
        <div class="box2"></div>
         <div style="clear: both;"></div>
    </div>
    <div class="box"></div>
</body>
</html>

常用:after伪类
clear属性只能操作块标签,对内联标签不起作用

<!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>
        #box1{width: 200px;border: 1px black solid;}
        #box2{width: 100px;height: 200px;background-color: red; float: left;}
         .clear::after{content: '';clear: both;display: block;} /* 清除浮动 */
    </style>
</head>
<body>
    <div id="box1" class="clear">
        <div id="box2"></div>
    </div>
</body>
</html>

在这里插入图片描述
其他方法(不推荐)
固定宽高
父元素浮动(会影响后面的元素)
overflow:hidden(子元素溢出,受到影响)

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>
        .box1,.box2{
            width: 200px;
            height: 200px;
            float: left;
        }
        .box1{
            background-color: yellow;
        }
        .box2{
            background-color: blue;
        }
        .box{
            width: 300px;
            height: 300px;
            background-color: red;
        }
        .container {
            overflow: hidden;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
    <div class="box"></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>
        *{margin: 0;padding: 0;}
        ul{list-style: none;}
        img{display: block;}
        a{text-decoration: none;color: chocolate;}
        h1,h2,h3{font-size: 16px;}
        .l{float: left;}
        .r{float: right;}
        .clear{content: "";display: block;clear: both;}
        #main{width: 366px;margin: auto 20px;}
        #main.title{height: 23px;line-height: 23px;font-size: 12px;font-weight: bold;padding-left: 30px;/* background: url();repeat-x; */}
        #main ul{overflow: hidden;margin-top: 13px;}
        #main li{margin-bottom: 22px;}
        #main .pic{width: 99px;border: 1px solid greenyellow;}
        #main .pic img{margin: 2px;}
        #main .content{width: 240px;margin-left: 13px;}
        #main .content h2{font-size: 12px;line-height: 24px;}
        #main .content p{font-size: 12px;line-height: 20px;}
    </style>
</head>
<body>
    <div id="main">
        <h2 class="title">金晨</h2>
        <ul>
            <li class="clear">
                <div class="l pic">
                    <a href="#">
                        <img src="https://ps.ssl.qhimg.com/sdmt/120_186_100/t012df863969a6890e1.webp" width="50px"> 
                    </a>
                </div>
                <div class="l content">
                    <h2>金晨与神秘男子同返公寓</h2>
                    <p>
                        2月25日,有媒体曝光了一段金晨和一名年轻帅气小伙子共进晚餐后,同返公寓的画面,一时间也受到了网友们的广泛关注。当天的金晨和李斯丹妮一同现身北京机场,两人一黑一白的造型也特别...
                        <a href="#">详细</a>
                    </p>
                </div>
            </li>
        </ul>
    </div>
</body>
</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值