Flex 布局 新手学习总结
Flex 布局
采用Flex布局的元素,称为Flex容器(flex container),简称”容器”。它的所有子元素自动成为容器成员,称为Flex项目(flex item),简称”项目”
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start
,结束位置叫做main end
;交叉轴的开始位置叫做cross start
,结束位置叫做cross end
。
项目默认沿主轴排列。单个项目占据的主轴空间叫做main size
,占据的交叉轴空间叫做cross size
。
一、Flex 布局之容器的六大属性:
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content
1.flex-direction属性
flex-direction属性决定主轴的方向(即项目的排列方向)。
它可能有4个值
.box { flex-direction: row | row-reverse | column | column-reverse; } 字段含义: row(默认值):主轴为水平方向,起点在左端。 row-reverse:主轴为水平方向,起点在右端。 column:主轴为垂直方向,起点在上沿。 column-reverse:主轴为垂直方向,起点在下沿。
*以下假设主轴为从左到右(row)
2.flex-wrap属性
flex-wrap属性如果一行排不下进行换行展示
它可能取3个值。
.box{ flex-wrap: nowrap | wrap | wrap-reverse; } 字段含义: nowrap(默认):不换行。 wrap:换行,第一行在上方。 wrap-reverse:换行,第一行在下方。
3.flex-flow属性
lex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
.box { flex-flow: <flex-direction> <flex-wrap>; }
4.justify-content属性(常用)
justify-content属性定义了项目在主轴上的对齐方式。
它可能有5个值
.box { justify-content: flex-start | flex-end | center | space-between | space-around; } 字段含义: flex-start(默认值):左对齐 flex-end:右对齐 center: 居中 space-between:两端对齐,项目之间的间隔都相等。 space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
5.align-items属性(常用)
align-items属性定义项目在交叉轴上如何对齐。
它可能有5个值
.box { align-items: flex-start | flex-end | center | baseline | stretch; }
字段含义: flex-start:交叉轴的起点对齐。 flex-end:交叉轴的终点对齐。 center:交叉轴的中点对齐。 baseline: 项目的第一行文字的基线对齐。 stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
6.align-content属性
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
该属性可能取6个值。
.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; }
字段含义: flex-start:与交叉轴的起点对齐。 flex-end:与交叉轴的终点对齐。 center:与交叉轴的中点对齐。 space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。 space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。 stretch(默认值):轴线占满整个交叉轴。
二、Flex 布局之项目的六大属性:
order
flex-grow
flex-shrink
flex-basis
flex
align-self
1.order属性
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
.box{ display: flex; border: 1px solid #000077; width: 400px; height: 100px; justify-content: center; align-items: center } .box div{ width: 50px; height: 50px; border: 1px solid #A52A2A; margin: 0 10px; text-align: center; }
<div class="box"> <div class="li" style="order:1">1</div> <div class="li" style="order:2">2</div> <div class="li" style="order:4">4</div>
<div class="li" style="order:3">3</div>
</div>
2.flex-grow属性
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
.box{ display: flex; border: 1px solid #000077; width: 400px; height: 100px; justify-content: center; align-items: center; margin: 20px; } .box div{ height: 50px; border: 1px solid #A52A2A; margin: 0 10px; text-align: center; }
<div class="box"> <div style="flex-grow:1">1</div> <div style="flex-grow:2">2</div> <div style="flex-grow:1">1</div> </div> <div class="box"> <div style="flex-grow:1">1</div> <div style="flex-grow:1">1</div> <div style="flex-grow:1">1</div> </div>
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
3.flex-shrink属性
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
.box{ display: flex; border: 1px solid #000077; width: 400px; height: 100px; justify-content: center; align-items: center; margin: 20px; } .box div{ height: 50px; width: 60px; border: 1px solid #A52A2A; margin: 0 10px; text-align: center; }
<div class="box"> <div style="flex-shrink:1">1</div> <div style="flex-shrink:0">1</div> <div style="flex-shrink:1">1</div> <div style="flex-shrink:1">1</div> <div style="flex-shrink:1">1</div> <div style="flex-shrink:1">1</div> <div style="flex-shrink:1">1</div> <div style="flex-shrink:1">1</div> </div>
如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
负值对该属性无效。
4.flex-basis属性
lex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
.item { flex-basis: <length> | auto; /* default auto */ }
5.flex属性
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
.item { flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] }
6.align-self属性
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch
.flex{ display: flex; justify-content: center; align-items: center; height: 100px; width: 400px; border: 1px solid #000077; margin-bottom: 10px; } .flex.center { align-items: center; } .flex.end { align-items: flex-end; } .flex.baseline { align-items: baseline; } .flex.stretch { align-items: stretch; } .item { width: 120px; box-sizing: border-box; padding: 5px; background: #999; text-align: center; margin: 0 5px; } .flex>.item:nth-child(1) { height: 60px; } .flex>.item:nth-child(2) { background: orange; height: 100px; font-size: 22px; } .flex>.item:nth-child(3) { background: green; height: 70px; } .flex>.item.s { height: auto; } .flex.stretch { margin-bottom: 60px; }
<div class="flex"> <div class="item">flex-start</div> <div class="item">flex-start</div> <div class="item">flex-start</div> </div> <div class="flex center"> <div class="item">center</div> <div class="item">center</div> <div class="item">center</div> </div> <div class="flex end"> <div class="item">flex-end</div> <div class="item">flex-end</div> <div class="item">flex-end</div> </div> <div class="flex baseline"> <div class="item">baseline</div> <div class="item">baseline</div> <div class="item">baseline</div> </div> <div class="flex stretch"> <div class="item s">stretch</div> <div class="item s">stretch</div> <div class="item s">stretch</div> </div>
纸上得来终觉浅,绝知此事要躬行!!
