美文网首页
清除浮动的几种方式

清除浮动的几种方式

作者: WPeach | 来源:发表于2017-03-11 10:28 被阅读0次

    浮动元素脱离文档流,不占据空间。浮动元素碰到包含它的边框或者浮动元素的边框停留。

    <style type="text/css">
            .float-div div{
                float: left;
                width: 100px;
                height: 100px;
                margin: 20px;
                background-color:#FFFF99;
                text-align: center;
                line-height: 100px;
            }
            .normal-div{
                width: 100px;
                height: 100px;
                background-color:#CCCCFF;
                line-height: 100px;
            }
        </style>
        <body>
            <div class="float-div">
                <div>1</div>
                <div>2</div>
                <div>3</div>
            </div>
            <div class="normal-div">A</div>
        </body>
    
    原排版图

    方法一:添加空div标签 clear:both

          <div class="float-div">
                <div>1</div>
                <div>2</div>
                <div>3</div>
         </div>
         <div class="clear-both"></div>
         <div class="normal-div">A</div>
    
        .clear-both{
                clear: both;
            }
    

    方法二:父级标签定义伪类after

    .float-div::after{
                display: block;
                clear: both;
                content: '';
                height: 0px;
            }
    

    方法三:父级标签overflow:hidden

     .float-div{
                overflow:hidden;
            }
    

    问题解决

    正确排版图

    相关文章

      网友评论

          本文标题:清除浮动的几种方式

          本文链接:https://www.haomeiwen.com/subject/scragttx.html