CSS 清除浮動的方法

來源:互聯網
上載者:User

標籤:nbsp   方法   相容   head   oct   png   清除   lock   back   

1. 使用額外標籤法
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <style>        .outer{            border: 1px solid black;            width: 210px;        }        .inner{            width: 50px;            height: 50px;            background-color: #005FC3;            margin:0 10px;            float: left;        }        .footer{            background-color: #ff4400;            width: 212px;            height: 100px;        }        .clearfix{            clear: both;        }    </style></head><body>    <div class="outer">        <div class="inner"></div>        <div class="inner"></div>        <div class="inner"></div>        <div class="clearfix"></div>    </div>    <div class="footer"></div></body></html>

這是早期普遍使用的方法,但是對於有代碼潔癖的人來說,解決的不夠完美

2. 使用 :after 為元素
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <style>        .outer{            border: 1px solid black;            width: 210px;        }        .inner{            width: 50px;            height: 50px;            background-color: #005FC3;            margin:0 10px;            float: left;        }        .footer{            background-color: #ff4400;            width: 212px;            height: 100px;        }        .clearfix:after{  /*最簡方式*/            content: ‘‘;            display: block;            clear: both;        }        /* 新浪使用方式        .clearfix:after{             content: ‘‘;            display: block;            clear: both;            height: 0;            visibility: hidden;        }        */        .clearfix{ /*相容 IE*/            zoom: 1;        }    </style></head><body>    <div class="outer clearfix">        <div class="inner"></div>        <div class="inner"></div>        <div class="inner"></div>    </div>    <div class="footer"></div></body></html>

3. 給父元素定高
<!DOCTYPE html><html lang="en">    <head>        <meta charset="UTF-8">        <title></title>        <style>            .outer {                border: 1px solid black;                width: 210px;                height: 50px;            }            .inner {                width: 50px;                height: 50px;                background-color: #ff4400;                margin: 0 10px;                float: left;            }            .footer {                background-color: #005FC3;                width: 212px;                height: 100px;            }        </style>    </head>    <body>        <div class="outer">            <div class="inner"></div>            <div class="inner"></div>            <div class="inner"></div>        </div>        <div class="footer"></div>    </body></html>

4. 利用 overflow:hidden 屬性
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <style>        .outer{            border: 1px solid black;            width: 210px;            overflow: hidden;            zoom: 1;/*相容 IE*/        }        .inner{            width: 50px;            height: 50px;            background-color: #005FC3;            margin:0 10px;            float: left;        }        .footer{            background-color: #ff4400;            width: 212px;            height: 100px;        }    </style></head><body>    <div class="outer">        <div class="inner"></div>        <div class="inner"></div>        <div class="inner"></div>    </div>    <div class="footer"></div></body></html>

 

CSS 清除浮動的方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.