flex弹性布局详解

2022-04-17 16:25:13
目录
初了解基本概念属性容器属性(container)flex 项目属性(item属性)骰子布局实践

初了解

在学习弹性布局之前首先就要明白其概念main axis(主轴)从main start 开始往main end方向排布flex-direction决定了main axis的方向,有四个取值row(默认值)、row-reverse、column、column-reverse

.box {  flex-direction: row | row-reverse | column | column-reverse;}

row(默认值):主轴为水平方向,起点在左端。
row-reverse:主轴为水平方向,起点在右端。
column:主轴为垂直方向,起点在上沿。
column-reverse:主轴为垂直方向,起点在下沿。

2.justify-content

justify-content决定了flex item在main axis上的对齐方式flex-start(默认值):与main start对齐flex-end:与main end对齐center:居中space-between:flex items 之间的距离相等,与main start、main end两端对齐space-evenly: flex items 之间的距离相等,flex items与main start 、main 易采站长站end 之间的距离等于flex items之间的距离space-around :flex items 之间的距离相等,flex items与main start 、main end 之间的距离等于flex items之间的距离的一半

这个属性的目的主要就是为了排列main axis的item位置

):单行wrap:多行//这个比较少用wrap-reverse:多行(对比wrap,cross start 与cross end相反)

默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

5 align-content

 决定了多行flex items 在cross axis的对齐方式 用法与justify-content相似 一个是横轴。一个控制竖轴stretch(默认值):与align-items的stretch类似,当items有高度的时候,无效果flex-start:与cross start 对齐flex-end :与cross end 对齐center:居中对齐space-between:flex items 之间的距离相等,与cross start、cross end两端对齐space-evently: flex items 之间的距离相等,flex items与cross start 、cross end 之间的距离等于flex items之间的距离space-around :flex items 之间的距离相等,flex items与cross start 、cross end 之间的距离等于flex items之间的距离的一半

6 flex-flow 是flex-direction与flex-wrap的简写

也就是说,当你使用这个属性的时候,你可以使用上述两个的属性值,例如:flex-flow: row wrap;(水平排列,多行显示)

flex 项目属性(item属性)

orderflex-growflex-shrinkflex-basisalign-selfflex

1 order

 order 决定flex items的排布顺序  (用的不多)可以设置为任意整数(正整数、负整数、0),值越小越排在前面默认值为0

这个属性了解即可,说实话没怎么用过

2 align-self

 可以通过align-self 覆盖flex container 设置的align-itemsauto(默认值):遵从flex container的align-items设置stretch、flex-start、flex-end、center、baseline效果与align-items一致

相当于继承父元素的align-items属性,如果没有父元素,则等同于stretch。

3 flex-grow

决定了flex items如何扩展可以设置为任意非父数字(小数,整数 0),默认为0当flex container 在main axis方向上有剩余得size时,flex-grow属性才会有效如果所有flex items 的flex-grow 综合sum不超过1,这直接乘以剩余size就是扩展大小、如果超过1 扩展size=剩余size*flex-grow/sum

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。

4 flex-shrink

flex-shrink (shrink 缩小,收缩)与flex-grow相似,一个扩展,一个伸缩 可以设置为任意非父数字(小数,整数 0),默认为1当flex items在main axis 方向上超过了flex container 的size flex-shrink属性才会生效、如果所有flex items 的flex-shrink 总和sum超过1,每个flex item 收缩的size为:flex item 超出flex container 的size*收缩比例/每个flex items 的收缩比例之和如果sum不超过1,每个flex item 收缩的size为:size = 超出的size * flex-shrink值flex items收缩后的最终size不能小于min-widthmin-height

有扩大自然就会有缩小,flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。具体的可以自己动手尝试一下哦,最后将会给出一个骰子布局的案例!

5 flex-basis

用来设置flex items 在 main axis方向上的base size默认为auto,可以设置具体的宽度数值决定flex items最终base size 的因素,优先级从高到低max-widthmax-heightmin-widthmin-heightflex-basiswidthheight内容本身的size

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目(item)的本来大小。也可以设置跟width,height一样的宽高,表示item将占据固定的空间!

6 flex

flex 是flex-grow || flex-shink||flex-basis的简写可以指定1 2 3个值 依次按照上述顺序!默认值为 0 1 auto
.item {  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]}

注意:

该属性的默认值为 0 1 auto(注意顺序),后两个属性可选
该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
如果需要这三个属性的时候,建议使用flex,而不是单独的三个分离的属性,因为浏览器会推算相关值

骰子布局实践

光说不练假把式,手撕代码真功夫!
下面利用flex写了几个骰子布局,可以参考一下!

<!DOCTYPE html><html><head><meta charset="utf-8"><title></title><style type="text/css">#container{background-color: #CCCCCC;height: 600px;width: 500px;/* flex */display: flex;justify-content: space-evenly;align-items: center;}.item{background-color: yellow;width: 100px;height: 100px;}/* 单点 */.one{/* 对点使用flex布局 */display: flex;justify-content: center;align-items: center;}/* 点 */.item-one{display: block;height: 20px;width: 20px;background-color: #1890FF;border-radius: 50%;}/* 三点 */.two{display: flex;justify-content: space-between;}.two span{margin: 2px;display: block;height: 20px;width: 20px;border-radius: 50%;background-color: #1890FF;}.two2{align-self: center;}.two3{align-self: flex-end;}/* 五点 */.three{display: flex;justify-content: space-around;}.three span{display: block;height: 20px;width: 20px;border-radius: 50%;background-color: #1890FF;}#three_one, #three_three{padding: 2px;display: flex;flex-direction: column;justify-content: space-between;}#three_two{display: flex;flex-direction: column;justify-content: center;}/* 六点 */.four{display: flex;justify-content: space-around;}.four span{display: block;height: 20px;width: 20px;border-radius: 50%;background-color: #1890FF;}#four1,#four2{padding: 2px;display: flex;flex-direction: column;justify-content: space-between;}</style></head><body><div id="container"><!-- 一个点居中 --><div class="item one"><span class="item-one"></span></div><!-- 三点 --><div class="item two"><span class="two1"></span><span class="two2"></span><span class="two3"></span></div><!-- 五点 --><div class="item three"><div id="three_one"><span></span><span></span></div><div id="three_two"><span></span></div><div id="three_three"><span></span><span></span></div></div><!-- 六点 --><div class="item four"><div id="four1"><span></span><span></span><span></span></div><div id="four2"><span></span><span></span><span></span></div></div></div></body></html>

测试结果

flex弹性布局详解