HTML5中div布局的float属性
2017-04-03 14:49
316 查看
今天在看div布局的时候讲到了利用float属性来实现元素的浮动,一开始搞得不是很明白,现总结如下:
无论如何,div是一种块元素,每个元素铁定会占一行,无论当前行是否已经用完了,也就是所谓的“流”的概念
例如:
<div id="container">
<div id="b1">1号块</div>
<div id="b2">2号块</div>
<div id="b3">3号块</div>
<div id="b4">4号块</div>
</div>
样式:
div#container{
background-color: grey;
width: 100%;
height: 200px;
}
div#b1{
width: 50px;
height: 50px;
background-color: red;
}
div#b2{
width: 50px;
height: 50px;
background-color: aquamarine;
}
div#b3{
width: 50px;
height: 50px;
background-color: blueviolet;
}
div#b4{
width: 50px;
height: 50px;
background-color: yellow;
}
结果如下:
如果一个div块被设为浮动的话,它将脱离这个流,自主地去填充这个区域,去靠到最左边或者最右边
比如讲2号块设为向左浮动,为了醒目,将3号块的长度变长了
div#container{
margin: 0px;
background-color: grey;
width: 100%;
height: 500px;
}
div#b1{
width: 50px;
height: 50px;
background-color: red;
}
div#b2{
width: 50px;
height: 150px;
background-color: aquamarine;
float: left;
}
div#b3{
width: 50px;
height: 250px;
background-color: blueviolet;
}
div#b4{
width: 50px;
height: 50px;
background-color: yellow;
}
效果如下:
如果将连续的两个块设为浮动的话,他们都将和流中的上一个元素的下边沿对齐,并且靠后的那个浮动元素(假设两个浮动元素都是向左浮动的)会靠在前面那个元素的左边。如果空间不够,则会被挤向下一行。
比如:
div#container{
margin: 0px;
background-color: grey;
width: 100%;
height: 500px;
}
div#b1{
width: 50px;
height: 50px;
background-color: red;
}
div#b2{
width: 50px;
height: 150px;
background-color: aquamarine;
float: left;
}
div#b3{
width: 50px;
height: 250px;
background-color: blueviolet;
float: left;
}
div#b4{
width: 50px;
height: 250px;
background-color: yellow;
}
效果图如下(为了不被挡住,将4号块的长度伸长为250Px):
当然,也可以向右浮动。这样的话,排在前面的元素会更靠右,后面的元素会更靠左。
还有一点要注意,就是元素的id必须以英文字母开头,否则识别不到。
无论如何,div是一种块元素,每个元素铁定会占一行,无论当前行是否已经用完了,也就是所谓的“流”的概念
例如:
<div id="container">
<div id="b1">1号块</div>
<div id="b2">2号块</div>
<div id="b3">3号块</div>
<div id="b4">4号块</div>
</div>
样式:
div#container{
background-color: grey;
width: 100%;
height: 200px;
}
div#b1{
width: 50px;
height: 50px;
background-color: red;
}
div#b2{
width: 50px;
height: 50px;
background-color: aquamarine;
}
div#b3{
width: 50px;
height: 50px;
background-color: blueviolet;
}
div#b4{
width: 50px;
height: 50px;
background-color: yellow;
}
结果如下:
如果一个div块被设为浮动的话,它将脱离这个流,自主地去填充这个区域,去靠到最左边或者最右边
比如讲2号块设为向左浮动,为了醒目,将3号块的长度变长了
div#container{
margin: 0px;
background-color: grey;
width: 100%;
height: 500px;
}
div#b1{
width: 50px;
height: 50px;
background-color: red;
}
div#b2{
width: 50px;
height: 150px;
background-color: aquamarine;
float: left;
}
div#b3{
width: 50px;
height: 250px;
background-color: blueviolet;
}
div#b4{
width: 50px;
height: 50px;
background-color: yellow;
}
效果如下:
如果将连续的两个块设为浮动的话,他们都将和流中的上一个元素的下边沿对齐,并且靠后的那个浮动元素(假设两个浮动元素都是向左浮动的)会靠在前面那个元素的左边。如果空间不够,则会被挤向下一行。
比如:
div#container{
margin: 0px;
background-color: grey;
width: 100%;
height: 500px;
}
div#b1{
width: 50px;
height: 50px;
background-color: red;
}
div#b2{
width: 50px;
height: 150px;
background-color: aquamarine;
float: left;
}
div#b3{
width: 50px;
height: 250px;
background-color: blueviolet;
float: left;
}
div#b4{
width: 50px;
height: 250px;
background-color: yellow;
}
效果图如下(为了不被挡住,将4号块的长度伸长为250Px):
当然,也可以向右浮动。这样的话,排在前面的元素会更靠右,后面的元素会更靠左。
还有一点要注意,就是元素的id必须以英文字母开头,否则识别不到。
相关文章推荐
- 利用div进行页面的布局1(float&clear属性)
- div+css网页布局教程(1):float属性介绍
- HTML>精通CSS DIV网页样式与布局>理解CSS定位和DIV布局>float属性的自我理解
- 像table一样布局div的CSS属性详解
- HTML5培训第10节课堂笔记(盒子模型、行内与块级、float、定位、html5布局)
- (转)DIV入门之流式布局:float,clear <等>
- html5-div布局
- CSS 布局属性(display,float,clear,visibility,overflow,overflow-x,overflow-y)
- 学习div float 在ff和ie下的布局区别
- div+css中设置了float属性后如何让外层的高度随着内层的高度大小自动调整
- 如何终止 DIV 的 float 属性
- float 左右布局,左右div等高
- Div+Css布局相关的属性
- html5 div布局与table布局详解
- CSS布局 ——从display,position, float属性谈起
- CSS布局 ——从display,position, float属性谈起
- div+css中设置了float属性后如何让外层的高度随着内层的高度大小自动调整
- FireFox网页布局时候内容需自动换行时float属性不起作用
- (转)DIV+CSS布局中常用到的属性、参数及说明