浮动
传统网页布局的三种方式
网页布局的本质:用 CSS 来摆放盒子,把盒子摆放到相应位置。
CSS 提供了三种传统布局方式(简单说就是盒子如何进行排列)。
这里指的只是传统布局,其实还有一些特殊高级的布局方式。
标准流(普通流/文档流)
所谓的标准流:就是标签按照规定好的默认方式排列。
- 块级元素会独占一行,从上向下顺序排列。
- 行内元素会按照顺序,从左到右顺序排列,碰到父元素边缘则自动换行。
以上都是标准流布局,我们前面学习的就是标准流,标准流是最基本的布局方式。
这三种布局方式都是用来摆放盒子的,盒子摆放到合适位置,布局自然就完成了。
注意: 实际开发中,一个页面基本都包含了这三种布局方式(后面移动端学习新的布局方式) 。
为什么需要浮动?
提问:我们用标准流能很方便的实现如下效果吗?
- 如何让多个块级盒子(div)水平排列成一行?

比较难,虽然转换为行内块元素可以实现一行显示,但是他们之间会有大的空白缝隙,很难控制。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <head> <title>行内块中间有缝隙</title> <style> div { width: 150px; height: 200px; background-color: #d87093; display: inline-block; } </style> </head>
<body> <div>1</div> <div>2</div> <div>3</div> </body>
|

- 如何实现两个盒子的左右对齐?

总结: 有很多的布局效果,标准流没有办法完成,此时就可以利用浮动完成布局。 因为浮动可以改变元素标签默认的排列方式。
浮动最典型的应用:可以让多个块级元素一行内排列显示。
网页布局第一准则:多个块级元素纵向排列找标准流,多个块级元素横向排列找浮动!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <head> <style> div { float: left; width: 150px; height: 200px; background-color: #d87093; } </style> </head>
<body> <div>1</div> <div>2</div> <div>3</div> </body>
|

拓展: 浮动的盒子不会发生外边距合并!
什么是浮动?
通过浮动可以使一个元素向其父元素的左侧或右侧移动
注意
语法:
属性 |
描述 |
none |
元素不浮动(默认值) |
left |
元素向左浮动 |
right |
元素向右浮动 |
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
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>什么是浮动</title> <style> .left, .right { float: left; width: 200px; height: 200px; background-color: pink; } </style> </head>
<body> <div class="left">左青龙</div> <div class="right">右白虎</div> </body>
</html>
|

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
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>什么是浮动</title> <style> .left, .right { float: left; width: 200px; height: 200px; background-color: pink; }
.right { float: right; } </style> </head>
<body> <div class="left">左青龙</div> <div class="right">右白虎</div> </body>
</html>
|

浮动的特点
-
浮动元素会脱离标准流(脱标),不再占据文档流中的位置
- 脱离标准普通流的控制(浮) 移动到指定位置(动),(俗称脱标)
- 浮动的盒子不再保留原先的位置

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
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动特性1</title> <style>
.box1 { float: left; width: 200px; height: 200px; background-color: pink; }
.box2 { width: 300px; height: 300px; background-color: gray; } </style> </head>
<body> <div class="box1">浮动的盒子</div> <div class="box2">标准流的盒子</div> </body>
</html>
|

-
设置浮动以后,元素会向父元素的左侧或右侧移动
-
浮动元素默认不会从父元素中移出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <style> .box1 { width: 100px; height: 100px; background-color: orange; float: left; }
.box2 { width: 200px; height: 200px; background-color: red; } </style>
<div class="box1"></div> <div class="box2"></div>
|

- 浮动元素向左或向右移动时,不会超过前边的浮动元素(先来后到的顺序)
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
| <style> .box1 { width: 200px; height: 200px; background-color: orange; float: left; }
.box2 { width: 200px; height: 200px; background-color: red; float: left; }
.box3 { width: 200px; height: 200px; background-color: yellow; float: left; } </style>
<div class="box1"></div> <div class="box2"></div> <div class="box3"></div>
|

- 浮动元素不会超过上边的浮动的兄弟元素,最多就是和它一样高
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
| <style> .box1 { width: 300px; height: 300px; background-color: orange; float: left; }
.box2 { width: 400px; height: 400px; background-color: red; float: left; }
.box3 { width: 300px; height: 300px; background-color: yellow; float: right; } </style>
<div class="box1"></div> <div class="box2"></div> <div class="box3"></div>
|

- 如果浮动元素的上边是一个没有浮动的块元素,则浮动元素无法上移
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <style> .box1 { width: 200px; height: 200px; background-color: orange; }
.box2 { width: 200px; height: 200px; background-color: red; float: left; } </style>
<div class="box1"></div> <div class="box2"></div>
|

-
浮动元素不会盖住文字,文字会自动环绕在浮动元素的周围,所以我们可以利用浮动来设置文字环绕图片的效果

-
浮动的元素会一行内显示并且元素顶部对齐
- 如果多个盒子都设置了浮动,则它们会按照属性值一行内显示并且顶端对齐排列。
- 浮动的元素是互相贴靠在一起的(不会有缝隙),如果父级宽度装不下这些浮动的盒子,多出的盒子会另起一行对齐。
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
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动元素特性-浮动元素一行显示</title> <style> div { float: left; width: 200px; height: 200px; background-color: pink; }
.two { background-color: skyblue; height: 249px; }
.four { background-color: skyblue; } </style> </head>
<body> <div>1</div> <div class="two">2</div> <div>3</div> <div class="four">4</div> </body>
</html>
|



- 浮动的元素会具有行内块元素的特性
任何元素都可以浮动。不管原先是什么模式的元素,添加浮动之后具有行内块元素相似的特性。
- 块级盒子:没有设置宽度时默认宽度和父级一样宽,但是添加浮动后,它的大小根据内容来决定
- 行内盒子:宽度默认和内容一样宽,直接设置高宽无效,但是添加浮动后,它的大小可以直接设置
- 浮动的盒子中间是没有缝隙的,是紧挨着一起的
- 即:默认宽度由内容决定,同时支持指定高宽,盒子之间无空隙
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 36
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动的元素具有行内块元素特点</title> <style> span, div { float: left; width: 200px; height: 100px; background-color: pink; }
p { float: right; height: 200px; background-color: skyblue; } </style> </head>
<body> <span>span1</span> <span>span2</span>
<div>div</div> <p>pppppppppppppp</p> </body>
</html>
|

注意:之所以顶部没有对齐,原因是 p 标签自带的外边距 > span div 自带的外边距。
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 36 37 38 39 40
| <!doctype html> <html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>浮动的元素具有行内块元素特点</title> <style> * { margin: 0px; }
span, div { float: left; width: 200px; height: 100px; background-color: pink; }
p { float: right; height: 200px; background-color: skyblue; } </style> </head>
<body> <span>span1</span> <span>span2</span>
<div>div</div> <p>pppppppppppppp</p> </body>
</html>
|

简单总结:
脱离文档流的特点
块元素:
1 2 3 4 5 6 7 8
| <style> .box1 { background-color: orange; } </style>
<div class="box1">hello</div>
|

行内元素:
1 2 3 4 5 6 7 8 9 10
| <style> span { width: 200px; height: 200px; background-color: orange; float: left; } </style>
<span>I am a Span</span>
|

脱离文档流之后的特点很像行内块元素,不过存在一些差异
1 2 3 4 5 6 7 8 9 10 11 12
| <style> span { width: 200px; height: 200px; background-color: orange; float: left; } </style>
<span>I am a Span</span> <span>I am a Span</span>
|

盒模型
常见网页布局