浮动清理另类方法

在进行浮动布局时,大多数人都深知,在必要的地方进行浮动清理:

<div style="clear:both;"></div>
Html代码 复制代码
  1. < BR > < div   style = "background:#666;" > < BR > <!-- float container --> < BR >    < div   style = "float:left; width:30%; height:40px;background:#EEE; " > Some Content </ div > < BR > </ div > < BR >   

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


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

Html代码 复制代码
  1. < BR > < div   style = "background:#666;" > < BR >    <!-- float container --> < BR >    < div   style = "float:left; width:30%; height:40px;background:#EEE; " > Some Content </ div > < BR >    < div   style = "clear:both" > </ div > < BR > </ div > < BR >   

<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代码修改为:

Html代码 复制代码
  1. < BR > < div    class = "clearfix"   style = "background:#666;" > < BR >    <!-- float container --> < BR >    < div   style = "float:left; width:30%; height:40px;background:#EEE; " > Some Content </ div > < BR > </ div > < BR >   

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


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

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

.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代码 复制代码
  1. < BR > < body > < BR > < div   id = "wrapper" > < BR >      < div   id = "masthead" > < BR >         masthead content goes here < BR >      </ div > < BR >      < div   id = "sidebar" > < BR >         sidebar content goes here < BR >      </ div > < BR >      < div   id = "content" > < BR >         main content goes here < BR >          < br /> < BR >         main content goes here < BR >      </ div > < BR >      < div   id = "footer" > < BR >         footer < BR >      </ div > < BR > </ div > < BR > < BR > </ body > < BR >   

<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代码 复制代码
  1. <BR>body {}{<BR>    margin: 0 ; padding: 0 ;<BR>    background-color:#FFFFCC;<BR>}<BR>#wrapper {}{<BR>    width:800px;<BR>    margin: 0  auto;<BR>}<BR>/**//*Masthead*/<BR>#masthead {}{<BR>    padding:10px;<BR>    background:#FFCC33;<BR>    margin-bottom:10px;<BR>}<BR>/**//*Content*/<BR>#content {}{<BR>    float:left;<BR>    width: 60 %;<BR>    background:#CCCCCC;<BR>}<BR>/**//*Sidebar*/<BR>#sidebar {}{<BR>    float:right;<BR>    width: 36 %;<BR>    background:# 999999 ;<BR>}<BR>/**//*Footer*/<BR>#footer {}{<BR>    clear:both;<BR>    padding:10px;<BR>    background:#FFCC33;<BR>}<BR> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值