多列布局
类似报纸或杂志中的排版方式,上要用以控制大篇幅文本。用得不多。
格式举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| .wrapper{ -webkit-column-count: 3;
-webkit-column-rule: 1px dashed red;
-webkit-column-gap: 60px;
}
h4{ -webkit-column-span: all; text-align: center; }
|
备注:上面这几个属性涉及到兼容性问题,需要加私有前缀。
flex:伸缩布局
CSS3在布局方面做了非常大的改进,使得我们对块级元素的布局排列变得十分灵活,适应性非常强。其强大的伸缩性,在响应式开中可以发挥极大的作用。
如上图所示,有几个概念需要了解一下:
PS:主轴和侧轴并不是固定不变的,通过flex-direction可以互换。
设置伸缩布局的步骤
(1)指定一个盒子为伸缩布局:
(2)设置 flex-direction
属性来调整此盒的子元素的布局方式。默认的方向是水平方向。
(3)可互换主侧轴,也可改变主侧轴的方向。
各属性详解
**1、flex-direction
属性:**设置主轴方向。
flex-direction: row;
设置主轴方向,默认是水平方向。属性值可以是:
row
水平方向(默认值)
reverse-row
反转
column
垂直方向
reverse-column
反转列
代码演示:
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; list-style: none; } body{ background-color: #eee; font-family: "Microsoft Yahei"; font-size:22px; }
h3{ font-weight: normal; } section{ width: 1000px;
margin:40px auto; }
ul{ background-color: #fff; border: 1px solid #ccc;
}
ul li{ width: 200px; height: 200px; background-color: pink; margin:10px; } section:nth-child(1) ul{ overflow: hidden; } section:nth-child(1) ul li{ float: left; } section:nth-child(2) ul{ display: flex; }
section:nth-child(3) ul{ display: flex; flex-direction: row; }
section:nth-child(4) ul{ display: flex; flex-direction: row-reverse; }
section:nth-child(5) ul{ display: flex; flex-direction: column; }
section:nth-child(6) ul{ display: flex; flex-direction: column-reverse; } </style> </head> <body> <section> <h3>传统布局</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>伸缩布局 display:flex</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>主轴方向 flex-direction:row</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>主轴方向 flex-direction:row-reverse</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>主轴方向 flex-direction:column</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>主轴方向 flex-direction:column-reverse</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section> </body> </html>
|
2、justify-content:设置子元素在主轴上的对齐方式。
justify-content: flex-start;
设置子元素在主轴上的对齐方式。属性值可以是:
flex-start
从主轴的起点对齐(默认值)
flex-end
从主轴的终点对齐
center
居中对齐
space-around
在父盒子里平分
space-between
两端对齐 平分
代码演示:
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; list-style:none;} body{ background-color: #eee; font-family: "Microsoft Yahei";
} section{ width: 1000px; margin:50px auto; } section h3{ font-size:22px; font-weight: normal; }
ul{ border: 1px solid #999; background-color: #fff; display: flex;
}
ul li{ width: 200px; height: 200px; background: pink; margin:10px;
}
section:nth-child(1) ul{ justify-content: flex-start; }
section:nth-child(2) ul{ justify-content: flex-end; }
section:nth-child(3) ul{ justify-content: center; }
section:nth-child(4) ul{ justify-content: space-around;
}
section:nth-child(5) ul{ justify-content: space-between; } </style> </head> <body> <section> <h3>主轴的对齐方式:justify-content:flex-start</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>主轴的对齐方式:justify-content:flex-end</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>主轴的对齐方式:justify-content:center</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>主轴的对齐方式:justify-content:space-round</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>主轴的对齐方式:justify-content:space-bettwen</h3> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> </section> </body> </html>
|
3、align-items:设置子元素在侧轴上的对齐方式。
align-items:flex-start;
设置子元素在侧轴上的对齐方式。属性值可以是:
flex-start
从侧轴开始的方向对齐
flex-end
从侧轴结束的方向对齐
baseline
基线 默认同flex-start
center
中间对齐
stretch
拉伸
代码演示:
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; list-style:none; } body{ background-color: #eee; font-family: "Microsoft Yahei";
} section{ width: 1000px; margin:50px auto; } section h3{ font-size:22px; font-weight: normal; }
ul{ border: 1px solid #999; background-color: #fff; display: flex; height:500px;
}
ul li{ width: 200px; height: 200px; background: pink; margin:10px;
}
section:nth-child(1) ul{ align-items:flex-start; }
section:nth-child(2) ul{ align-items:flex-end; }
section:nth-child(3) ul{ align-items:center; }
section:nth-child(4) ul{ align-items:baseline; }
section:nth-child(5) ul{ align-items:stretch;
}
section:nth-child(5) ul li{ height:auto; }
</style> </head> <body> <section> <h3>侧轴的对齐方式:align-items :flex-start</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>侧轴的对齐方式:align-items:flex-end</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>侧轴的对齐方式:align-items:center</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>侧轴的对齐方式:align-itmes:baseline</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>侧轴的对齐方式:align-itmes: stretch</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section> </body> </html>
|
4、flex
属性:设置子盒子的权重
代码演示:
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; list-style:none; } body{ background-color: #eee; font-family: "Microsoft Yahei";
} section{ width: 1000px; margin:50px auto; } section h3{ font-size:22px; font-weight: normal; }
ul{ border: 1px solid #999; background-color: #fff; display: flex;
}
ul li{ width: 200px; height: 200px; background: pink; margin:10px;
}
section:nth-child(1) ul li:nth-child(1){ flex:1; }
section:nth-child(1) ul li:nth-child(2){ flex:1; }
section:nth-child(1) ul li:nth-child(3){ flex:8; }
section:nth-child(2) ul li:nth-child(1){
}
section:nth-child(2) ul li:nth-child(2){ flex:1; }
section:nth-child(2) ul li:nth-child(3){ flex:4; }
</style> </head> <body> <section> <h3>伸缩比例:flex</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
<section> <h3>伸缩比例:flex</h3> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </section>
</body> </html>
|