Float浮动&清除浮动产生的影响

Float浮动:浮动元素脱离文档流,原先位置不保留,飘在文档流上方

一、兄弟元素之间的浮动

<body>
    <div class="one"></div>
    <div class="two"></div>
</body>

1)、未浮动时

<style>
    .one {
        width: 100px;
        height: 100px;
        background-color: plum;
        padding-left: 50px;
    }

    .two {
        width: 100px;
        height: 200px;
        background-color: palevioletred;
    }
</style>

如图显示:
在这里插入图片描述2)、one向左浮动后:如下图,two所在的元素跑到了one的下方

.one {
    width: 100px;
    height: 100px;
    background-color: plum;
    float: left;
}

在这里插入图片描述3)、清除one的浮动对two的影响
给one的兄弟元素two的样式设置clear:both

.two {
    clear: both;
    width: 100px;
    height: 200px;
    background-color: palevioletred;
}

在这里插入图片描述

二、父子元素之间子元素浮动

<body>
    <div class="parent">
        <div class="child">child</div>
    </div>
</body>

1)、未浮动时

<style>
    .parent {
        width: 200px;
        border: 1px solid blue;
    }
    .child {
        width: 100px;
        height: 100px;
        background-color: palevioletred;
    }
</style>

在这里插入图片描述

2)、child向左浮动后

<style>
    .parent {
        width: 200px;
        border: 1px solid blue;
    }

    .child {
        width: 100px;
        height: 100px;
        background-color: palevioletred;
        float: left;
    }
</style>

child向左浮动后,因为父元素没有设置高度,所以父元素会发生高度塌陷,如下图

在这里插入图片描述3)、清除子元素浮动对父级元素产生的影响

给父元素的样式设置overflow:hidden;即可

<style>
    .parent {
        width: 200px;
        border: 1px solid blue;
        overflow: hidden;
    }

    .child {
        width: 100px;
        height: 100px;
        background-color: palevioletred;
        float: left;
    }
</style>

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值