什么是弹性布局
弹性布局Flexbox
Flexbox 是 flexible box 的简称(灵活的盒子容器),是 CSS3 引入的新的布局模式。它决定了元素如何在页面上排列,使它们能在不同的屏幕尺寸和设备下可预测地展现出来。
为什么要使用弹性布局
在弹性布局模型中,弹性容器的子元素可以在任何方向上排布,也可以“弹性伸缩”其尺寸,既可以增加尺寸以填满未使用的空间,也可以收缩尺寸以避免父元素溢出。子元素的水平对齐和垂直对齐都能很方便的进行操控。通过嵌套这些框(水平框在垂直框内,或垂直框在水平框内)可以在两个维度上构建布局。
基本概念
- flex容器:采用flex布局的元素
- flex项:又称flex子元素,是需要布局的元素
- 排列方向(direction):决定了 flex 项的布局方向
- 主轴:默认为容器的水平方向
- 侧轴:默认为容器的垂直方向
设置flex布局:display:flex
给父容器添加CSS语句:display:flex
即可把容器设置为flex布局,其子容器会受flex布局影响
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>flex</title>
<style>
#flex1{ /*父容器*/
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex; /*父容器设置为flex布局*/
}
#item1{ /*子容器1*/
width: 300px;
height: 300px;
background: pink;
font-size: 50px;
}
#item2{ /*子容器2*/
width: 300px;
height: 300px;
background: yellow;
font-size: 50px;
}
#item3{ /*子容器3*/
width: 300px;
height: 300px;
background: grey;
font-size: 50px;
}
</style>
</head>
<body>
<div id="flex1">
<div id="item1">
1
</div>
<div id="item2">
2
</div>
<div id="item3">
3
</div>
</div>
</body>
</html>
flex容器的属性
flex布局中只能在父容器中使用的属性有6条:
- flex-direction
- justify-content
- align-items
- flex-wrap
- align-content
- flex-flow
容器之flex-direction
设置主轴的方向,它有四个属性:
row:默认值,主轴为水平方向,起点在左端
row-reverse:主轴为水平方向,起点在右端
column:主轴为垂直方向,起点在上方
column-reverse:主轴为垂直方向,起点在下方
flex-direction:row-reverse
主轴为水平方向,起点在右端
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row-reverse;
}
</style>
flex-direction:column
主轴为垂直方向,起点在上方
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:column;
}
</style>
flex-direction:column-reverse
主轴为垂直方向,起点在下方
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:column-reverse;
}
</style>
以下演示例子主轴均取默认值。
容器之justify-content
设置主轴上子元素的排列方式,它有5个属性:
flex-start:默认值,左对齐
flex-end:右对齐
center:在主轴居中对齐
space-around:平分剩余空间,项目之间间隔相等
space-between:先两边贴边,再平分剩余空间
justify-content:flex-start
默认值,左对齐
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
justify-content:flex-start;
}
</style>
justify-content:flex-end
右对齐
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
justify-content:flex-end;
}
</style>
justify-content:center
在主轴居中对齐
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
justify-content:center;
}
</style>
justify-content:space-around
平分剩余空间,项目之间间隔相等
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
justify-content:space-around;
}
</style>
justify-content:space-between
先两边贴边,再平分剩余空间
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
justify-content:space-between;
}
</style>
容器之align-items
设置侧轴上的子元素排列方式(单行),有4个属性:
flex-start:默认值,从上到下
flex-end:从下到上
center:垂直居中(默认主轴和侧轴时)
stretch:拉伸
align-items:flex-start
默认值,从上到下
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-items:flex-start;
}
</style>
align-items:flex-end
从下到上
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-items:flex-end;
}
</style>
align-items:center
垂直居中
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-items:center;
}
</style>
align-items:stretch
拉伸(子元素未设置宽度时)
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-items:stretch;
}
</style>
容器之flex-wrap
设置子元素是否换行,它有2个属性:
nowrap:默认值,不换行
wrap:换行
flex-wrap:nowrap
默认值,不换行,如所有子元素的长度之和超过父容器,则子项目长度自动压缩,不会不会超出父容器
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
flex-wrap:nowrap;
}
</style>
flex-wrap:wrap
换行
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
flex-wrap:wrap;
}
</style>
容器之align-content
设置侧轴上的子元素排列方式并且只能用于子项出现换行的情况(多行),在单行无效果,它有6个属性:
flex-start:默认值,子元素从侧轴头部开始排列
flex-end:子元素从侧轴尾部开始排列
center:子元素在侧轴居中显示
space-around:子元素在侧轴平分剩余空间,项目之间间隔相等
space-between:子元素在侧轴两边贴边,再平分剩余空间
stretch:子元素高度平分父元素高度
align-content:flex-start
默认值,子元素从侧轴头部开始排列
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-content:flex-start;
}
</style>
align-content:flex-end
子元素从侧轴尾部开始排列
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-content:flex-end;
}
</style>
align-content:center
子元素在侧轴居中显示
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-content:center;
}
</style>
align-content:space-around:
子元素在侧轴平分剩余空间,项目之间间隔相等
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-content:space-around;
}
</style>
align-content:space-between
子元素在侧轴两边贴边,再平分剩余空间
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-content:space-between;
}
</style>
align-content:stretch
子元素高度平分父元素高度(子元素高度未指定时)
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-direction:row;
align-content:stretch;
}
</style>
容器之flex-flow
复合属性,相当于同时设置了flex-direction和flex-wrap。使用这个属性时可使用上述两个属性的属性值,例如:flex-flow: row wrap
flex-flow:row wrap
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
flex-flow: row wrap;
}
</style>
flex项目(子元素)的属性
flex布局中只能在子元素中使用的属性有6条:
- order
- flex-grow
- flex-shrink
- flex-basis
- flex
- align-self
项目之order
改变项目的排列顺序,数值越小,排列越前,默认为0
<style>
#flex1{
width: 1200px;
height: 700px;
border: 1px solid red;
margin: auto;
display: flex;
/*父容器不能定义order*/
}
#item1{
width: 300px;
height: 300px;
background: pink;
font-size: 50px;
order: 1;
}
#item2{
width: 300px;
height: 300px;
background: yellow;
font-size: 50px;
order: 2;
}
#item3{
width: 300px;
height: 300px;
background: grey;
font-size: 50px;
/*未定义order,默认为0*/
}
#item4{
width: 300px;
height: 300px;
background: blue;
font-size: 50px;
/*未定义order,默认为0*/
}
#item5{
width: 300px;
height: 300px;
background: orange;
font-size: 50px;
order: 3;
}
</style>
项目之flex-grow
定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大,即定义每个子元素所占父元素的空间的比例大小
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
}
#item1{
background: pink;
font-size: 50px;
flex-grow: 0;
}
#item2{
background: yellow;
font-size: 50px;
flex-grow: 1;
}
#item3{
background: grey;
font-size: 50px;
flex-grow: 5;
}
#item4{
background: blue;
font-size: 50px;
flex-grow: 0;
}
#item5{
background: orange;
font-size: 50px;
flex-grow: 0;
}
</style>
项目之 flex-shrink
定义项目的缩小比例,默认为1,即如果空间不足,该项目将缩小
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
}
#item1{
width: 300px;
height: 300px;
background: pink;
font-size: 50px;
flex-shrink: 1;
}
#item2{
width: 300px;
height: 300px;
background: yellow;
font-size: 50px;
flex-shrink: 2;
}
#item3{
width: 300px;
height: 300px;
background: grey;
font-size: 50px;
flex-shrink: 3;
}
#item4{
width: 300px;
height: 300px;
background: blue;
font-size: 50px;
flex-shrink: 1;
}
#item5{
width: 300px;
height: 300px;
background: orange;
font-size: 50px;
flex-shrink: 1;
}
</style>
项目之flex-basis
属性定义了在分配多余空间之前,项目占据的主轴空间
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
}
#item1{
width: 300px;
height: 300px;
background: pink;
font-size: 50px;
flex-basis: 10%;
}
#item2{
width: 300px;
height: 300px;
background: yellow;
font-size: 50px;
flex-basis: 20%;
}
#item3{
width: 300px;
height: 300px;
background: grey;
font-size: 50px;
flex-basis: 30%;
}
#item4{
width: 300px;
height: 300px;
background: blue;
font-size: 50px;
flex-basis: 40%;
}
#item5{
width: 300px;
height: 300px;
background: orange;
font-size: 50px;
flex-basis: 50%;
}
</style>
项目之flex
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0、1和auto,可以理解为项目所占的父元素空间的比例大小
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
}
#item1{
width: 300px;
height: 300px;
background: pink;
font-size: 50px;
flex: 5;
}
#item2{
width: 300px;
height: 300px;
background: yellow;
font-size: 50px;
flex: 4;
}
#item3{
width: 300px;
height: 300px;
background: grey;
font-size: 50px;
flex: 3;
}
#item4{
width: 300px;
height: 300px;
background: blue;
font-size: 50px;
flex: 2;
}
#item5{
width: 300px;
height: 300px;
background: orange;
font-size: 50px;
flex: 1;
}
</style>
项目之align-self
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。它有6个属性:
auto:默认值,从上到下
flex-start:元素位于容器的开头
flex-end:元素位于容器的结尾
center:垂直居中(默认主轴和侧轴时)
stretch:元素被拉伸以适应容器
baseline:元素位于容器的基线上
<style>
#flex1{
width: 1200px;
height: 400px;
border: 1px solid red;
margin: auto;
display: flex;
}
#item1{
width: 300px;
height: 300px;
background: pink;
font-size: 50px;
align-self: auto;
}
#item2{
width: 300px;
height: 300px;
background: yellow;
font-size: 50px;
align-self: flex-start;
}
#item3{
width: 300px;
height: 300px;
background: grey;
font-size: 50px;
align-self: flex-end;
}
#item4{
background: blue;
font-size: 50px;
align-self: stretch;
}
#item5{
width: 300px;
height: 300px;
background: orange;
font-size: 50px;
align-self: baseline;
}
</style>