基本概念
采用 Flex 布局的元素,称为 Flex 容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为 Flex 项目(flex item),简称"项目"。
容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。
项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。
容器的属性
flex-direction\
flex-wrap\
flex-flow\
justify-content\
align-items\
align-content
flex-direction 属性
属性决定主轴的方向(即项目的排列方向)
flex-direction: row | row-reverse | column | column-reverse;
- 默认 display:flex 属性 方向从左到右
<style>
*{
<!--重置-->
margin: 0;
padding: 0;
}
.box *{
<!-- 美化代码 -->
text-align: center;
padding: 40px;
color:white;
font-size: 20px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.box{
display: flex;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
.item1{
background: #FF6666;
}
.item2{
background: #33CC99;
}
.item3{
background: #0099CC;
}
.item4{
background: #FFCC33;
}
.item5{
background: #336699;
}
.item6{
background: #FF0033;
}
</style>
<div class="box">
<div class="item1">item 1</div>
<div class="item2">item 2</div>
<div class="item3">item 3</div>
<div class="item4">item 4</div>
<div class="item5">item 5</div>
<div class="item6">item 6</div>
</div>
- flex-direction: row;
.box{
display: flex;
flex-direction: row;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- flex-direction: row-reverse;
.box{
display: flex;
flex-direction: row-reverse;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- flex-direction: column;
.box{
display: flex;
flex-direction: column;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- flex-direction: column-reverse;
.box{
display: flex;
flex-direction: column-reverse;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
flex-wrap属性
默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。
flex-wrap: nowrap | wrap | wrap-reverse;
- flex-wrap: nowrap;
.box{
display: flex;
flex-direction: row;
flex-wrap: nowrap;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- flex-wrap: wrap;
.box{
display: flex;
flex-direction: row;
flex-wrap: wrap;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- flex-wrap: wrap-reverse;
.box{
display: flex;
flex-direction: row;
flex-wrap: wrap-reverse;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
flex-flow属性
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
flex-flow: <flex-direction> || <flex-wrap>;
.box{
display: flex;
flex-flow: row wrap-reverse;
background: #FFFFCC;
height: 100vh;
}
justify-content属性
justify-content属性定义了项目在主轴上的对齐方式。
justify-content: flex-start | flex-end | center | space-between | space-around;
- justify-content: flex-start;
.box{
display: flex;
flex-direction: row;
justify-content: flex-start;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- justify-content: flex-end;
.box{
display: flex;
flex-direction: row;
justify-content: flex-end;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- justify-content: center;
.box{
display: flex;
flex-direction: row;
justify-content: center;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- justify-content: space-between;
.box{
display: flex;
flex-direction: row;
justify-content: space-between;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
- justify-content: space-around;
.box{
display: flex;
flex-direction: row;
justify-content: space-around;
<!--淡黄色-->
background: #FFFFCC;
height: 100vh;
}
align-items 属性
align-items属性定义项目在交叉轴上如何对齐。
align-items: flex-start | flex-end | center | baseline | stretch;flex-start:交叉轴的起点对齐。\
flex-end:交叉轴的终点对齐。\
center:交叉轴的中点对齐。\
baseline: 项目的第一行文字的基线对齐。\
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
- align-items: flex-start
<style>
*{
margin: 0;
padding: 0;
}
.box *{
text-align: center;
padding: 40px;
color:white;
font-size: 20px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.box{
display: flex;
background: #FFFFCC;
height: 100vh;
justify-content:space-between;
align-items: flex-start;
}
.item1{
background: #FF6666;
height: 20vh;
}
.item2{
background: #33CC99;
height: 40vh;
}
.item3{
background: #0099CC;
height: 30vh;
}
.item4{
background: #FFCC33;
height: 50vh;
}
.item5{
background: #336699;
height: 30vh;
}
.item6{
background: #FF0033;
height: 40vh;
}
</style>
<div class="box">
<div class="item1">item 1</div>
<div class="item2">item 2</div>
<div class="item3">item 3</div>
<div class="item4">item 4</div>
<div class="item5">item 5</div>
<div class="item6">item 6</div>
</div>
- align-items: flex-end
.box{
display: flex;
background: #FFFFCC;
height: 100vh;
justify-content:space-between;
align-items: flex-end;
}
- align-items: center
.box{
display: flex;
background: #FFFFCC;
height: 100vh;
justify-content:space-between;
align-items: center;
}
- align-items: stretch
.box{
display: flex;
background: #FFFFCC;
/* height: 100vh; */ justify-content:space-between;
align-items: stretch;
}
.item1{
background: #FF6666;
/* height: 20vh; */
}
.item2{
background: #33CC99;
height: 40vh;
}
.item3{
background: #0099CC;
/* height: 30vh; */
}
.item4{
background: #FFCC33;
/* height: 50vh; */
}
.item5{
background: #336699;
/* height: 30vh; */
}
.item6{
background: #FF0033;
/* height: 40vh; */
}
align-content属性
align-content属性定义了多根轴线的对齐方式。如果项目只有<mark>一根</mark>轴线,该属性不起作用。
align-content: flex-start | flex-end | center | space-between | space-around | stretch;flex-start:与交叉轴的起点对齐。\
flex-end:与交叉轴的终点对齐。\
center:与交叉轴的中点对齐。\
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。\
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。\
stretch(默认值):轴线占满整个交叉轴。
项目的属性
order\
flex-grow\
flex-shrink\
flex-basis\
flex\
align-self
order属性
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
order: <integer>;
.item1{
background: #FF6666;
order: 3;
}
.item2{
background: #33CC99;
order: 2;
}
.item3{
background: #0099CC;
order: -2;
}
.item4{
background: #FFCC33;
order: 4;
}
.item5{
background: #336699;
order: 1;
}
.item6{
background: #FF0033;
order: 6;
}
flex-grow属性
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
flex-grow: <number>; / default 0 /
.item1{
background: #FF6666;
flex-grow: 1;
}
.item2{
background: #33CC99;
flex-grow: 2;
}
.item3{
background: #0099CC;
flex-grow: 1;
}
flex-shrink属性
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
flex-shrink: <number>; / default 1 /
如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。\
负值对该属性无效。
flex-basis属性
flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
flex-basis: <length> | auto; / default auto /
它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。
flex属性
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。
align-self属性
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
align-self: auto | flex-start | flex-end | center | baseline | stretch;
该属性可能取6个值,除了auto,其他都与align-items属性完全一致。
- align-self: flex-end;
.box{
display: flex;
flex-direction: column;
background: #FFFFCC;
align-content: space-between;
}
.item1{
background: #FF6666;
width:30vh;
}
.item2{
background: #33CC99;
width:40vh;
align-self: flex-end;
}
.item3{
background: #0099CC;
width:60vh;
}
.item4{
background: #FFCC33;
width:50vh;
}
.item5{
background: #336699;
width:20vh;
}
.item6{
background: #FF0033;
width:60vh;
}
(完)
安利一个动画教程:https://www.bilibili.com/vide... \
参考:http://www.ruanyifeng.com/blo...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。