Clear both 的妙用

在DIV + CSS设计网页中,经常需要设置多个DIV并列排列,往往是使用float:left或float:right来实现,但问题出现了,当前面并列的多个 DIV总宽度不足100%,下面的的DIV就很可能向上提,和上一行的并列的DIV在同一行,这不是我们想要的结果。使用Clear属性正好可以解决这一问题,下面引用帮助的介绍:

CSS clear属性

Image and text elements that appear in another element are called floating elements. The clear property sets the sides of an element where other floating elements are not allowed.
图片和文字元素出现在另外元素中,那么它们(图片和文字)可称为浮动元素(floating element)。使用clear属性可以让元素边上不出现其它浮动元素。

Note: This property does not always work as expected if it is used along with the "float" property.
注意: 当这个属性随着"float"属性一起使用的话,那么结果可能会不尽如人意

Inherited: No
继承性:

可用值

 

描述
leftNo floating elements allowed on the left side
不允许元素左边有浮动的元素
rightNo floating elements allowed on the right side
不允许元素的右边有浮动的元素
bothNo floating elements allowed on either the left or the right side
元素的两边都不允许有浮动的元素
noneAllows floating elements on both sides
允许元素两边都有浮动的元素

 

举例

Html代码 复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >   
  2. < html   xmlns = "http://www.w3.org/1999/xhtml" >   
  3. < head >   
  4. < meta   http-equiv = "Content-Type"   content = "text/html; charset=utf-8"   />   
  5. < title > 无标题文档 </ title >   
  6. < style   type = "text/css" >   
  7. .LeftText{   
  8.    margin: 3px;   
  9.    float: left;   
  10.    height: 180px;   
  11.    width: 170px;   
  12.    border: 1px solid #B1D1CE;   
  13.    background-color: #F3F3F3;   
  14. }   
  15.   
  16. .FootText{   
  17.    height: 180px;   
  18. }   
  19.   
  20. .Clear   
  21. {   
  22.    clear:both;   
  23. }   
  24. </ style >   
  25. </ head >   
  26.   
  27. < body >   
  28. < div   class = "LeftText" > 区块1 </ div >   
  29. < div   class = "LeftText" > 区块2 </ div >   
  30. < div   class = "Clear" > </ div >   
  31. < div   class = "FootText" > 区块3 </ div >   
  32. </ body >   
  33. </ html >   
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.LeftText{
   margin: 3px;
   float: left;
   height: 180px;
   width: 170px;
   border: 1px solid #B1D1CE;
   background-color: #F3F3F3;
}

.FootText{
   height: 180px;
}

.Clear
{
   clear:both;
}
</style>
</head>

<body>
<div class="LeftText">区块1</div>
<div class="LeftText">区块2</div>
<div class="Clear"></div>
<div class="FootText">区块3</div>
</body>
</html>

 代码说明:

如果没有Clear这一层,“区块3”会紧接区块2并列在同一行。
加了Clear这一层后,会把上面的浮动DIV的影响清除,使其不至影响下面DIV的布局

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值