技术分享 调用clear:both清除浮动

开发 前端
在CSS中我们会经常要用到“清除浮动”Clear,比较典型的就是clear:both;这里向大家简单描述一下clear:both清除浮动的用,希望对你的学习有所帮助。

本文和大家重点讨论一下clear:both清除浮动的使用,在CSS中我们会经常要用到“清除浮动”Clear,比较典型的就是clear:both;该属性的值指出了不允许有浮动对象的边。这个属性是用来控制float属性在文档流的物理位置的。

用clear:both清除浮动

在CSS中我们会经常要用到“清除浮动”Clear,比较典型的就是clear:both;

CSS手册上是这样说明的:该属性的值指出了不允许有浮动对象的边。这个属性是用来控制float属性在文档流的物理位置的。

◆当属性设置float(浮动)时,其所在的物理位置已经脱离文档流了,但是大多时候我们希望文档流能识别float(浮动),或者是希望float(浮动)后面的元素不被float(浮动)所影响,这个时候我们就需要用clear:both;来清除。

viewplaincopytoclipboardprint?

  1. <pstylepstyle="float:left;width:200px;">这个是第1列,p> 
  2. <pstylepstyle="float:left;width:400px;">这个是第2列,p> 
  3. <p>这个是第3列。p> 
  4.  
  5. <pstylepstyle="float:left;width:200px;">这个是第1列,p> 
  6. <pstylepstyle="float:left;width:400px;">这个是第2列,p> 
  7. <p>这个是第3列。p> 

 如果不用清除浮动,那么第3列文字就会和第1、2列文字在一起,所以我们在第3个这列加一个清除浮动clear:both;

通常,我们往往会将“清除浮动”单独定义一个CSS样式,如:

viewplaincopytoclipboardprint?

  1. .clear{  
  2. clear:both;  
  3. }  
  4.  
  5. .clear{  
  6. clear:both;  
  7. }  

然后使用

来专门进行“清除浮动”。

◆clear:both;可以终结在出现他之前的浮动

viewplaincopytoclipboardprint? 

  1. .demodiv{float:left;width:100px;height:50px;  
  2. background:red;margin:5px;}  
  3. .d{clear:both}  
  4. style> 
  5. <divclassdivclass="demodiv">1div> 
  6. <divclassdivclass="demodiv">2div> 
  7. <divclassdivclass="d">div> 
  8. <divclassdivclass="demodiv">3div> 
  9. <divclassdivclass="demodiv">4div> 
  10.  
  11. .demodiv{float:left;width:100px;height:50px;  
  12. background:red;margin:5px;}  
  13. .d{clear:both}  
  14. style> 
  15. <divclassdivclass="demodiv">1div> 
  16. <divclassdivclass="demodiv">2div> 
  17. <divclassdivclass="d">div> 
  18. <divclassdivclass="demodiv">3div> 
  19. <divclassdivclass="demodiv">4div> 
  20.  

 效果图如下:

#p#
clear:both参数说明

◆语法:clear:none|left|right|both

◆参数:

none:允许两边都可以有浮动对象
both:不允许有浮动对象
left:不允许左边有浮动对象
right:不允许右边有浮动对象

◆说明:该属性的值指出了不允许有浮动对象的边。请参阅float属性。对应的脚本特性为clear

主要是用在div套div的结构中。如果内div是浮动的,一般都需要clear浮动,不然的话内div会超出外div的框架

所用什么时候用clear:both;就很重要,一般我们在需要清除浮动的时候用到clear:both;不要轻意用到clear:both;因为它也有副伯用.

你在要浮动的两个div后再加一个div,并设置样式为clear:both,这样就可以了(因为clear:both说明这个div不允许左右有浮动元素,于是就往下跑,撑开了父div),如下:

viewplaincopytoclipboardprint? 

  1. <divstyledivstyle="float:left;..."> 
  2. abc  
  3. div> 
  4. <divstyledivstyle="float:left;..."> 
  5. abc  
  6. div> 
  7. <divstyledivstyle="clear:both">div> 
  8.  
  9. <divstyledivstyle="float:left;..."> 
  10. abc  
  11. div> 
  12. <divstyledivstyle="float:left;..."> 
  13. abc  
  14. div> 
  15. <divstyledivstyle="clear:both">div> 
  16.  

 你这种两列的情况,我一般都是两个都float:left。
 

【编辑推荐】

  1. ***实现CSS页面居中方法揭秘
  2. 八个困扰新手的DIV CSS网页布局问题
  3. 全面解析CSS优先级规则
  4. 专家推荐 10款优秀CSS框架
  5. 实例解析清除CSS float浮动的三种方法

 

 

责任编辑:佚名 来源: nowamagic.net
相关推荐

2020-12-18 05:40:37

CSS clearHtml

2013-03-28 11:02:26

CSS浮动

2023-05-15 08:34:36

css浮动

2010-09-02 09:14:35

CSS浮动

2017-02-06 09:40:35

浮动布局 浮动

2010-08-23 10:04:48

CSS浮动

2010-09-01 12:50:04

CSS清除浮动

2010-08-31 13:18:22

CSS浮动

2010-01-13 15:52:59

VB.NET浮动窗体

2010-09-08 15:16:46

clearCSS

2010-08-31 16:49:58

2010-10-09 17:11:16

病毒分析

2010-06-29 14:51:26

UML建模技术

2018-10-09 15:00:43

Hadoop分布式架构

2009-10-23 15:50:07

接入技术

2009-12-23 13:51:05

浮动静态路由技术

2011-08-11 18:13:07

clear中文man

2016-03-21 10:05:18

2015-04-15 13:19:52

Chinapub读书会

2021-12-01 15:03:56

Java开发代码
点赞
收藏

51CTO技术栈公众号