09-CSS清除浮动

2018-10-29  本文已影响0人  喝酸奶要舔盖__

清除浮动方式一

清除浮动方式二

清除浮动方式三

<style>
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            background-color: red;
            /*margin-bottom: 10px;*/
        }
        .box2{
            background-color: green;
            /*margin-top: 10px;*/
        }
        .box1 p{
            width: 100px;
            background-color: blue;
        }
        .box2 p{
            width: 100px;
            background-color: yellow;
        }
        p{
            float: left;
        }
        .wall{
            clear: both;
        }

        /*如果想要让两个盒子中间有间距,一般都会设置墙的高度*/
        .h20{
            height: 20px;
            background-color: skyblue;
        }
    </style>
<div class="box1">
    <p>我是文字1</p>
    <p>我是文字1</p>
    <p>我是文字1</p>
</div>

<div class="wall h20"></div>

<div class="box2">
    <p>我是文字2</p>
    <p>我是文字2</p>
    <p>我是文字2</p>
</div>
<style>
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            background-color: red;
            /*margin-bottom: 10px;*/
        }
        .box2{
            background-color: green;
            /*margin-top: 10px;*/
        }
        .box1 p{
            width: 100px;
            background-color: blue;
        }
        .box2 p{
            width: 100px;
            background-color: yellow;
        }
        p{
            float: left;
        }
        .wall{
            clear: both;
        }

        /*如果想要让两个盒子中间有间距,一般都会设置墙的高度*/
        .h20{
            height: 20px;
            background-color: skyblue;
        }
    </style>
<div class="box1">
    <p>我是文字1</p>
    <p>我是文字1</p>
    <p>我是文字1</p>
    <div class="wall h20"></div>
</div>
<div class="box2">
    <p>我是文字2</p>
    <p>我是文字2</p>
    <p>我是文字2</p>
</div>

伪元素选择器

格式:
标签名称::before{
    属性名称:值;
}
给指定标签的内容前面添加一个子元素

标签名称::after{
    属性名称:值;
}
给指定标签的内容后面添加一个子元素
<style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 200px;
            background-color: red;
        }

        /*div::before{
            content: "爱你";
            width: 50px;
            height: 50px;
            background-color: pink;
            display: block;
        }*/
        div::after{
            /*指定添加的子元素中存储的内容*/
            content: "";
            /*指定添加的子元素的宽度和高度*/
            width: 30px;
            height: 30px;
            /*注意点: 内容是可以超出标签的范围的, 所以高度为0依然可以看见内容*/
            /*指定添加的子元素的显示模式*/
            display: block;
            background-color: pink;
            /*隐藏添加的子元素*/
            visibility: hidden;
        }
    </style>

清除浮动方式四

<style>
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            background-color: red;
            /*margin-bottom: 10px;*/
        }
        .box2{
            background-color: green;
            /*margin-top: 10px;*/
        }
        .box1 p{
            width: 100px;
            background-color: blue;
        }
        .box2 p{
            width: 100px;
            background-color: yellow;
        }
        p{
            float: left;
        }
        .box1::after{
            content: "";
            height: 0;
            display: block;
            visibility: hidden;
            /*给添加的子元素设置clear: both;*/
            clear: both;
        }
        /*兼容IE6*/
        .box1{
            *zoom: 1;
        }
</style>
<div class="box1">
    <p>我是文字1</p>
    <p>我是文字1</p>
    <p>我是文字1</p>
</div>

<div class="box2">
    <p>我是文字2</p>
    <p>我是文字2</p>
    <p>我是文字2</p>
</div>

清除浮动方式五

上一篇 下一篇

猜你喜欢

热点阅读