Flex基本介绍
1、布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性。它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现。
2、Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。
3、任何一个容器可以指定为Flex布局
.box1{
display:flex;
}
.box2{
display:inline-flex;
}
.box3{
display:-webkit-flex;
display:flex;
}
值得注意的是:设置成Flex布局之后,子元素的float、clear、vertical-align属性无效
Flex 基本概念
采用Flex布局的元素,称为Flex容器(flex container),简称”容器”。它的所有子元素自动成为容器成员,称为Flex项目(flex item),简称”项目”。
容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。
项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。
容器的属性(是容器的属性,不是里边的item)
1、flex-direction
flex-direction属性决定主轴的方向(即项目的排列方向)
.box{
flex-direction: row | row-reverse | column | column-reverse;
/*
row(默认值):主轴为水平方向,起点在左端。
row-reverse:主轴为水平方向,起点在右端。
column:主轴为垂直方向,起点在上沿。
column-reverse:主轴为垂直方向,起点在下沿。
*/
}
例子:
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
.box{
display: flex;
flex-direction: row;
border: 1px solid black;
}
.item{
width: 100px;
height: 100px;
border: 1px solid red;
}
flex-direction: row的效果图:
flex-direction: row-reverse的效果图:
flex-direction: column的效果图:
flex-direction: column-reverse的效果图:
2、flex-wrap
默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,如果一条轴线排不下,如何换行。
.box{
flex-wrap: nowrap | wrap | wrap-reverse;
/*
nowrap : 不换行
wrap :换行,第一行在上面
wrap-reverse: 换行,第一行在下边
*/
}
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box{
width: 450px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
border: 1px solid black;
}
.item{
width: 100px;
height: 100px;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
</body>
</html>
flex-wrap: nowrap的效果图:撑起来了
flex-wrap: wrap的效果图:
flex-wrap: wrap-reverse的效果图:
3、flex-flow
flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
.box {
flex-flow: <flex-direction> <flex-wrap>;
}
4、justify-content
justify-content属性定义了项目在主轴上的对齐方式。
.box {
justify-content: flex-start | flex-end | center | space-between | space-around;
/*
flex-start(默认值):左对齐
flex-end:右对齐
center: 居中
space-between:两端对齐,项目之间的间隔都相等。
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
*/
}
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 650px;
display: flex;
flex-direction: row;
justify-content: space-around;
border: 1px solid black;
}
.item {
width: 100px;
height: 100px;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
</body>
</html>
justify-content:flex-start效果图:
justify-content: flex-end效果图:
justify-content: center效果图:
justify-content: space-between效果图:
justify-content: space-around效果图:
5、align-items
align-items属性定义项目在交叉轴上如何对齐。
.box {
align-items: flex-start | flex-end | center | baseline | stretch;
/*
flex-start:交叉轴的起点对齐。
flex-end:交叉轴的终点对齐。
center:交叉轴的中点对齐。
baseline: 项目的第一行文字的基线对齐。
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
*/
}
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 650px;
height: 300px;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: flex-start;
border: 1px solid black;
}
.item {
width: 100px;
height: 100px;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="box">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
</body>
</html>
align-items:flex-start效果图:
align-items:flex-end效果图:
align-items:center 效果图:
6、align-content
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
.box {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
/*
flex-start:与交叉轴的起点对齐。
flex-end:与交叉轴的终点对齐。
center:与交叉轴的中点对齐。
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
stretch(默认值):轴线占满整个交叉轴。
*/
}
项目的属性
order
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
.item {
order: <integer>;
}
flex-grow
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
.item {
flex-grow: <number>; /* default 0 */
}
例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 500px;
height: 300px;
display: flex;
flex-direction: row;
justify-content: flex-start;
border: 1px solid black;
}
.item {
width: 100px;
height: 100px;
}
.item1{
flex-grow: 1;
background: red;
}
.item2{
background: yellow;
}
.item3{
background: blue;
}
</style>
</head>
<body>
<div class="box">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
</body>
</html>
剩余空间为500-100*3 = 200,把item1的flex-grow:1,其他的都是0,那么剩余空间就会分成1份,全部分给item1,此时item1的实际宽度比设计的要大。
如果item1的flex-grow:1,item2的flex-grow:2,其他是0,那么剩余空间会分成三份,其中1分分为item1,2份分给item2。
flex-shrink
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
.item {
flex-shrink: <number>; /* default 1 */
}
因为flex环境默认是不换行的,即使父容器宽度不够也不会,除非设置flex-wrap来换行。这个时候flex-shrink就有意义了。flex-grow表示扩张的比例,那么flex-shrink就代表压缩的比例。来看例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 100px;
height: 150px;
display: flex;
flex-direction: row;
justify-content: flex-start;
border: 1px solid black;
}
.item {
width: 100px;
height: 100px;
}
.item1{
flex-shrink: 1;
background: red;
}
.item2{
flex-shrink: 2;
background: yellow;
}
.item3{
flex-shrink: 3;
background: blue;
}
</style>
</head>
<body>
<div class="box">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
</div>
</body>
</html>
总共宽度为100,三个div肯定不够的,看flex-shrink,item1:item2:item3 = 1:2:3,这表示压缩率的比是1:2:3,因此有如下方程100 = 100x + 100y + 100z,其中x:y:z = 1:2:3.
flex-basis
flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。目前没用过这个属性。
.item {
flex-basis: <length> | auto; /* default auto */
}
flex
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
align-self
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
/*该属性可能取6个值,除了auto,其他都与align-items属性完全一致。*/
}