.clearfix:after的用法,清除浮動


今天回顧一下css中的清除浮動

 /*所有主流瀏覽器都支持 :after 偽元素。*/
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden}
    
.clearfix{*+height:1%;}/*不知道有什么用處,不加ie7也沒有問題*/

 

測試代碼:

<html >
 <head>
  <title> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
 </head>

 <body>
 <style type="text/css">
    /*所有主流瀏覽器都支持 :after 偽元素。*/
    .clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden}
    
    .clearfix{*+height:1%;}/*不知道有什么用處,不加ie7也沒有問題*/

    .box{ background:#111;width:500px; position:relative;;}
    .l{float:left; background:#333;width:200px; height:100px;}
    .r{float:right;background:#666;width:200px; height:200px;}
    .s{width:100px; height:100px;background:#999;position:absolute;right:-50px;;}
 </style>
  <div class="box clearfix">
    <div class="l">left</div>
    <div class="r">right</div>
    <div class="s">absolute</div>
  </div>
 </body>
</html>

 

擴展閱讀:

CSS :after 偽元素 http://www.w3school.com.cn/css/pr_pseudo_after.asp

:after 偽元素在元素之后添加內容。

這個偽元素允許創作人員在元素內容的最后面插入生成內容。默認地,這個偽元素是行內元素,不過可以使用屬性 display 改變這一點。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM