浮动 ( float css属性)

  float : left right

Elements are floated horizontally, this means that an element can only be floated left or right, not up or down.

http://www.w3schools.com/css/css_float.asp

浮动可以让元素在容器中尽量向左或者向右push移动, 这样其他元素会围绕浮动元素,

所谓的其他元素就是浮动元素后面的非浮动元素, 浮动影响的对象之后的元素,这个先后是指元素标签位置的先后次序;并不是展示出来的次序,由于浮动的存在,展示出来的次序,可以不是实际元素标签的顺序,例如 有这种代码 (div[float:left] div[float:right] div), 最后一个div的内容显示在 第一个 和 第二个div之间, 给人眼错觉好像是实际上的第三个div看起来是第二个。

The elements after the floating element will flow around it.

The elements before the floating element will not be affected.

浮动最初用作 图片周边 有文字围绕效果,后来普遍为布局使用。

清浮动需求

float元素实际上是脱离了HTML文档流,浮动元素的高度是不被外层容器元素计算的,

如果浮动元素仅仅被后续的元素围绕了高度的一半,这个时候添加一个新div块, 要求这个新块能够 在浮动元素的下方一行显示,

按照浮动的实现效果, 新添加的 div块为了实现围绕效果, 还是会从浮动元素的一半开始显示新添加的div。

很多布局情况,要求多个块能够按照float的push排列,同时又能够让float元素像正常的文档元素一样,可以在文档流中被计算高度。

要是实现这个效果,需要引入清浮动  clear。

清浮动理解

clear : left right both

清浮动,就是把之前元素的浮动状态删除,按照正常文档流元素高度来解析之前元素。

w3.org官方的解释是:「元素盒子的边不能和前面的浮动元素相邻」。

The clear property indicates which sides of an element's box(es) may not be adjacent to an earlier floating box.

http://zzstudy.offcn.com/archives/14575 这个解释的有点意思, float元素,可以理解为边框围绕, 则元素盒子的边必然与浮动元素相邻,清浮动,就是要破除这种相邻, 跟浮动元素不相邻,换行显示。

left -- 是指清浮动元素的 左边框 不能与前面的 浮动元素的边相邻。

right -- 是指清浮动元素的 右边框 不能与前面的 浮动元素的边相邻。

both -- 是指清浮动元素的 左边框 和 右边框 不能与前面的 浮动元素的边相邻。

注释: 这里的左右边是指 清浮动 元素 的 左右边框,  不是指 此元素标签位置 的 左边(前边) 和 右边(后边)

记住所有清浮动元素, 要清理的浮动属性, 是针对此元素标签位置前面的浮动标签元素。

上面说的不能相邻是总体上说法,但是怎样不能相邻,看w3c自己的即使最透彻:

http://www.w3.org/wiki/CSS/Properties/clear

  • left
    Requires that the top border edge of the box be
    below the bottom outer edge of any left-floating boxes that resulted
    from elements earlier in the source document.
  • right
    Requires that the top border edge of the box
    be below the bottom outer edge of any right-floating boxes that resulted
    from elements earlier in the source document.
    • both
      Requires that the top border edge of the box be
      below the bottom outer edge of any right-floating and left-floating
      boxes that resulted from elements earlier in the source document.

W3C清浮动例子

下面网站提供的一个清浮动,分两行显示图片,每一行都向左对齐。

http://www.w3schools.com/css/tryit.asp?filename=trycss_float_clear

<!DOCTYPE html>
<html>
<head>
<style>
.thumbnail {
float: left;
width: 110px;
height: 90px;
margin: 5px;
} .text_line {
clear: left;
margin-bottom: 2px;
}
</style>
</head>
<body> <h3>Image Gallery</h3>
<p>Try to resize the browser-window to see what happens when the images does not have enough room.</p>
<img class="thumbnail" src="klematis_small.jpg" width="107" height="90">
<img class="thumbnail" src="klematis2_small.jpg" width="107" height="80">
<img class="thumbnail" src="klematis3_small.jpg" width="116" height="90">
<img class="thumbnail" src="klematis4_small.jpg" width="120" height="90"> <h3 class="text_line">Second row</h3>
<img class="thumbnail" src="klematis_small.jpg" width="107" height="90">
<img class="thumbnail" src="klematis2_small.jpg" width="107" height="80">
<img class="thumbnail" src="klematis3_small.jpg" width="116" height="90">
<img class="thumbnail" src="klematis4_small.jpg" width="120" height="90">
<div>iiiiiii</div>
</body>
</html>
 

自研clear清左右例子

前面设置了两个浮动块div,左右浮动,  第三个div设置clear各种值(left right both none), 以观察各种情况下的清浮动情况。

同时为了能看出,清左 和 清右 的效果, 将第一个浮动div 设置高度为 100px, 第二个浮动div高度设置为 200px。

建议亲自运行下, 可容易看出 clear left 和 rigt 效果。

<html>
<head>
<!--<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>-->
<style>
.clear {
clear: both;
}
</style>
</head>
<body> <h1>left + left => clear none</h1>
<div style="float:left;width:200px;height:100px;background:green">这个是第1列,</div>
<div style="float:left;width:400px;height:200px;background:grey">这个是第2列,</div>
<div style="text-align:left; background:yellow; clear:none">这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。</div>
<div class="clear"></div> <h1>left + left => clear left</h1>
<div style="float:left;width:200px;height:100px;background:green">这个是第1列,</div>
<div style="float:left;width:400px;height:200px;background:grey">这个是第2列,</div>
<div style="text-align:left; background:yellow; clear:left">这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。</div>
<div class="clear"></div> <h1>rihgt + right => clear none</h1>
<div style="float:right;width:200px;height:100px;background:green">这个是第1列,</div>
<div style="float:right;width:400px;height:200px;background:grey">这个是第2列,</div>
<div style="text-align:left; background:yellow; clear:none">这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。</div>
<div class="clear"></div> <h1>rihgt + right => clear right</h1>
<div style="float:right;width:200px;height:100px;background:green">这个是第1列,</div>
<div style="float:right;width:400px;height:200px;background:grey">这个是第2列,</div>
<div style="text-align:left; background:yellow; clear:right">这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。</div>
<div class="clear"></div> <h1>left + right => clear none</h1>
<div style="float:left;width:200px;height:100px;background:green">这个是第1列,</div>
<div style="float:right;width:400px;height:200px;background:grey">这个是第2列,</div>
<div style="text-align:left; background:yellow; clear:none">这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。</div>
<div class="clear"></div> <h1>left + right => clear left</h1>
<div style="float:left;width:200px;height:100px;background:green">这个是第1列,</div>
<div style="float:right;width:400px;height:200px;background:grey">这个是第2列,</div>
<div style="text-align:left; background:yellow; clear:left">这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。</div>
<div class="clear"></div> <h1>left + right => clear right</h1>
<div style="float:left;width:200px;height:100px;background:green">这个是第1列,</div>
<div style="float:right;width:400px;height:200px;background:grey">这个是第2列,</div>
<div style="text-align:left; background:yellow; clear:right">这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。</div>
<div class="clear"></div> <h1>left + right => clear both</h1>
<div style="float:left;width:200px;height:100px;background:green">这个是第1列,</div>
<div style="float:right;width:400px;height:200px;background:grey">这个是第2列,</div>
<div style="text-align:left; background:yellow; clear:both">这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。这个是第3列。</div>
<div class="clear"></div>
</body>
</html>

CSS浮动与清浮动的更多相关文章

  1. 关于浮动与清浮动 float

    浮动常见的几种属性值 float {left;  right;  none;  } 主要是定义元素朝哪个方向浮动: 元素浮动后的特性 在一行显示,父级的宽度放不下,自己折行: 支持宽高等样式: 不设置 ...

  2. web前端浮动、清浮动问题

    浮动的问题如下一一列举如有考虑不周的欢迎欢迎大家补充: 1.浮动,兼容性问题3px的问题,双边距的问题 在平时工作的过程中,解决3px的问题,一般都是初始化*{margin:0;padding:0px ...

  3. day44--浮动和清浮动基本小结

    浮动和清浮动 一:浮动 浮动元素会生成一个块级框,而不论它本身是何种元素. 关于浮动的两个特点: 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 由于浮动框不在文档的普 ...

  4. web开发:清浮动

    一.display总结 二.overflow 三.浮动布局 四.清浮动 五.清浮动的方式 一.display总结 <!DOCTYPE html> <html> <head ...

  5. CSS清浮动处理(Clear与BFC)

    在CSS布局中float属性经常会被用到,但使用float属性后会使其在普通流中脱离父容器,让人很苦恼 1 浮动带来布局的便利,却也带来了新问题 <!doctype html> <h ...

  6. css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?一起来$('.float')

    一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class="outer">     <div class=&quo ...

  7. 【转】css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?

    摘要: css清除浮动float的三种方法总结,为什么清浮动?浮动会有那些影响?     一.抛一块问题砖(display: block)先看现象: 分析HTML代码结构: <div class ...

  8. css清浮动与动态计算元素宽度

    css常用清浮动写法 /*清除浮动*/| .clears:after{ display: block; content: ''; clear: both; height: ; visibility: ...

  9. CSS清浮动办法

    骨灰级解决办法: .clear{clear:both;height:0;overflow:hidden;} 上诉办法是在需要清除浮动的地方加个div.clear或者br.clear,我们知道这样能解决 ...

随机推荐

  1. Apache Spark技术实战之2 -- PackratParsers实例

    欢迎转载,转载请注明出处,徽沪一郎 概要 通过一个简明的Demo程序来说明如何使用scala中的PackratParsers DemoApp import scala.util.parsing.com ...

  2. DS实验题 Floyd最短路径 & Prim最小生成树

    题目: 提示: Floyd最短路径算法实现(未测试): // // main.cpp // Alg_Floyd_playgame // // Created by wasdns on 16/11/19 ...

  3. 当多个工程互相引用时,若有serverlet工程,提示java.lang.NoClassDefFoundError错误

    serverlet工程和其他的工程引用有所不同,直接在buildpath中添加引用的工程会报NoClassDefFoundError错误错误, 需要在properties-depoyment asse ...

  4. 会php不回缓存行吗?多重实现

    1.普遍缓存技术: 数据缓存:这里所说的数据缓存是指数据库查询PHP缓存机制,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中 ...

  5. 运行EFDC出现这样的错误:forrt1:severe<157>:Program Exception-access violation

    经过检查是由于TQSER出现读写错误,原来我的数据输入文件的时间是427天,后来延长到639,但其中有一个点的时间仍然维持在427.故此出现这个错误.EFDC是用Fortran编译的,通过debug才 ...

  6. spm完成dmp在windows系统上导入详细过程

    --查询dmp字符集 cat spmprd_20151030.dmp ','xxxx')) from dual; spm完成dmp在windows系统上导入详细过程 create tablespace ...

  7. Java中对象构造

    构造函数 作用:在构造对象的同时初始化对象.java强制要求对象 诞生同时被初始化,保证数据安全. 调用过程和机制:①申请内存,②执行构造函数的函数体,③返回对象的引用. 特点:与类同名,无返回类型, ...

  8. Solaris 10下Qt编译Oracle 10g驱动

    上回书讲到<Oracle 10g在Solaris 10中安装详解>,现在开始用Qt来编译下Oracle 10g驱动吧!这样就可以通过Qt程序联入Oracle数据库了! Oracle的环境变 ...

  9. Http响应code

    Http响应报文 HTTP响应也由三个部分组成,分别是:状态行.消息报头.响应正文. 其中,HTTP-Version表示服务器HTTP协议的版本:Status-Code表示服务器发回的响应状态代码:R ...

  10. CSS3新添加的选择器

    ---条件选择器:--- .ccc[cusid*= value] { backgroud-color:#0094ff; } //表示使用了class="ccc"元素自定义属性cus ...