多栏布局
1、column-count属性
写法:
-webkit-column-count: 栏目数;
-moz-column-count: 栏目数;
2、column-width属性:指定栏目的宽度
-webkit-column-width: 值;
-moz-column-width: 值;
缩小页面时,不会像column-count属性不变栏目数,会根据页面宽度显示恰当的栏目数
3、column-gap属性:指定栏目与栏目之间的距离
-webkit-column-gap: 值;
-moz-column-gap: 值;
4、column-rule属性:栏目与栏目之间的分割线
-webkit-column-rule: solid 2px color ;
-moz-column-rule: solid 2px color ;
与设置边框相似
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>CSS3之前的布局</title>
<style>
#div1{
width: 100%;
-webkit-column-count:8;
-moz-column-count:8;
/*-webkit-column-width:150px;*/
/*-moz-column-width:150px;*/
-webkit-column-gap:50px;
-moz-column-gap:50px;
-webkit-column-rule:solid 1px #909090;
-moz-column-rule:solid 1px #909090;
}
</style>
</head>
<body>
<div id="div1">column-width属性:指定栏目的宽度
column-gap属性:指定栏目与栏目之间的距离
column-rule属性:栏目与栏目之间的分割线
column-width属性:指定栏目的宽度
column-gap属性:指定栏目与栏目之间的距离
column-rule属性:栏目与栏目之间的分割线
column-width属性:指定栏目的宽度
column-gap属性:指定栏目与栏目之间的距离
column-rule属性:栏目与栏目之间的分割线
column-width属性:指定栏目的宽度
column-gap属性:指定栏目与栏目之间的距离
column-rule属性:栏目与栏目之间的分割线
column-width属性:指定栏目的宽度
column-gap属性:指定栏目与栏目之间的距离
column-rule属性:栏目与栏目之间的分割线
column-width属性:指定栏目的宽度
column-gap属性:指定栏目与栏目之间的距离
column-rule属性:栏目与栏目之间的分割线
column-width属性:指定栏目的宽度
column-gap属性:指定栏目与栏目之间的距离
column-rule属性:栏目与栏目之间的分割线
column-width属性:指定栏目的宽度
column-gap属性:指定栏目与栏目之间的距离
</div>
</body>
</html>
盒布局
1、使用float和position属性时的缺点
缺点:多栏目时,底部不能对齐
2、使用盒布局
display: -moz-box;
display: -webkit-box;
实例:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Float属性布局</title>
<style>
#centerboy{
width: 1000px;
margin: 0 auto;
}
div#left{
float: left;
width: 300px;
padding: 10px;
background-color: green;
}
div#right{
float: right;
width: 300px;
padding: 10px;
background-color: #ff39ca;
}
div#conter{
float: left;
width: 400px;
background-color: #0064ff;
}
</style>
</head>
<body>
<div id="centerboy">
<div id="left">
<h2>栏目列表</h2>
<ul>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
<li><a href="index.html">栏目名称</a> </li>
</ul>
</div>
<div id="conter">
<p> 使用盒布局
display: -moz-box;
display: -webkit-box;使用盒布局
display: -moz-box;
display: -webkit-box; 使用盒布局
display: -moz-box;
display: -webkit-box;使用盒布局
display: -moz-box;
display: -webkit-box;#### 使用盒布局
display: -moz-box;
display: -webkit-box;使用盒布局
display: -moz-box;
display: -webkit-box;使用盒布局
display: -moz-box;
display: -webkit-box;使用盒布局
display: -moz-box;
display: -webkit-box;</p>
</div>
<div id="right">
<h2>推荐文章</h2>
<ul>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
<li><a href="index.html">推荐文章</a> </li>
</ul>
</div>
</div>
</body>
</html>
出现上面这样的情况,可以使用box-sizing: border-box;
#centerboy{
width: 1000px;
margin: 0 auto;
display: -moz-box;
display: -webkit-box; /*使得三个div可以底部对齐*/
}
div#left{
/*float: left;*/
width: 300px;
padding: 10px;
background-color: green;
}
div#right{
/*float: right;*/
width: 300px;
padding: 10px;
background-color: #ff39ca;
}
div#conter{
/*float: left;*/
width: 400px;
background-color: #0064ff;
}
div#left,div#conter,div#right{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
显示效果:
弹性盒布局
1、适应窗口的弹性盒布局
box-flex属性,使得div随着浏览器窗口变化而变化
-webkit-box-flex: 1;
-moz-box-flex: 1;
2、改变元素的显示顺序
box-ordinal-group属性,可以实现改变元素的显示顺序,浏览器会根据序号的从小到大来显示这些元素
-webkit-box-ordinal-group: 序号;
-moz-box-ordinal-group:序号;
原本三个div的颜色分别是绿 蓝 红,改变序号后是红 绿 蓝。
3、改变元素的排列方向
-webkit-box-orient:值;
-moz-box-orient:值;
值:horizontal(盒子在水平方向上从左向右排列)/ vertical(盒子从上向下垂直排列)
vertical效果(这张图没有取消前两个div的宽度):
4、弹性盒布局的用处
1、在div里加入box-flex属性,可以消除容器内的空白
2、对多个元素使用box-flex属性,可以让容器中的元素的总宽度与总高度都等于容器的高度或者宽度
消除空白:
#centerboy{
display: -moz-box;
display: -webkit-box;
width: 500px;
height: 300px;
border: solid 2px #000000;
-webkit-box-orient: horizontal;
-moz-box-orient:horizontal; /*盒子水平*/
}
div#div1{
background-color: olivedrab;
}
div#div2{
background-color: #b224ef;
}
div#div3{
background-color: #f46d72;
}
div#div1,div#div2,div#div3{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
加入-webkit-box-flex: 1;
-moz-box-flex: 1;
#centerboy{
display: -moz-box;
display: -webkit-box;
width: 500px;
height: 300px;
border: solid 2px #000000;
-webkit-box-orient: horizontal;
-moz-box-orient:horizontal;
}
div#div1{
background-color: olivedrab;
}
div#div2{
background-color: #b224ef;
-webkit-box-flex: 1;
-moz-box-flex: 1; /*弹性盒布局*/
}
div#div3{
background-color: #f46d72;
}
div#div1,div#div2,div#div3{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
控制容器高宽度:
#centerboy{
display: -moz-box;
display: -webkit-box;
width: 500px;
height: 300px;
border: solid 2px #000000;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
}
div#div1{
background-color: olivedrab;
}
div#div2{
background-color: #b224ef;
}
div#div3{
background-color: #f46d72;
}
div#div1,div#div2,div#div3{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-flex: 1;
-moz-box-flex: 1; /*弹性盒布局*/
}
对各个元素进行box-flex属性指定,成比例分布:
div#div1{
background-color: olivedrab;
-webkit-box-flex: 1;
-moz-box-flex: 1;
}
div#div2{
background-color: #b224ef;
-webkit-box-flex: 2;
-moz-box-flex: 2;
}
div#div3{
background-color: #f46d72;
-webkit-box-flex: 3;
-moz-box-flex: 3;
}
div#div4{
background-color: #0e92b3;
-webkit-box-flex: 4;
-moz-box-flex: 4;
}