CSS:浮动清理另类方法

作者:edream 来源:沉寂博客 时间:2008-11-17 11:45:00 

在进行浮动布局时,大多数人都深知,在必要的地方进行浮动清理:<div style="clear:both;"></div>。

例如: 

<div style="background:#666;"> <!-- float container -->
                <div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
</div>

 此时预览此代码,我们会发现最外层的父元素float container,并没有显示。这是因为子元素因进行了浮动,而脱离了文档流,导致父元素的height为零。

若将代码修改为:  

<div style="background:#666;"> <!-- float container -->
                <div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
                <div style="clear:both"></div>
</div>

 注意,多了一段清理浮动的代码。这是一种好的CSS代码习惯,但是这种方法增加了无用的元素。这里有一种更好的方法,将HTML代码修改为:

        <div  class="clearfix" style="background:#666;"> <!-- float container -->
                <div style="float:left; width:30%; height:40px;background:#EEE; ">Some Content</div>
        </div>

定义CSS类,进行“浮动清理”的控制:  

 .clearfix:after {}{
  content: ".";
  clear: both;
  height: 0;
  visibility: hidden;
  display: block;
}            /* 这是对Firefox进行的处理,因为Firefox支持生成元素,而IE所有版本都不支持生成元素 */
.clearfix {}{
  display: inline-block;     
}                /* 这是对 Mac 上的IE浏览器进行的处理 */
/**//* Hides from IE-mac \*/
* html .clearfix {}{height: 1%;}        /* 这是对 win 上的IE浏览器进行的处理 */
.clearfix {}{display: block;}        /* 这是对display: inline-block;进行的修改,重置为区块元素*/
/**//* End hide from IE-mac */   

此时,预览以上代码(  删去这种注释   ),会发现即使子元素进行了浮动,父元素float container仍然会将其包围,进行高度自适应。

代码参考:http://www.positioniseverything.net/easyclearing.html

clear元素的margin-top被重置为零,当你使用clear(left & both & right)清理一个浮动元素时,该元素的margin-top会被重置为0。所以为了创建浮动列,并使用footer进行浮动清理时,必须对浮动列(sidebar && content)都指定margin-bottom,最好margin-bottom相同。(Firefox会将margin-top重置0,而IE不重置footer的margin-top)。

例如:

分别在Firefox和IE中运行一下代码,仔细读者会发现页脚(footer)的margin-top在火狐中并没有显示,而在IE中却出现了10像素的上边距。

HTML代码  

<body>
<div id="wrapper">
    <div id="masthead">
        masthead content goes here
    </div>
    <div id="sidebar">
        sidebar content goes here
    </div>
    <div id="content">
        main content goes here
        <br/>
        main content goes here
    </div>
    <div id="footer">
        footer
    </div>
</div>

</body>CSS代码 body {}{
    margin:0; padding:0;
    background-color:#FFFFCC;
}
#wrapper {}{
    width:800px;
    margin:0 auto;
}
/**//*Masthead*/
#masthead {}{
    padding:10px;
    background:#FFCC33;
    margin-bottom:10px;
}
/**//*Content*/
#content {}{
    float:left;
    width:60%;
    background:#CCCCCC;
}
/**//*Sidebar*/
#sidebar {}{
    float:right;
    width:36%;
    background:#999999;
}
/**//*Footer*/
#footer {}{
    clear:both;
    padding:10px;
    background:#FFCC33;
}
标签:浮动,css,布局,代码
0
投稿

猜你喜欢

  • 用ASP实现IE地址栏参数的判断

    2008-10-10 15:54:00
  • 分享13款非常有用的jQuery插件

    2011-05-16 19:07:00
  • asp Server对象之MapPath方法

    2010-07-07 12:28:00
  • 站长如何活用"nofollow"标签

    2008-05-13 12:40:00
  • 安装PHP遇到“无法载入mysql扩展”解决方法

    2007-06-15 15:04:00
  • 学以致用 驳“ASP低能论”

    2007-09-30 13:01:00
  • MySQL 语言参考

    2007-11-21 20:37:00
  • 我要如何了解用户的需求

    2007-08-26 17:19:00
  • 如何将多宿主计算机配置为允许SQL Server访问

    2011-01-04 14:04:00
  • SQL Join的一些总结(实例)

    2012-08-21 10:19:29
  • 网页中英文混排行高不等问题

    2008-08-26 17:03:00
  • asp如何让计数器只对新进用户计数?

    2010-05-13 16:36:00
  • shtml网页SSI使用详解

    2008-02-20 19:13:00
  • ie和firefox中css自动换行实现方法

    2008-04-08 12:49:00
  • Windows下ORACLE 10g完全卸载的方法分析

    2012-07-11 16:09:26
  • 中国传统色彩名录

    2007-11-29 18:36:00
  • Oracle轻松取得建表和索引的DDL语句

    2009-02-26 10:26:00
  • FrontPage2002简明教程三:网页布局

    2008-09-17 11:19:00
  • Access2000迁移到Oracle9i要点

    2010-07-27 13:10:00
  • asp使用ServerVariables集合

    2008-02-27 13:22:00
  • asp之家 网络编程 m.aspxhome.com