css浮动

学习内容一:

        如何进行浮动

在css样式里加float:left;或者float:right;

    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box1{
            width: 50px;
            height: 100px;
            background-color: skyblue;
            float: left;
        }
        .box2{
            width: 100px;
            height: 100px;
            background-color: pink;
            float: left;
        }
        .box3{
            width: 150px;
            height: 100px;
            background-color: black;
            float: left;
        }
        .box4{
            width: 200px;
            height: 100px;
            background-color: green;
            float: left;
        }
        .box5{
            width: 250px;
            height: 100px;
            background-color: purple;
            float: left;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
    <div class="box4"></div>
    <div class="box5"></div>
</body>

   浮动前的效果:

    浮动后的效果:


 浮动后的影响:

        

      1.脱离标准文档流

        影响:破坏了布局,导致布局错乱

        2.不脱离文本流

规定:

        同级之间,加float属性,其余的也要加。


浮动导致 父级盒子坍塌

        

        解决办法:

        1.父级盒子加高(不能自动撑开,高是固定的,不够灵活)

        ul{
            list-style: none;
            border: 5px solid yellow;
            /* 方法一:手动给父级盒子高 */
            height: 152px;
        }

        2.父级盒子加overflow:hidden;(自动撑开,但会导致溢出的部分无法显示)

        ul{
            list-style: none;
            border: 5px solid yellow;
            /* 方法二:溢出属性 */
            overflow: hidden;
        }

        3.加一个无内容的空盒子:clear:both;(必须无意义无宽高)

    <ul class="clear">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <!-- 方法三:加一个无内容的盒子,没有宽高 -->
        <div class="clear"></div>
    </ul>

                这是div盒子的css样式

        .clear{
            content: "";
            display: block;
            clear: both;
        }

       4.父盒子加伪类属性 ::after{content: '';display:block; clear:both}(达到添加无意义空盒子的效果且不受影响

<ul class="clear">

</ul>

        css样式

        .clear::after{
            content: "";
            display: block;
            clear: both;
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值