2017年8月16日 下午8:30
官方文档:CSS 定位
其实这个官方文档已经写得特别特别好,特别特别精炼了。
我在这里其实就是一些总结和补充
总结
一切皆为框
- div、h1 或 p 元素常常被称为块级元素。这意味着这些元素显示为一块内容,即“块框”。与之相反,span 和 strong 等元素称为“行内元素”,这是因为它们的内容显示在行中,即“行内框”。
- 您可以使用 display 属性改变生成的框的类型。这意味着,通过将 display 属性设置为 block,可以让行内元素(比如 元素)表现得像块级元素一样。还可以通过把 display 设置为 none,让生成的元素根本没有框。这样的话,该框及其所有内容就不再显示,不占用文档中的空间。
- 但是在一种情况下,即使没有进行显式定义,也会创建块级元素。这种情况发生在把一些文本添加到一个块级元素(比如 div)的开头。即使没有把这些文本定义为段落,它也会被当作段落对待:在这种情况下,这个框称为无名块框,因为它不与专门定义的元素相关联。
1
2
3
4<div>
some text
<p>Some more text.</p>
</div> - 块级元素的文本行也会发生类似的情况。假设有一个包含三行文本的段落。每行文本形成一个无名框。无法直接对无名块或行框应用样式,因为没有可以应用样式的地方(注意,行框和行内框是两个概念)。但是,这有助于理解在屏幕上看到的所有东西都形成某种框。
CSS 定位机制
- CSS 有三种基本的定位机制:普通流、浮动和绝对定位。
- 除非专门指定,否则所有框都在普通流中定位。也就是说,普通流中的元素的位置由元素在 (X)HTML 中的位置决定。
- 块级框从上到下一个接一个地排列,框之间的垂直距离是由框的垂直外边距计算出来。
- 行内框在一行中水平布置。可以使用水平内边距、边框和外边距调整它们的间距。但是,垂直内边距、边框和外边距不影响行内框的高度。由一行形成的水平框称为行框(Line Box),行框的高度总是足以容纳它包含的所有行内框。不过,设置行高可以增加这个框的高度。
CSS position 属性
- static
- 元素框正常生成。块级元素生成一个矩形框,作为文档流的一部分,行内元素则会创建一个或多个行框,置于其父元素中。
- relative
- 元素框偏移某个距离。元素仍保持其未定位前的形状,它原本所占的空间仍保留。
- absolute
- 元素框从文档流完全删除,并相对于其包含块定位。包含块可能是文档中的另一个元素或者是初始包含块。元素原先在正常文档流中所占的空间会关闭,就好像元素原来不存在一样。元素定位后生成一个块级框,而不论原来它在正常流中生成何种类型的框。
- fixed
- 元素框的表现类似于将 position 设置为 absolute,不过其包含块是视窗本身。
官方文档:CSS 浮动CSS clear 属性
总结
两条原则:
- 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
- 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。
行框和清理(补充)
注:文档中和我实验出来的效果不同
第一种方式(外部div)
- 使用最外部div使用float
总结:说明前面说的原则是不全的,加上同辈的标签之间是按边框是否碰见,父子类之间还是可以包含的1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.news {
background-color: gray;
border: solid 1px black;
float: left;
}
.news img {
float: left;
}
.news p {
float: right;
}
/*.clear {
clear: both;
}*/
</style>
</head>
<body>
<div class="news">
<img src="1.png" style="width: 300px" />
<p>some textsome textsome textsome textsome textsome textsome textsome </p>
<!-- <div class="clear"></div> -->
</div>
</body>
</html>
第二种方式(添加多余div块)
- 左右都float,但大的div不浮动,也没有clear
%E6%B5%AE%E5%8A%A8%20%E6%80%BB%E7%BB%93/8B0570E8-3146-468E-99D8-E3F5A965C362.png)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.news {
background-color: gray;
border: solid 1px black;
/*float: left;*/
}
.news img {
float: left;
}
.news p {
float: right;
}
/*.clear {
clear: both;
}*/
</style>
</head>
<body>
<div class="news">
<img src="1.png" style="width: 300px" />
<p>some textsome textsome textsome textsome textsome textsome textsome </p>
<!--<div class="clear" ></div>-->
</div>
</body>
</html>
有内部div,但内部div 不clear(内部div比img长)
%E6%B5%AE%E5%8A%A8%20%E6%80%BB%E7%BB%93/5EFE1638-487A-4687-9AF0-3AF7624DE87B.png)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.news {
background-color: gray;
border: solid 1px black;
/*float: left;*/
}
.news img {
float: left;
}
.news p {
float: right;
}
/*.clear {
clear: both;
}*/
</style>
</head>
<body>
<div class="news">
<img src="1.png" style="width: 300px" />
<p>some textsome textsome textsome textsome textsome textsome textsome </p>
<div class="clear" style="background: red;height: 20px;width: 350px"></div>
</div>
</body>
</html>有内部div,但内部div 不clear(内部div比img短,被img挡住了)
%E6%B5%AE%E5%8A%A8%20%E6%80%BB%E7%BB%93/819C4DAA-5E3B-422C-9379-9431E94E9906.png)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.news {
background-color: gray;
border: solid 1px black;
/*float: left;*/
}
.news img {
float: left;
}
.news p {
float: right;
}
/*.clear {
clear: both;
}*/
</style>
</head>
<body>
<div class="news">
<img src="1.png" style="width: 300px" />
<p>some textsome textsome textsome textsome textsome textsome textsome </p>
<div class="clear" style="background: red;height: 20px;width: 250px"></div>
</div>
</body>
</html>
- 加上一个内部div,并且把内部div clear
%E6%B5%AE%E5%8A%A8%20%E6%80%BB%E7%BB%93/EAEB0E88-FBE0-441A-B6F4-70265C88F515.png)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
.news {
background-color: gray;
border: solid 1px black;
/*float: left;*/
}
.news img {
float: left;
}
.news p {
float: right;
}
.clear {
clear: both;
}
</style>
</head>
<body>
<div class="news">
<img src="1.png" style="width: 300px" />
<p>some textsome textsome textsome textsome textsome textsome textsome </p>
<div class="clear" style="background: red;height: 20px;width:250px"></div>
</div>
</body>
</html>
对比1-4:
- 说明clear不仅仅让左右两边没有浮动体,而且要求表面上面也不能有浮动体
- 左右不能有浮动体的例子W3School在线测试工具 V2