日期:2020-10-11
作者:19届WY
标签:CSS弹性盒子,弹性容器的样式,弹性元素的样式
一、弹性盒和弹性元素
- flex(弹性盒,伸缩盒)
- 是CSS中的一种布局手段,他用来代替浮动来完成页面的布局
- flex可以使元素具有弹性,让元素可以随页面的大小的改变而改变
- 弹性容器
- 要使用弹性盒,必须先将一个元素设置为弹性容器
- 我们通过 display 来设置弹性容器
- display:flex 设置块级弹性容器
- display:inline-flex 设置为行内的弹性容器
- 弹性元素
- 弹性容器的子元素是弹性元素(弹性项)
- 弹性元素可以同时设置弹性容器
<style>
ul{
width:500px;
border:10px red solid;
/*将ul设置为弹性容器*/
display:flex;
}
</style>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
二、弹性容器的属性
- flex-direction 指定容器中弹性元素的排列方式
- 可选值:
- row 默认值,弹性元素在容器中水平排列(左向右)
- 主轴 自左向右 - row-reverse 弹性元素在容器中水平排列(右向左)
- 主轴 自右向左 - column 弹性元素纵向排列(自上向下)
- 主轴 自上向下 - column-reverse 弹性元素纵向排列(自下向上)
- 主轴 自下向上
- row 默认值,弹性元素在容器中水平排列(左向右)
- 主轴:
弹性元素的排列方向称为主轴
ul{
width:500px;
border:10px red solid;
/*将ul设置为弹性容器*/
display:flex;
flex-direction: row;
}
三、弹性元素的属性
- 弹性元素的属性:
- flex-grow 指定弹性元素的伸展的系数
- 当父元素有多余的空间时,子元素进行伸展
- 父元素的剩余空间,会按照比例进行分配 - flex-shrink 指定弹性元素的收缩系数
- 当父元素中的空间不足以容纳所有的子元素时,对子元素进行收缩
- flex-grow 指定弹性元素的伸展的系数
li{
width:100px;
height:100px;
background-color: #bfa;
font-size: 50px;
text-align: center;
line-height:100px;
/*flex-grow: 1;*/
flex-shrink:0;
}
li:nth-child(1){
flex-grow: 0;
}
li:nth-child(2){
background-color:pink;
flex-grow: 2;
}
li:nth-child(3){
background-color: orange;
flex-grow: 3;
}
四、弹性容器的样式
1、flex-wrap
- flex-wrap:
- 设置弹性元素是否在弹性容器中自动换行
- 可选值:
- nowrap 默认值,元素不会自动换行
- wrap 元素沿着辅轴方向自动换行
- wrap-reverse 元素沿着主轴反方向换行
<head>
<meta charset="utf-8">
<title></title>
<style>
*{
margin:0;
padding:0;
list-style:none;
}
ul{
width:500px;
border:10px red solid;
/*设置ul为弹性容器*/
display:flex;
flex-wrap:wrap-reverse;
}
li{
width:200px;
height:100px;
background-color: #bfa;
font-size: 50px;
text-align: center;
line-height:100px;
flex-shrink:0;
}
/*li:nth-child(1){
}*/
li:nth-child(2){
background-color:pink;
}
li:nth-child(3){
background-color: orange;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
- flex-flow:
- wrap和direction的简写属性
ul{
width:500px;
border:10px red solid;
/*设置ul为弹性容器*/
display:flex;
flex-flow:row wrap;
}
2、justify-content
justify-content:
- 如何分配主轴上的空白空间(主轴上的元素如何排列)
- 可选值:
- flex-start 元素沿着主轴起边排列
- flex-end 元素沿着主轴终边排列
- center 元素居中排列
- space-between 空白均分分布到元素间
- space-around 空白分布到元素两侧
- space-evenly 空白分布到元素的单侧
ul{
width:500px;
border:10px red solid;
/*设置ul为弹性容器*/
display:flex;
justify-content:space-around;
}
3、align-items
- align-items:
- 元素在轴上如何对齐
- 元素间的关系:
- 可选值:
- stretch 默认值,将元素的长度设置为相同的值
- flex-start 元素不会拉伸,沿着辅轴起边对齐
- flex-end 沿着辅轴的终边对齐
- center 居中对齐
- baseline 基线对齐
4、align-content:
- align-content:
辅轴空白空间的分布
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{
margin:0;
padding:0;
list-style:none;
}
ul{
width:600px;
height:800px;
border:10px red solid;
/*设置ul为弹性容器*/
display:flex;
align-items:center;
align-content: space-between;
}
li{
width:200px;
background-color: #bfa;
font-size: 50px;
text-align: center;
line-height:100px;
flex-shrink:0;
}
li:nth-child(1){
/*align-self:用来覆盖当前弹性元素上的align-items*/
align-self:stretch;
}
li:nth-child(2){
background-color:pink;
}
li:nth-child(3){
background-color: orange;
}
li:nth-child(4){
background-color: yellow;
}
li:nth-child(5){
background-color: green;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>
2
<div>2</div>
</li>
<li>
3
<div>3</div>
<div>3</div>
</li>
<li>1</li>
<li>
2
<div>2</div>
</li>
</ul>
</body>
</html>
align-self:用来覆盖当前弹性元素上的align-items
五、弹性元素的样式
1、弹性的增长缩减系数
- 弹性的增长系数
flex-grow: 1;
- 弹性元素的缩减系数
- 缩减系数的计算方式比较复杂
- 缩减多少是根据 缩减系数 和 元素大小来计算的
flex-shrink:1;
2、元素基础长度
- flex-basis 指定的是元素在主轴上的基础长度
- 如果主轴是横向的 则 该值指定的就是元素的宽度
- 如果主轴是纵向的 则 该值指定的就是元素的高度
- 默认值是 auto ,表示参考元素自身的高度或宽度
- 如果传递了一个具体的数值,则以该值为准
flex-basis:auto;
3、flex设置弹性元素的所有三个样式
- flex 增长 缩减 基础
- initial “flex:0 1 auto”
- auto “flex:1 1 auto”
- none "flex:0 0 auto"弹性元素没有弹性
4、order
- order决定弹性元素的排列顺序
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{
margin:0;
padding:0;
list-style:none;
}
ul{
width:900px;
border:10px red solid;
/*设置弹性盒*/
display:flex;
}
li{
width:200px;
height:100px;
background-color: #bfa;
font-size: 50px;
text-align: center;
line-height:100px;
flex-shrink:1;
flex:0 0 auto;
}
li:nth-child(1){
/*order 决定弹性元素的排列顺序*/
order:2;
}
li:nth-child(2){
background-color:pink;
order:3;
/*flex-grow: 2;*/
}
li:nth-child(3){
background-color: orange;
order:1;
/*flex-grow: 3;*/
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
换成flex:auto;