Flex:Flexible Box的缩写,意为”弹性布局”
开启Flex布局的标志:在父容器上定义display:flex;
示例
实现代码:
<div class="box">
<div class="box1">one</div>
<div class="box2">two</div>
<div class="box3">three</div>
</div>
.box{
width: 500px;
height: 400px;
/*开启flex布局的标志*/
display: flex;
background-color: lightgray;
border: 1px solid #000;
}
.box1,.box2,.box3{
width: 100px;
height: 100px;
color: darkred;
font-size: 30px;
text-align: center;
line-height: 100px;
}
.box1{
background-color: bisque;
}
.box2{
background-color: chocolate;
}
.box3{
background-color: cornflowerblue;
}
原始效果图:
主要介绍内容
定义在父容器上的属性:决定子元素整体的排列方式
父容器上的属性:在flex布局中是一个一维(只有一根主轴)的布局方式,即只能处理一个维度上的布局(只能处理一行或一列)
属性小目录:[点击可跳转到相应详细位置]
- 决定主轴方向:flex-direction
- 是否换行: flex-wrap
- 决定主轴方向的排列方式: justify-content
- 垂直轴/交叉轴的排列方向: align-items
- 决定多条主轴的对齐方式:align-content
-
开启布局方式:
display:flex;
-
决定主轴方向:flex-direction
-
flex-direction: row;
(默认):水平方向,start => end.box{ ... ... display: flex; flex-direction: row; ... ... }
效果图:
-
flex-direction: row-reverse;
:水平反向,end => start.box{ ... ... display: flex; flex-direction: row-reverse; ... ... }
效果图:
-
flex-direction: column;
:垂直方向,start => end.box{ ... ... display: flex; flex-direction: column; ... ... }
效果图:
-
flex-direction: column-reverse;
:垂直反向,end => start.box{ ... ... display: flex; flex-direction: column-reverse; ... ... }
效果图:
-
-
是否换行: flex-wrap
<div class="box"> <div class="box1">one</div> <div class="box2">two</div> <div class="box3">three</div> <div class="box1">one</div> <div class="box2">two</div> <div class="box3">three</div> </div>
-
flex-wrap: nowrap;
(默认): 不换行,会压缩 ‘div’.box{ ... ... display: flex; flex-wrap: nowrap; ... ... }
效果图:
-
flex-wrap: wrap;
: 换行.box{ ... ... display: flex; flex-wrap: wrap; ... ... }
效果图: 中间出现留白部分,如何解决? 见下文部分
-
flex-wrap: wrap-reverse;
: 反向换行.box{ ... ... display: flex; flex-wrap: wrap-reverse; ... ... }
效果图:
-
-
决定主轴方向的排列方式: justify-content
-
justify-content: flex-start;
: 以当前起点作为元素的起点.box{ ... ... display: flex; justify-content: flex-start; ... ... }
效果图:
-
justify-content: flex-end;
: 以end作为元素的起点.box{ ... ... display: flex; justify-content: flex-end; ... ... }
效果图:
-
justify-content: center;
: 排列在中心的位置.box{ ... ... display: flex; justify-content: center; ... ... }
效果图:
-
justify-content: space-between;
: 两边对齐.box{ ... ... display: flex; justify-content: space-between; ... ... }
效果图:
-
justify-content: space-around;
: 等距对齐(每一个盒子的间距都相等).box{ ... ... display: flex; justify-content: space-around; ... ... }
效果图:
注意:
如果主轴方向改变(变为垂直),那么改变的是垂直方向的排列顺序
例如:
.box{ ... ... display: flex; flex-direction: column; justify-content: space-around; ... ... }
效果图:
上述其他属性同理…
- 垂直轴/交叉轴的排列方向: align-items-
align-items: flex-start;
.box{ ... ... display: flex; align-items: flex-start; ... ... }
效果图:
-
align-items: flex-end;
.box{ ... ... display: flex; align-items: flex-end; ... ... }
效果图:
-
align-items: center;
.box{ ... ... display: flex; align-items: center; ... ... }
效果图:
-
align-items: baseline;
: 基线(以文字基准线对齐)上面初始设置子盒子div:
line-height:100px;
修改并设置三个盒子不同的line-height值:
.box1,.box2,.box3{ width: 100px; height: 100px; color: darkred; font-size: 30px; text-align: center; /* line-height: 100px; */ } .box1{ background-color: bisque; line-height: 100px; } .box2{ background-color: chocolate; line-height: 80px; } .box3{ background-color: cornflowerblue; line-height: 60px; }
效果图:
接下来,设置以文字基线对齐:
.box{ ... ... display: flex; align-items: baseline; ... ... }
效果图:
-
align-items: stretch;
: 延展,只有在当前高度没有定义的情况下才会延展.box{ ... ... display: flex; align-items: stretch; ... ... } .box1,.box2,.box3{ width: 100px; /* height: 100px; */ color: darkred; font-size: 30px; text-align: center; /* line-height: 100px; */ }
效果图:
-
-
决定多条主轴的对齐方式:align-content
只有一根主轴:
多根主轴:
解决换行留白问题:
通过调整align-content来调整多根主轴的对齐方式
-
align-content: flex-start;
: 多根主轴按向上靠近(flex-start)方式来排列.box{ ... ... display: flex; flex-wrap: wrap; align-content: flex-start; ... ... }
效果图:
-
align-content: flex-end;
: 多根主轴按向下靠近(flex-end)方式来排列.box{ ... ... display: flex; flex-wrap: wrap; align-content: flex-end; ... ... }
效果图:
-
align-content: center;
: 多根主轴按向中间靠近(center)方式来排列.box{ ... ... display: flex; flex-wrap: wrap; align-content: center; ... ... }
效果图:
-
align-content: space-between;
: 多根主轴按向两端靠近(space-between)方式来排列.box{ ... ... display: flex; flex-wrap: wrap; align-content: space-between; ... ... }
效果图:
-
align-content: space-around;
: 多根主轴按向两端等距靠近(space-around)方式来排列.box{ ... ... display: flex; flex-wrap: wrap; align-content: space-around; ... ... }
效果图:
-
align-content: stretch;
: 由当前的内容高度(line-height)来决定.box{ ... ... display: flex; flex-wrap: wrap; align-content: stretch; ... ... } .box1,.box2,.box3{ ... ... line-height: 100px; }
效果图:
调整子盒子line-height的高度:
-
如果没有属性:
align-content: stretch;
.box{ ... ... display: flex; flex-wrap: wrap; /*align-content: stretch;*/ ... ... } .box1,.box2,.box3{ ... ... /*line-height: 100px;*/ } .box1{ background-color: bisque; line-height: 100px; } .box2{ background-color: chocolate; line-height: 80px; } .box3{ background-color: cornflowerblue; line-height: 60px; }
效果图:
-
添加属性:
align-content: stretch;
并 关闭高度.box{ ... ... display: flex; flex-wrap: wrap; align-items: flex-start; align-content: stretch; ... ... } .box1,.box2,.box3{ ... ... /* height: 100px; */ /*line-height: 100px;*/ } .box1{ background-color: bisque; line-height: 100px; } .box2{ background-color: chocolate; line-height: 80px; } .box3{ background-color: cornflowerblue; line-height: 60px; }
效果图:
注意:
要设置排列对齐方式:
align-items: flex-start | flex-end | center;
否则,不起效果
-
-