1156 字
6 分钟
布局详解
flex布局深入了解
1. flex布局
flex容器的属性
| 属性名称 | 属性含义 | 属性可能的值 |
|---|---|---|
flex-direction | 决定主轴的方向 | row(默认) 水平,起点在左端row-reverse水平,起点在右端column:垂直,起点在上沿column-reverse:垂直,起点在下沿 |
flex-wrap | 决定一条轴线放不下,如何换行 | Nowrap(默认) 不换行Wrap:换行,第一行在上面Wrap-reverse:换行,第一行在下面 |
flex-flow | 是上面两个属性的简写 | 默认值是 row nowrap |
justify-content | 定义项目在主轴上的对齐方式 | Flex-start(默认值)左对齐Flex-end 右对齐Center居中Space-between:两端对齐,项目之间的间隔都相等Space-around:每个项目之间的间隔相等,所以项目之间的间隔比项目与边框之间的间隔大一倍 |
align-items | 定义项目在交叉轴上如何对齐 | Flex-start交叉轴的起点对齐Flex-end交叉轴的终点对齐Center:交叉轴的中点对齐Baseline:项目的第一行文字的基线对齐Stretch (默认值)如果项目未设置高度或者设为auto,将占满整个容器的高度 |
align-content | 定义多跟轴线对齐方式,一条轴线该属性不起作用 | Flex-start: 与交叉轴的起点对齐Flex-end 与交叉轴的终点对齐Center:与交叉轴的中点对齐Space-between:与交叉轴的两端对齐,轴线之间的间隔平均分布Space-around:每根轴线之间的间隔都相等,所以轴线之间的间隔比轴线与边框之间的间隔大一倍 |
**flex容器下面项目的属性 **
| 属性名称 | 属性含义 | 属性可能的值 |
|---|---|---|
order | 定义项目的排列顺序,数值越小,排列越靠前 | 默认0 |
flex-grow | 定义项目的放大比例,如果存在剩余空间,不放大 | 默认0(如果所有项目的flex-grow属性为1,则等分剩余空间) |
flex-shrink | 定义项目的缩小比例 | 默认1 负值对该属性无效 |
flex-basis | 定义在分配多余空间之前,项目占据的主轴空间,浏览器根据这个属性来计算主轴是否有多余空间 | 默认auto,即项目本来大小 |
flex | 是上面三个的简写 | 默认值 0 1 auto 后两个值可选 |
align-self | 允许单个项目与其他项目不一样的对齐方式,可覆盖align-items属性 | 默认值auto 表示继承父元素的align-items属性,如果没有父元素,则等同于stretch |
2.1 常用的垂直居中
css
.container { display: flex; align-items: center; justify-content: center; height: 500px; background: #666; }
.box { width: 200px; height: 200px; background: #ff0; }Html
<div class="container"> <div class="box">hello 翁恺敏</div></div>效果图:

2.2 常用的ul,li布局横向等宽排列

Html
<ul class="container"> <li>翁</li> <li>恺</li> <li>敏</li> <li>你</li> <li>好</li> </ul>css
.container { width: 500px; display: flex; list-style: none; padding: 0; } .container >li{ flex: 1; height: 50px; } .container >li:nth-of-type(2n){ background: red } .container >li:nth-of-type(2n-1){ background: green }2.3 骰子布局详解


2.3.1 单项目
首先,只有左上角1个点的情况。Flex布局默认就是首行左对齐,所以一行代码就够了。

.box { display: flex;}设置项目的对齐方式,就能实现居中对齐和右对齐。

.box { display: flex; justify-content: center;}
.box { display: flex; justify-content: flex-end;}设置交叉轴对齐方式,可以垂直移动主轴。垂直居中

.box { display: flex; align-items: center;}
.box { display: flex; justify-content: center; align-items: center;}
.box { display: flex; justify-content: center; align-items: flex-end;}
.box { display: flex; justify-content: flex-end; align-items: flex-end;}2.3.2 双项目

.box { display: flex; justify-content: space-between;}
.box { display: flex; flex-direction: column; justify-content: space-between;}
.box { display: flex; flex-direction: column; justify-content: space-between; align-items: center;}
.box { display: flex; flex-direction: column; justify-content: space-between; align-items: center;}
.box { display: flex;}
.item:nth-child(2) { align-self: center;}
.box { display: flex; justify-content: space-between;}
.item:nth-child(2) { align-self: flex-end;}2.3.3 三项目

.box { display: flex;}
.item:nth-child(2) { align-self: center;}
.item:nth-child(3) { align-self: flex-end;}2.3.3 四项目

.box { display: flex; flex-wrap: wrap; justify-content: flex-end; align-content: space-between;}
HTML
<div class="box"> <div class="column"> <span class="item"></span> <span class="item"></span> </div> <div class="column"> <span class="item"></span> <span class="item"></span> </div></div>css
.box { display: flex; flex-wrap: wrap; align-content: space-between;}
.column { flex-basis: 100%; display: flex; justify-content: space-between;}2.3.4 六项目

.box { display: flex; flex-wrap: wrap; align-content: space-between;}
.box { display: flex; flex-direction: column; flex-wrap: wrap; align-content: space-between;}2.4 圣杯布局(常见)

html
<div class="container"> <header>Header</header> <div class="content"> <main>Main</main> <nav>Nav</nav> <aside>Aside</aside> </div> <footer>Footer</footer> </div>css
* { margin: 0; padding: 0; }
html, body { width: 100%; height: 100%; }
.container { display: flex; /* 垂直*/ flex-direction: column; width: 100%; /*视口被均分为100单位的vh 占据整个窗口*/ min-height: 100vh; }
header, footer { background: #999; /*放大缩小比例为0 占据垂直方向80px*/ flex: 0 0 80px; }
.content { display: flex; /*1 1 auto 后两个值省略*/ flex: 1; }
nav { /*默认 0 数值越小 排列越靠前*/ order: -1; flex: 0 0 80px; background: royalblue; }
aside { flex: 0 0 80px; background: aqua; }
main { flex: 1; background: green; }