flex 布局
flex 布局
启用flex: diplay:flex | inline-flex (块级 | 行内)
容器属性6个
1 2 3
| flex-direction justify-content, align-items, align-content, flex-wrap, flex-flow
|
项目属6个
1 2 3
| flex flex-grow, flex-shrink, flex-basis order, align-self
|
常用 一共4个属性flex-direction
, justify-content
, align-items
, flex
容器属性6个
1 2 3 4 5 6 7 8 9
| diplay:flex | inline-flex (块级 | 行内)
// 容器属性6个 flex-direction (主轴方向) justify-content (主轴对齐方式) align-items (交叉轴对齐方式) align-content (多根轴线在交叉轴上对齐方式)(属性类似align-items) flex-wrap (轴线上换行) flex-flow (是flex-direction 和 flex-wrap 的简写)
|
1-01)flex-direction
属性决定主轴的方向
1 2 3 4 5 6 7 8
|
.container{ flex-direction: row; }
|
1-02) justify-content
属性决定主轴对齐方式
1 2 3 4 5 6 7 8 9 10
|
.container{ justify-content: flex-start; }
|
1-03) align-items
属性决定在交叉轴对齐方式
1 2 3 4 5 6 7 8 9
|
.container{ align-items: stretch ; }
|
1-04) align-content
多根轴线在交叉轴上对齐方式
1 2 3 4 5 6 7 8 9 10 11 12 13
|
.container{ align-content: stretch ; }
|
1-05) flex-wrap
一条轴线方不下,轴线上换行
1 2 3 4 5 6 7
|
.container{ flex-wrap: nowrap; }
|
1-06) flex-flow
是 flex-direction 和 flex-wrap 的简写
1 2 3 4 5
|
.container{ flex-flow: row nowrap; }
|


项目属性6个
1 2 3 4 5 6
| flex (属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。) flex-grow (项目的放大比例) flex-shrink (项目的缩小比例) flex-basis order (项目的排列顺序) align-self
|
flex的三个属性
1 2
| flex:0 1 auto; flex: flex-grow flex-shrink flex-basic
|
flex
属性是 flex-grow
、flex-shrink
和 flex-basis
属性的简写,描述弹性项目的整体的伸缩性zi
自动分配空间,不超长
flex: 1;width: 0;
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
| <style> .wrapper { display: flex; align-items: center; justify-content: space-between; width: 300px; background: #eee8aa; padding: 4px; border-radius: 4px; border: 2px solid #808080; }
.right { flex-shrink: 0; margin-left: 8px; }
.left { display: flex; flex: 1; width: 0; }
.start,.end { background: #ff6b81; }
.center { margin: 0 8px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } </style> <div class="wrapper"> <div class="left"> <div class="start">左侧</div> <div class="center">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div> <div class="end">标题</div> </div> <div class="right">其他</div> </div>
|
Flex 布局教程:语法篇
使用 CSS 弹性盒子
对齐弹性容器中的弹性项目
flex