css中清除浮動方法介紹

來源:互聯網
上載者:User

 

 代碼如下 複製代碼
   <style>
            .container {
                border: 3px solid #000;
                width: 600px;
                background-color: #eee;
                margin-bottom:50px;
            }
           
            .floatedbox {
                float: left;
                border: 3px solid #bbb;
                width: 125px;
                height: 125px;
                background-color: #fff;
                margin-right: 1em;
                padding: 0 10px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="floatedbox">
                <p>
                    floated box
                </p>
            </div>
            <h3>Container 1</h3>
            <p>
                (<em>without</em>
                easy clearing)
            </p>
        </div>
    </body>
</html>


去網上查了一下,這就是著名的浮動問題。我們需要做的就是將浮動效果給清除。
咋個解決方案
網上有好多解決辦法,但是普遍是在這三類中:
在浮動的div外層,增加含有clear:both樣式定義的div元素
使用overflow:auto,但是這個傳說中會引起一些很杯具的效果
使用after虛擬元素進行處理,同時對於IE使用hack
首先第二種,既然傳說中會杯具,那麼我肯定不用。而第一種,對於我這種有類似html潔癖的人也肯定不會使用,平白無故增加一個div標籤,實在感覺有些划不來,還有些噁心。
所以我彩after虛擬元素來處理,具體處理後的代碼如下:

 代碼如下 複製代碼

 

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>簡易浮動清除</title>
        <style>
            .container {
                border: 3px solid #000;
                width: 600px;
                background-color: #eee;
                margin-bottom:50px;
            }
           
            .floatedbox {
                float: left;
                border: 3px solid #bbb;
                width: 125px;
                height: 125px;
                background-color: #fff;
                margin-right: 1em;
                padding: 0 10px;
            }
           
            .clearfix:after{
                content:".";
                display:block;
                height:0;
                clear:both;
                visibility:hidden;
            }
           
            * html .clearfix{height:1%;}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="floatedbox">
                <p>
                    floated box
                </p>
            </div>
            <h3>Container 1</h3>
            <p>
                (<em>without</em>
                easy clearing)
            </p>
        </div>

    <div class="container">
            <div class="floatedbox">
                <p>
                    floated box
                </p>
            </div>
            <h3>Container 1</h3>
            <p>
                (<em>without</em>
                easy clearing)
            </p>
        <div style="clear:both;"></div>
        </div>
   
        <div class="clearfix container">
            <div class="floatedbox">
                <p>
                    floated box
                </p>
            </div>
            <h3>Container 1</h3>
            <p>
                (<em>without</em>
                easy clearing)
            </p>
        </div>
    </body>
</html>

問題得以解決。網上還有人說在div標籤後面加上一個“點”感覺不爽,而且還要使用visibility來隱藏。所以建議使用如下代碼替代:

 代碼如下 複製代碼

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

這樣我們的問題就解決了。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.