块级元素
和行内元素
,若是想让一些元素既要有块级元素的特色也同时保留行内元素特色,只能让这些元素脱离标准文档流便可。float
属性来实现的。float
属性值说明表:属性值 | 描述 |
---|---|
left | 设置元素向左浮动。 |
right | 设置元素向右浮动。 |
class
属性值为.box1
元素设置为右浮动。代码块html
<!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> .box{ width: 600px; border: 1px solid #000; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
结果图布局
div
标签中尚未内容呢,如今咱们将子div
标签设置宽高度为100px
像素而且添加背景颜色。代码块ui
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
结果图spa
div
标签都是块级元素。class
属性值为.box1
的元素设置为右浮动。代码块code
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:right; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
结果图htm
注意:如今咱们发现
calss
属性值为.box
元素高度变矮了,这就说明了(浮动元素它已经脱离了标准文档流,再也不占用空间了)、而且向右浮动,浮动到自身的父元素的边缘位置就中止了浮动。blog
class
属性值为.box1
元素设置为左浮动。代码块文档
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
结果图it
class
属性值为.box2
元素看不见的缘由。结果图Atable
结果图B
class
属性值为.box
是一个池塘,3
个子元素都是可以漂浮在池塘水面上的东西,如今咱们将calss
属性值为.box1
元素浮动起来,漂在池塘水面上,是否是就再也不占用池塘内的空间了。class
属性值为.box2
元素看不见,并不表明它不存在只是被class
属性值为.box1
元素给遮挡住了,如今咱们将class
属性值为.box2
元素宽度设置为150px
像素。代码块
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
结果图
注意:事实证实
class
属性值为.box2
元素是存在的。
calss
属性值为.box2
元素设置为左浮动看看有什么不同的效果代码块
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
结果图
class
属性值为.box2
的元素左浮动并无左浮动到自己父元素的边缘位置,为何在class
属性值为.box1
后面呢?由于父元素已经有了浮动的子元素后面的子元素在浮动就浮动到前面浮动的元素以后。class
属性值为.box3
的元素设置为左浮动,看看有什么不同的效果。代码块
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float:left; } .box2{ width: 150px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> </body> </html>
结果图
注意:浮动元素浮动之后,其父元素再也不将浮动的子元素包裹在父元素以内,因此结果图出现一条黑色的边框线,如有不明白的看第一个实践内容。
div
标签中的span
标签设置为左浮动。span
标签设置宽高度和背景颜色有什么效果。代码块
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <span class="box1">微笑是最初的信仰1</span> <span class="box2">微笑是最初的信仰2</span> <span class="box3">微笑是最初的信仰3</span> </div> </body> </html>
结果图
span
标签设置了宽高度为100px
像素并无生效,由于如今span
标签仍是行内元素。span
标签设置左浮动,而后咱们在看看效果如何。代码块
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <span class="box1">微笑是最初的信仰1</span> <span class="box2">微笑是最初的信仰2</span> <span class="box3">微笑是最初的信仰</span> </div> </body> </html>
结果图
注意:行内元素设置为浮动以后就拥有了块级元素的特色。
class
属性值为.box
元素的子类元素没有浮动前的效果实践。代码块
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; } .box2{ width: 100px; height: 100px; background-color: #0f0; } .box3{ width: 100px; height: 100px; background-color: #00f; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> <h1>清除浮动</h1> </body> </html>
结果图
class
属性值为.box
元素的子元素左浮动以后影响到下面的元素排版布局实践。代码块
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> <h1>清除浮动</h1> </body> </html>
结果图
如今你们应该明白了为何要清除浮动了,有浮动就必须清除浮动,由于上面的元素设置了浮动就会影响到下面元素排版布局。
600px
像素的高度,一块儿来看看效果如何。代码块
<!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> .box{ width: 600px; height: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </div> <h1>清除浮动</h1> </body> </html>
结果图
这样是解决了下面元素排版布局问题,可是笔者不推荐这么作,由于高度是由子元素的内容撑起来的高度,而不是咱们给的固定高度。
CSS
中也有清除浮动的属性,清除浮动属性名为clear
。clear
属性值说明表属性值 | 描述 |
---|---|
left | 清除左侧浮动元素。 |
right | 清除右侧浮动元素。 |
both | 清除左右侧浮动元素。 |
clear
属性必须建立一个新的div元素,建立新的div
元素不能放置任何内容,它只能作一个件事情,那就是清除浮动而且将这个新建立的div
元素放在最后一个浮动元素的后面才会生效。both
属性值就能够了,左右清除浮动,干吗还要计较它是左浮动或右浮动呢,直接清除左右浮动就ok
了。代码块
<!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> .box{ width: 600px; border: 1px solid #000; } .box1{ width: 100px; height: 100px; background-color: #f00; float: left; } .box2{ width: 100px; height: 100px; background-color: #0f0; float: left; } .box3{ width: 100px; height: 100px; background-color: #00f; float: left; } .clear{ clear: both; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="clear"></div> </div> <h1>清除浮动</h1> </body> </html>
结果图
注意:这才是咱们真正想要的结果,而且从视觉上来看浮动的元素包裹在父元素以内的效果。
overflow
而且属性值为hidden
来清除浮动,必须将这个属性设置在浮动元素的父元素身上。给你们普及下属性为overflow
而且属性值为hidden
,它原意是用来将溢出的部份内容进行隐藏,可是它还能够清除浮动。
代码块
<!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{ width: 100px; height: 50px; border: 1px solid #000; } </style> </head> <body> <div> 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 </div> </body> </html>
结果图
代码块
<!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{ width: 100px; height: 50px; border: 1px solid #000; overflow: hidden; } </style> </head> <body> <div> 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 微笑是最初的信仰,微笑是最初的信仰,微笑是最初的信仰。 </div> </body> </html>
结果图
overflow
而且属性值为hidden
来清除浮动。代码块
<!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> ul{ list-style: none; } ul li{ float: left; border: 1px solid red; } </style> </head> <body> <ul> <li>微笑是最初的信仰1</li> <li>微笑是最初的信仰2</li> <li>微笑是最初的信仰3</li> <li>微笑是最初的信仰4</li> <li>微笑是最初的信仰5</li> <li>微笑是最初的信仰6</li> <li>微笑是最初的信仰7</li> <li>微笑是最初的信仰8</li> </ul> </body> </html>
结果图
注意:在这里笔者尚未给浮动元素清除浮动呢,你们能够明显的看到
ul
标签高度为0
。
代码块
<!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> ul{ list-style: none; overflow: hidden; } ul li{ float: left; border: 1px solid red; } </style> </head> <body> <ul> <li>微笑是最初的信仰1</li> <li>微笑是最初的信仰2</li> <li>微笑是最初的信仰3</li> <li>微笑是最初的信仰4</li> <li>微笑是最初的信仰5</li> <li>微笑是最初的信仰6</li> <li>微笑是最初的信仰7</li> <li>微笑是最初的信仰8</li> </ul> </body> </html>
结果图
如今咱们很清楚的看到
ul
标签高度为23px
像素,为何要使用:属性为overflow
而且属性值为hidden
来清除浮动,由于ul
标签中只能使用li
标签元素不能使用其它元素,因此属性为overflow
而且属性值为hidden
来清除浮动是最好不过啦。