重学flex布局

165 阅读3分钟

flex容器属性

改变主轴方向flex-direction

flex-direction: row默认

image-20220212214719022

flex-direction: row-reverse

image-20220212214727609

flex-direction: column

image-20220212214732803

flex-direction: column-reverse

image-20220212214739075

换行flex-wrap

flex-wrap: nowrap默认

image-20220212214747177

flex-wrap: wrap

img

flex-wrap: wrap-reverse

image-20220212214752405

缩写flex-flow: [flex-direction] [flex-wrap]

flex-flow: column wrap

image-20220212214757643

主轴对齐justify-content

justify-content: flex-start默认

img

justify-content: flex-end

image-20220212214802306

justify-content: space-around 平均分配 每个方块的margin-left+margin-right+width相等

image-20220212214807733

justify-content: space-between 两边没有空隙 中间空隙平均分配

image-20220212214812645

justify-content: space-evenly所有空隙平均分配

image-20220212214819677

交叉轴整体对齐align-content

必须要有折行属性才能生效

align-content: stretch默认

  • 如果交叉轴上的宽度未设置则自动拉伸填满交叉轴

  • image-20220212214825843

  • 若交叉轴上的宽度已经设置则效果和flex-start一样

  • image-20220212214830873

align-content: flex-start

image-20220212214835653

align-content: flex-end

image-20220212214840206

align-content: center

image-20220212214844914

其他属性 space-around``space-between``space-evenly和主轴属性类似

交叉轴每一行对齐 align-items

align-items: stretch默认

align-items: flex-start

image-20220212214851355

align-items: flex-end

image-20220212214856140

align-items: center

image-20220212214900901

align-items: baseline 内容以小写x为基线对齐

image-20220212214905631

内联与块的上下左右居中布局

内联上下左右居中

image-20220212214909804

块级上下左右居中

image-20220212214915118

不定项居中布局

image-20220212214919862

均分列布局

image-20220212214924984

子项分组布局

复杂模式 使用div嵌套

image-20220212214930509

简单方式 margin-right: auto

image-20220212214936331

flex子项属性

扩展比例flex-grow

一个子元素时

  • 默认值为0

  • 比例值大于等于1,沾满剩余所有空间

  • 比例值为0.5,占剩余空间的一半

image-20220212214944988

image-20220212214950098

多个子元素时

  • 只有一个有flex-grow时

image-20220212214954991

  • 两个都有flex-grow时

image-20220212214959906

  • 当多个元素加起来小于1时,还有剩余空间

image-20220212215004740

收缩比例flex-shrink

  • 默认值为1,溢出部分完全收缩,小数按比例收缩

image-20220212215009431

默认情况当有两个及以上需要收缩时

  • mainwidth: 400

  • box1width: 200

  • box2width: 300

  • 则,默认情况下收缩后 所占比例按照宽度计算

  • box1收缩后所占尺寸:200-2/5*(200+300-400)=160

  • box2收缩后所占尺寸:300-3/5*(200+300-400)=240

image-20220212215014237

不同比例收缩时

  • mainwidth: 400

  • box1width: 200

  • box2width: 300

  • 则,比例情况下收缩后 所占比例按照宽度计算

  • box1收缩后所占尺寸:200-4/7*(200+300-400)=142

  • box2收缩后所占尺寸:300-3/7*(200+300-400)=257

image-20220212215019585

指定flex元素在主轴上的初始大小flex-basis

  • 当主轴方向是水平时,覆盖水平宽度

  • 当主轴方向是垂直时,覆盖垂直高度

  • 可选值:0% auto 200px 100% 0

image-20220212215024413

flex-basis: auto默认值

image-20220212215029405

flex-basis: 0表示占据最小宽度,会竖起来

image-20220212215034262

flex缩写

flex: 1

image-20220212215039444

flex: 0

image-20220212215044185

flex: auto

image-20220212215048190

改变某个子项的排序位置order

  • order: 0当前位置保持不变

  • order: -1向前排

  • order: 1向后拍

image-20220212215052972

控制单独某一个元素交叉轴的布局align-self

image-20220212215057472

等高布局 内容填充两边也等高

image-20220212215102252

两列或三列布局 两边固定宽度 中间自适应

image-20220212215107507

Sticky Footer布局 内容空页脚在最底部 内容满也在最底部

image-20220212215116299

溢出项布局

image-20220212215123298

image-20220212215128574