CSS Flex中12个属性的总结

本文深入解析Flex布局,涵盖justify-content、align-items等属性,详细解释元素在容器中的对齐、排序及尺寸调整策略,助您掌握响应式网页设计的核心。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

justify-content  定义元素沿主轴的对齐方式

  • flex-start(默认值)
  • flex-end
  • center
  • space-between
  • space-around

部分主要css代码如下:

.justify-content{
    display: flex;
    justify-content: center;
    /*align-items: flex-end;*/
    width: 500px;
    /*height: 100px;*/
    margin: auto;
    background: black;
}
.justify-content p{
    /*margin: 0;*/
    /*padding: 0;*/
    width: 100px;
    height: 40px;
    line-height: 40px;
    text-align: center;
}
.justify-content p.block1{
    background: blue;
}
.justify-content p.block2{
    background: gold;
}
.justify-content p.block3{
    background: hotpink;
}

flex-start

flex-end

center

space-between

space-around

align-items  定义子容器在Flex容器中按照交叉轴排列的方式

  • flex-start
  • flex-end
  • center
  • baseline 项目的第一行文字的基线对齐
  • stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度

css--baseline代码如下:

.justify-content{
    display: flex;
    /*justify-content: center;*/
    align-items: baseline;
    width: 500px;
    height: 100px;
    margin: auto;
    background: black;
}
.justify-content p{
    margin: 0;
    padding: 0;
    width: 100px;
    height: 40px;
    line-height: 40px;
    text-align: center;
}
.justify-content p.block1{
    background: blue;
}
.justify-content p.block2{
    background: gold;
    height:20px;
}
.justify-content p.block3{
    background: hotpink;
    height:60px;
}

baseline

stretch

flex-start

flex-end

center

 

align-self  定义了自身元素在Flex容器中按照交叉轴排列对齐的方式,可覆盖align-item属性

  • auto  表示继承父元素的属性(默认值)
  • flex-start
  • flex-end
  • center
  • baseline
  • stretch

flex-direction 定义了元素在flexbox容器中的排序方式

  • row(默认值)
  • row-reverse
  • column
  • column-reverse

css

.justify-content{
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column-reverse;
    width: 500px;
    height: 160px;
    margin: auto;
    background: black;
}

row

row-reverse

column

clolumn-reverse

flex-basis  定义了弹性盒子模型flexbox的初始化大小

  • auto(默认值)
  • 具体值

css

.justify-content{
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: row;
    flex-basis: 400px;
    /*width: 500px;*/
    height: 160px;
    margin: auto;
    background: black;
}

flex-wrap 定义了元素在flexbox容器中是显示一行还是多行

  • nowrap(默认值)
  • wrap
  • wrap-reverse

容器css代码

.total{
    display: flex;
    width: 600px;
}
.justify-content{
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: row;
    flex-wrap: wrap-reverse;
    flex-basis: 400px;
    /*width: 500px;*/
    height: 160px;
    margin: auto;
    background: black;
}

nowrap

wrap

wrap-reverse

align-content 定义了flex容器中每一行的对齐方式

  • strecch(默认值)
  • flex-start
  • flex-end
  • center
  • space-between
  • space-around

flex容器css

.justify-content{
    display: flex;
    /*justify-content: center;*/
    /*align-items: center;*/
    /*flex-direction: row;*/
    flex-wrap: wrap;
    flex-basis: 400px;
    align-content: flex-end;
    /*width: 500px;*/
    height: 160px;
    margin: auto;
    background: black;
}

flex-start

flex-end

stretch

space-between

space-around

flex-grow 定义了当元素有其他可用空间时如何填充(int类型的值)

  • 0(默认值)

0

1

flex-shrink 定义了当元素没有其他可用空间时如何压缩(int类型的值)

  • 1(默认值)

css容器css

.justify-content{
     display: flex;
     /*justify-content: center;*/
     /*align-items: center;*/
     /*flex-direction: row;*/
     /*flex-wrap: wrap;*/
     /*flex-basis: 400px;*/
     align-content: flex-start;
    flex-grow: 2;

     width: 500px;
     height: 160px;
     margin: auto;
     background: black;
 }
.justify-content p{
    margin: 0;
    padding: 0;
    width: 100px;
    height: 40px;
    line-height: 40px;
    text-align: center;
}
.justify-content p.block1{
    background: blue;
}
.justify-content p.block2{
    flex-shrink: 22;
    background: gold;
}
.justify-content p.block3{
    background: hotpink;
}

22

order 定义元素在容器中排序位置

  • 0(默认值)   数值约小越靠前

子项css

.justify-content p.block1{
    background: blue;
    order:1;
}
.justify-content p.block2{
    flex-shrink: 22;
    order:0;
    background: gold;
}
.justify-content p.block3{
    background: hotpink;
    order: 3;
}

 

flex-flow 是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap

flexflex-growflex-shrink 和 flex-basis的简写,默认值为0 1 auto

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值