css浮动基本规则

说到元素的浮动,首先要明确一个概念就是包含块(containing block)。

所谓浮动元素的包含块,就是其最近的块级祖先元素。我们后面会举例来说明。

这里关于浮动的问题,主要有以下几点:

  • 1、浮动元素的外边界不能超过其包含块的内边界。
  • 2、浮动元素的外边界是另一浮动元素的外边界。
  • 3、浮动元素的顶端不能比之前出现的浮动元素的顶端更高。
  • 4、浮动元素顶端不超过当前行。
  • 5、clear属性。

1、浮动元素的外边界不能超过其包含块的内边界。

我们看以下代码:

    <div class="col-md-6" id="outerBlock">
                <img src="images/test1.jpg" style="float:right" alt="plane" id="image1"/>
                <p id="p1">Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.
                              One day in the Year of the Fox
                              Came a time remembered well,
                              WheOne day, in the Year of the Fox
                              Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.   </p>

            </div>

这段代码看第来比较长,实际上只有3个元素,最外层的div#outerBlock以及其中包含的img#image1p#p1

注意代码第2行,我们让img#image1元素向右浮动,此时,该img元素的包含块就为div#outerBlock,也就是最外层的div块。

这段代码会呈现什么效果呢?为了更加清晰地说明问题,我们这里分别给div#outerBlock以及p#p1加上边框以及内边距:

div#outerBlock {
    border: 1px dotted;
    padding: 3em;
}

p#p1 {
    border: 1px solid;
    padding: 3em;
}

效果如下:

这里我们可以清楚地看到div#outerBlockimg#image1以及p#p1三者的位置关系。

div#outerBlock是img元素的包含块,img元素的外边界不得超过其包含块的内边界。由于div#outerBlock元素增加了内边距padding=3em,故img元素的最右侧以及最上侧为虚线内3em。

如果我们给img元素加上2em的外边距,可以预料到img元素右侧及上侧将会为虚线内5em。如下:

img#image1 {
    margin: 2em;
}

虽然看起来img元素很像包含在实线围起来的p元素中,事实上它的位置时依据它的包含块——虚线确定的,只是给img元素增加了2em的外边距,根据浮动元素外边界不超过其包含块内边界的原则,图像的边界将距离虚线5em。

接下来,我们做一些改变,将img元素放到p#p1中去,如下:

<div class="col-md-6" id="outerBlock">
                <p id="p1">Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              <img src="images/test1.jpg" style="float:right" alt="plane" id="image1"/>
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.
                              One day in the Year of the Fox
                              Came a time remembered well,
                              WheOne day, in the Year of the Fox
                              Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.   </p>

            </div>

注意看第9行,将img元素插入到了p元素中间。此时,img元素的包含块就变成了p#p1,页面结果如下:

同样是浮动元素外边界不超过其包含块内边界的原则,此时图像的边距离实线距离为2em+3em=5em。此时的位置是根据实线确定的。

2、浮动元素的外边界是另一浮动元素的外边界。

关于这个问题,实际上是为了避免浮动元素彼此之间的覆盖。也就是说,如果一个向右浮动的元素,碰到了另一个浮动元素,那么它的外边界将是碰到的那个浮动元素的外边界。

看下面的例子,这里我们包含2个img元素在p块中:

            <div class="col-md-6" id="outerBlock">
                <p id="p1">Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              <img src="images/test1.jpg" style="float:right" alt="plane" id="image1"/>
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              <img src="images/test1.jpg" style="float:right" alt="plane" id="image2"/>
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.
                              One day in the Year of the Fox
                              Came a time remembered well,
                              WheOne day, in the Year of the Fox
                              Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.   </p>

            </div>

注意看代码的9行以及15行,这里我们插入了2个img元素img#image1img#image2。为了更清楚地展示以上准则,这里给img元素加上内边距以及边框:

img#image1 {
    padding: 2em;
    border: 1px solid;
}
img#image2 {
    padding: 2em;
    border: 1px solid;
}

得到的效果如下:

说明一下,这里插入内边距是为了使边框更加清晰地展示边界。实际上,正如上面说的,浮动元素的外边界是另一浮动元素的外边界。

这里还有一个细节问题,如果一个包含块中有2个浮动元素,并且这2个浮动元素的外边距宽度之和大于包含块的宽度,那么这2个浮动元素便只能上下排列,如下:

3、浮动元素的顶端不能比之前出现的浮动元素的顶端更高。

这句话看起来很抽象,具体来说,假如现在有3个浮动块。第2个块在第1个块的下面,那么第3个块的顶端就是第2个块的顶端,而不会一直浮动到它们的包含块的内边界。

请看下面的代码,我们插入3个浮动块:

            <div class="col-md-6" id="outerBlock">
                <img src="images/test3.jpg" style="float:left" alt="plane" id="image1"/>
                <img src="images/test4.jpg" style="float:left" alt="plane" id="image2"/>
                <img src="images/test1.jpg" style="float:right" alt="plane" id="image3"/>
                <p id="p1">Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.
                              One day in the Year of the Fox
                              Came a time remembered well,
                              WheOne day, in the Year of the Fox
                              Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.   </p>

            </div>

注意代码的2、3、4行,我们插入了3个浮动img块。

其中,img#image1向左浮动,img#image2向左浮动,img#image3向右浮动。前2个浮动块由于宽度之和超出了它们的包含块,它们将上下排列,第3个浮动img的上边界是第2个浮动img的上边界,而不是第1个!如下图所示:

如上图所示,第三个浮动块img3,也就是右侧的小飞机,它的上边界不会超过在它之前的浮动元素的上边界。也就是说,它不会一直浮动到和img1等高的位置,它的上边界不超过img2的上边界。

4、浮动元素顶端不超过当前行

这条规则同3的功能类似,作用在于约束浮动块的上浮。主要针对浮动元素之前出现另一元素的情况而言。其实上面我们也看到了,只是没有刻意地拿出来说。不妨举两种情况作为例子来说。

第一种,在一个段落里,出现一个浮动元素。

<div class="col-md-6" id="outerBlock">
                <p id="p1">Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              <img src="images/test1.jpg" style="float:right" alt="plane" id="image1"/>
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.
                              One day in the Year of the Fox
                              Came a time remembered well,
                              WheOne day, in the Year of the Fox
                              Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.   </p>

            </div>

注意以上代码的第9行,有一个浮动元素。第4条规则的意思就是说,浮动元素的顶端不会超过其上面To the temple of the king.所在的行。我们具体看以上代码的效果:

第二种情况,如果浮动元素出现在一个段落后面,那么该浮动元素上浮将不能超过该段落的底部。如下:

<div class="col-md-6" id="outerBlock">
                <p id="p1">Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.
                              One day in the Year of the Fox
                              Came a time remembered well,
                              WheOne day, in the Year of the Fox
                              Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.   </p>
                              <img src="images/test1.jpg" style="float:right" alt="plane" id="image1"/> 

            </div>

我们将浮动块放在代码37行,处于块级兄弟元素p的后面,它的上浮情况如何呢?

很清楚地看到,浮动元素的顶部在p元素的下方。虽然它的包含块是虚线构成的div元素。

5、clear属性

如果我们不希望我们的某段内容流过浮动元素,我们可以使用clear属性,我们下面对比2种情况来看:

            <div  class="col-md-3" id="outerBlock">
            <img src="images/test1.jpg" style="float:right" alt="plane" id="image1"/>
                <p id="p1">Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.
                              One day in the Year of the Fox
                              Came a time remembered well,
                              WheOne day, in the Year of the Fox
                              Came a time remembered well,
                              When the strong young man of the rising sun
                              Heard the tolling of the great black bell.
                              One day in the Year of the Fox,
                              When the bell began to ring,
                              It meant the time had come for one to go
                              To the temple of the king.
                              There in the middle of the circle he stands,
                              Searching, seeking.
                              With just one touch of his trembling hand,
                              The answer will be found.
                              Daylight waits while the old man sings,
                              "Heaven help me!"
                              And then like the rush of a thousand wings,
                              It shines upon the One.
                              And the day has just begun.   </p>

            </div>

同样的一段代码,右侧的p元素添加clear:right,这样浮动元素将不会出现在其右侧。该p块将会被向下推,直到浮动元素的下面才会显示。

时间: 2024-10-06 10:00:21

css浮动基本规则的相关文章

CSS浮动的3个特性(高手绕行)

1.  浮动元素会脱离正常的文档流,按照其外边距指定的位置相对于它的上一个块级元素(或父元素)显示: 代码示例: 1 <!DOCTYPE HTML > 2 <html> 3 <head> 4 <meta charset='utf-8'/> 5 <title>浮动元素的性质</title> 6 <style type="text/css"> 7 *{ 8 margin:0; 9 padding:0; 10

css浮动(folat),清除浮动(clear)

css浮动(float) float是css样式,用于设置标签的居左浮动和居右浮动,浮动后的元素不属于html文档流,需要用清除浮动把文档拽回到文档流中 浮动值: left:向左浮动 right:向右浮动 html文档流:自窗体自上而下分成一行一行,并在没行中从左到右的顺序排放元素. 通过例子来解释浮动 1.页面上有两个块元素,每个块元素独占一行 2.我们希望他们能并排显示,这是就可以使用float,我们先给红色的div加float:left,发现两个是在一行了,但是他们重叠在了一起. 3.这是

css浮动和清除浮动

浮动存在的原因 在word排版中,文本可以环绕图片.在css中,想要实现文字环绕的效果,只能借助于float,没有其他的替代方式.在实际中,文档的布局经常会使用浮动. float属性 float属性,默认为none,也就是标准流通常的情况.如果将float的属性的值设置为left或right,元素就会向其父元素的左侧或右侧紧靠. inherit表示从父元素继承float属性的值. clear属性 clear 属性定义了元素的哪边上不允许出现浮动元素.在 CSS1 和 CSS2 中,这是通过自动为

验分享:CSS浮动(float,clear)通俗讲解

经验分享:CSS浮动(float,clear)通俗讲解 很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能力差,也可能是没能遇到一篇通俗的教程. 前些天小菜终于搞懂了浮动的基本原理,迫不及待的分享给大家. 写在前面的话: 由于CSS内容比较多,小菜没有精力从头到尾讲一遍,只能有针对性的讲解. 如果读者理解CSS盒子模型,但对于浮动不理解,那么这篇文章可以帮助你. 小菜水平有限,本文仅仅是入门教程,不当之处请谅解! 本文以div元素布局为例. 教程开始: 首先要知道,div是块级元素

Css3之基础-8 Css 浮动(定位,浮动定位)

一.CSS 定位概述 定位概述 - 定位: 定义元素框相对于其正常位置应该出现的位置,或者相对于父元素.另一个元素甚至浏览器窗口本身的位置 - 普通流定位 - 浮动定位 - 相对定位 - 绝对定位 普通流定位 - 页面中的块级元素框从上到下一个接一个地排列 - 每一个块级元素都会出现在一个新行中(比如<p>元素.<div>元素) - 元素框之间的垂直距离是由框的垂直外边距计算出来的 - 内联元素将在一行中从左到右排列水平布置 - 不需要从新行开始 - 可以使用水平内边距.边框和外边

彻底理解浮动float CSS浮动详解 清除浮动的方法

我们把网页的常用的布局格式分为以下三种: 1.标准流. 所谓的标准流就是,行内元素自己单独一行,而块级元素是上下显示的. 以前我们学习的都是标准流.   注意:标准流使我们网页布局中最稳定的一种结构 2. 浮动流 使我们学习的脱离标准流的第一种方式.会影响我们标准流的排列.所以,我们布局的时候,能用标准流做的,就不用浮动做. 3. 定位流 定位流也是脱离标准流的一种模式.它完全脱离标准流,不会对标准流有影响. 浮动(float) 我们要浮动的目的: 我们浮动的目的,就是可以把多个块级元素放到想要

css浮动(float,clear)

1. 以div元素布局为例,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流,是指标准流中的div. 无论多么复杂的布局,其基本出发点均是:“如何在一行显示多个div元素”,显然标准流已经无法满足需求,这就要用到浮动. 浮动可以理解为让某个div元素脱离标准流,漂浮在标准流之上,和标准流不是一个层次. 2. 假如某个div元素A是浮动的,如果A元素上一个元素也是浮动的,那么A元素会跟随在上一个元素的后边(如果一行放不下这两个元素,那么A元素会被挤到下一行):如果A元素上一个元素

CSS浮动(float)属性学习经验分享

作为一名前端开发的初学者,CSS的布局定位无疑成为了一个难点,这两天通过看一些博客的技术分享和自己的反复实践,大概领悟到了一些float的“门道”. 下面就通过一些例子来归纳总结一下我所学到的浮动特性: (一)浮动元素对其兄弟元素是标准流元素的影响: 现在假定HTML文档中从上到下有3个块元素A.B.C 1.现设定A.C为标准流中的元素,B设为float:left (注:为了更直观地显示,设B的透明度为0.5,B是在C上方的) 由此可见:将B设为浮动元素后,B脱离了标准文档流,浮于其上方,因此他

CSS浮动与清除浮动(overflow)例子

在css中浮动与清除浮动功能是我们开发中常用到的一个功能了,下面小编来为各位分析关于CSS浮动与清除浮动(overflow)例子吧. float脱离文本流,可是为什么文字却会有环绕的效果,这点实在是神奇,于是乎就去问了师匠: Normal flow is the way that elements are displayed in a web page in most circumstances. All elements in HTML are inside boxes which are e