介绍
传统的css布局是基于css盒模型,依赖display、position、float等属性的布局方案,有稳定的跨浏览器兼容性,但在某些布局方面仍存在限制,
flex布局是Flexible Box的缩写,意为 弹性布局。用来为盒状模型提供最大的灵活性。
flex布局的特点:
任意方向的伸缩;
在样式层可以调换和从排顺序
主轴和交叉轴方便配置
子元素的空间拉伸和填充
沿着容器对齐
开启了flex布局的元素叫flex container
flex container里面的子元素叫做flex item
display属性为flex或者inline-flex可以成为flex container
如:
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: inline-flex;
}
模型图
容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。
项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。
注意:这里的图我是在网上截取的一个,所以纵轴就是交叉轴
应用在flex container上的css属性
flex-flow
flex-direction
flex-wrap
justify-content
align-items
align-content
flex-direction
flex-item默认都是沿着main axis(主轴)从main start开始往main end方向排布。
flex-direction决定了main axis 的方向,有4个取值
row(默认值)、row-reverse、column、column-reverse
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
flex-direction: row;
}
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
flex-direction: row-reverse;
}
row-reverse 就是将主轴的main start 和main end位置做颠倒,而交叉轴不变
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
flex-direction: column;
}
column-reverse就是将corss start 和 corss end位置作颠倒而主轴不变
justify-content
justify-content决定了flex item在main axis(主轴) 上的对齐方式
flex-start(默认值):与main start对齐
flex-end:与main end对齐
center:居中对齐
space-between: flex item之间的距离相等,与mian start、mian end两端对齐
space-evenly: flex item之间的距离相等 ,flex item与main start、main end之间的距离等于flex item之间的距离
space-around: flex item之间的距离相等,flex item与main start、main end之间的距离是flex item之间的距离的一半
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
justify-content: flex-start;
}
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
justify-content: flex-end;
}
center这就没什么好说的了,就是在main axis(主轴)上居中对齐
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
justify-content: center;
}
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
justify-content: space-between;
}
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
justify-content: space-evenly;
}
.container {
width: 400px;
height: 400px;
border: 1px solid #000000;
margin: 50px 50px;
display: flex;
justify-content: space-around;
}
align-items
align-items决定了flex item在cross axis(交叉轴)上的对齐方式
stretch(默认值):当flex item在cross axis方向的size为auto时,会自动拉伸至填充flex container
flex-start:与cross start对齐
flex-end:与cross end对齐
center:居中对齐
baseline:与基准线对齐
看完前的justify-content和space-evenly之后相信你对这个flex布局当中的布局方式你已经知道了,主要分清除主轴和交叉轴的方向,判断出start 和 end在什么地方然后了解到了每一个属性的意思就明白了。
所以这里的align-items的每一个属性我就放图上来
这里我特别说明一下baseline,baselin它时与基准线对齐,意思就是与文字的基准线对齐(你可以这样理解,把基准线理解为下划线,这样就直观了)
然后说一下stretch(默认值):当flex item在
cross axis方向的size为auto时,会自动拉伸至填充flex container
意思就是:当你的flex item的size为auto时(例如:height: auto;)会自动拉伸。
flex wrap
flex-wrap决定了flex container是单行还是多行
nowrap(默认):单行
wrap:多行
wrap-reverse:多行(对比wrap,corss start与cross end相反)
flex-flow
flex-flow是flex-direction || flex-wrap的简写
比如:flex-flow:column wrap 等价于:flex-direction:column flex-wrap:wrap
比如:flex-flow:row-recerse 等价于: flex-direction:row-reverse flex-wrap:nowrap
比如:flex-flow:wrap 等价于flex-wrap:wrap
align-content
align-content决定了多行flex item在cross axis上的对齐方式,用法与justify-content类似
stretch(默认值):与多行align-items的stretch类似
flex-start:与corss start对齐
flex-end:与corss end对齐
center:居中对齐
space-between: flex item之间的距离相等,与cross start、cross end两端对齐
space-around:flex item之间距离相等,flex item是与cross start、 cross end之间距离
是flex item之间距离的一半
这里说一下flex-start和flex-end他两个的对齐方式是在cross axis(交叉轴)上对齐,所以在主轴 main axis(主轴)不会有变化,依旧是按照main start(左对齐)对齐,当然这里需要添加多行显示也就是 flex-wrap: wrap才能实现这些布局效果,不然flex默认为单行显示将会缩进;如下图:
应用在flex item上的css属性
flex
flex-grow
flex-basis
flex-shrink
order
align-self
order
可以设置任意整数(正整数,负整数,0),值越小就月排在前面。
默认值是0
如下图在2号flex item加上order:1;因为默认值为0,所以2排在最后面
在三号felx items中添加一个order:-1;这时候3就排在最前面
align-self
flex item 可以通过align-self覆盖flex container设置align-items
auto(默认值):遵从flex container的align-items设置
stretch、flex-start、flex-end、center、baseline,效果跟align-items一致
这个我就不多说了
.item:nth-of-type(1) {
background-color: red;
align-self: flex-start;
}
.item:nth-of-type(2) {
background-color: yellow;
align-self: flex-end;
}
.item:nth-of-type(3) {
background-color: blue;
}
.item:nth-of-type(4) {
background-color: rgb(60, 104, 68);
}
flex-grow
flex-grow决定了flex item如何扩展
可以设置任意非负数字(正小数,正整数,0),默认值0
当flex container 在main axis方向上有剩余size时,flex-grow属性才会有效
这里的意思就是在一个容器当中,在主轴方向有剩余空间flex-grow属性才会生效。也就是有多余的宽度。
当然flex-grow有几种情况:
1、这种情况为整数的时候就是按比例扩展,比如容器总宽度为400,flex size宽度为100,三个为300余下的100就是100/3这样子分给每一个flex item
我这里设置的宽度是一样的,当然这里还可以设置每一各flex item的宽度不一样的,同样也是按照比例开扩展
.item:nth-of-type(1) {
background-color: red;
flex-grow: 1;
}
.item:nth-of-type(2) {
background-color: yellow;
flex-grow: 1;
}
.item:nth-of-type(3) {
background-color: blue;
flex-grow: 1;
}
2、如果所有flex item的flex-grow总和不超过1,每个flex item扩展的size为flex container的剩余size*flex-grow
这一种情况就是小数之和不大于或等于1的情况,就是每一个的flex item * 0.2(flex-grow的值)来扩展
.item:nth-of-type(1) {
background-color: red;
flex-grow: 0.2;
}
.item:nth-of-type(2) {
background-color: yellow;
flex-grow: 0.2;
}
.item:nth-of-type(3) {
background-color: blue;
flex-grow: 0.2;
}
3、如果所有flex item的flex-grow总和sum大于1,每个flex item扩展的size为flex container的剩余size*flex-grow/sum
当小数之和大于1之后就是按照比列来扩展了,比如说分别给三个flex item分配0.2、0.8、0.8给flex-grow这时候的扩展的宽度就是应该是size * flex-grow / sum
.item:nth-of-type(1) {
background-color: red;
flex-grow: 0.2;
}
.item:nth-of-type(2) {
background-color: yellow;
flex-grow: 0.8;
}
.item:nth-of-type(3) {
background-color: blue;
flex-grow: 0.8;
}
4、flex item扩展后的最终size不能超过max-width\max-height
flex-grow:max-width;
flex-grow:max-height;
这里我就不用解释了吧,意思就是不管 你扩展的比例不管有多大,flex size都不能超过最大值
flex-shrink
flex-shrink决定了flex item如何收缩
可以设置任意非负数字(正小数、正整数、0),默认值是1
当flex item 在main axis方向上超过了flex container 的size,flex-shrink属性才会有效
每个flex item收缩size为
flex item超出flex container的size*收缩比例/所有flex item的收缩比例之和
收缩比例 = flex-shrink * flex item的base size
base size就是flex item放入flex container之前的size
width: 110px;
background-color: red;
flex-shrink: 1;
}
.item:nth-of-type(2) {
width: 120px;
background-color: yellow;
flex-shrink: 2;
}
.item:nth-of-type(3) {
width: 130px;
background-color: blue;
flex-shrink: 3;
}
.item:nth-of-type(4) {
width: 140px;
background-color: rgb(230, 23, 119);
flex-shrink: 4;
}
.item:nth-of-type(5) {
width: 150px;
background-color: rgb(8, 150, 79);
flex-shrink: 5;
}
这里可以看见宽度变化是不一样的;至于他是如何算的呢?马上就给你分析
下面这个的意思呢就是,flex item总和的宽度 - container总的宽度 = 超出部分的宽度
flex item的宽度 * flex- shrink的值 = item收缩的比例(注意这里的收缩比例的计算)
需要收缩的距离 = 超出部分的宽度 * item收缩的比例 / item收缩的比例之和
felx item之和: 110+120+130+140+150 = 650
item之和超出容器部分:650 - 400 = 250
假设 sum=110 * 1+120 * 2+130 * 3+140 * 4+150 * 5
item1的收缩比例是 110 * 1,需要收缩的距离250 * (110 * 1)/sum
item2的收缩比例是 110 * 2,需要收缩的距离250 * (110 * 2)/sum
item3的收缩比例是 110 * 3,需要收缩的距离250 * (110 * 3)/sum
item4的收缩比例是 110 * 4,需要收缩的距离250 * (110 * 4)/sum
item5的收缩比例是 110 * 5,需要收缩的距离250 * (110 * 5)/sum
这里也是一样, flex item收缩后的最终size不能小于min-width/min-height
看到这里是不是头疼了呢?可能你会问道,难道做项目的时候还要来算一算这多宽拿多宽吗?我敢百分百的肯定,基本没有人会在这上面去算这个东西,虽然在做项目的时候不去算,但是你要知道其中的原理后,做项目的时候才会快,才知道哪一个flex item收缩之后在容器中大概占据的位置是多少。
flex-basis
flex-base用来设置flex item在mian axis方向上的base size
auto(默认值)、content:取决于内容本省的size
决定flex item最终base size的因素,从优先级高到低
max-width\max-height\min-width\min-height
flex-basis (只能用在flex布局上取决于主轴)
width\height
内容本身size
只能用在flex布局上取决于主轴
felx-base用法和width是一样的只是一个只能使用在flex布局上面,同时使用还有一个优先级顺序
总结:
lex-flow是flex-direction || flex-wrap简写
flex-dirction设置main axis(主轴)的方向
flex-wrap设置是否能换行
justify-content设置flex item在main axis(主轴)上的对齐方式
align-items设置flex item在cross axis(主轴)上的对齐方式(一般是针对单行)
align-content设置flex item 在cross axis(主轴)上的对齐方式(一般是针对多行对齐)
flex是flex-grow flex-shrink? || flex-basis的简写
flex-grow决定了flex item在main axis(主轴)方向上如何扩展
flex-shrink决定了flex item在mian axis方向上如何扩展
flex-basis设置flex item是在main axis方向如何收缩的
flex-basis设置flex item在main axis方向上的base size
order设置flex item的排布顺序
align-self允许flex item覆盖flex container设置的align-items
这里我就介绍完了flex布局了,希望能给你们带来帮助,本文纯手打,然后截图粘贴花了不少的时间,以上有什么不对的,或者需要补充的,欢迎指点!
** 本文中若出现侵权请留言联系**