CSS3 弹性布局display:flex详解

弹性布局是CSS3新增的一种布局思想,他将盒子分为容器与项目

容器:最外层盒子设置display:flex,容器设置弹性布局后,会清除容器内的浮动布局

项目:容器下的子盒子,!注意不包括项目下的内容,但项目也可设置为容器

  <style>
    *{
      margin: 0;
      padding: 0;
    }
    /* 此时main为容器 */
    main{
      width: 100%;
      height: 400px;
      display: flex;
    }
    /* section为项目 */
    section{
      width: 200px;
      height: 200px;
      background-color: red;
      border: 1px solid blue;
    }
  </style>
</head>
<body>
  <main>
    <section>
      <!-- div为项目下的内容 -->
      <div></div>
    </section>
    <section></section>
    <section></section>
  </main>
</body>

 设置弹性布局后,沿着容器的水平方向会存在一条轴线为主轴,与其对应垂直方向会存在一条交叉轴,项目默认为上图的主轴进行排列。

容器属性:justify-content和align-items分别为主轴和交叉轴排布样式

    /* 此时main为容器 */
    main{
      width: 100%;
      height: 400px;
      display: flex;
      /* 主轴靠右对齐 */
      justify-content: flex-end;
      /* 主轴居中对齐 */
      justify-content: center;
      /* 主轴左右两端对齐 */
      justify-content: space-between;
      /* 主轴对齐,项目间的间距为两端的两倍 */
      justify-content: space-around;
      /* 主轴对齐,项目间的间距与两端间距相等 */
      justify-content: space-evenly;
    }
    /* section为项目 */
    section{
      width: 200px;
      height: 200px;
      background-color: red;
      border: 1px solid blue;
    }
  </style>
</head>
<body>
  <main>
    <section></section>
    <section></section>
    <section></section>
  </main>

主轴靠右对齐      justify-content: flex-end;

                                             主轴居中对齐         justify-content: center;

                                                  主轴左右两端对齐      justify-content: space-between;

主轴对齐,项目间的间距为两端的两倍       justify-content: space-around; 

               主轴对齐,项目间的间距与两端间距相等       justify-content: space-evenly;

 

 align-items交叉轴属性

/* 此时main为容器 */
    main{
      width: 100%;
      height: 400px;
      display: flex;
      /* 交叉轴居中对齐 */
      align-items: center;
      /* 交叉轴底部对齐 */
      align-items: flex-end;
      border: 5px solid rebeccapurple;
    }

 常用  justify-content: center; align-items: center;作为文本上下左右居中对齐

 

/* 此时main为容器 */
    main{
      width: 100%;
      height: 400px;
      display: flex;
      /* 轴线旋转 */
      flex-direction: column;
      /* 镜像翻转 */
      flex-direction: row-reverse;
      /* 轴线旋转加镜像翻转 */
      flex-direction: column-reverse;
      border: 5px solid rebeccapurple;
    }
    /* section为项目 */
    section{
      width: 200px;
      height: 200px;
      background-color: red;
      border: 1px solid blue;
    }
  </style>
</head>
<body>
  <main>
    <section>1</section>
    <section>2</section>
    <section>3</section>
  </main>

                              轴线旋转 flex-direction: column此时主轴与交叉轴旋转互换

镜像翻转  flex-direction: row-reverse;反向排布

 

轴线旋转加镜像翻转 flex-direction: column-reverse;

 

 /* 此时main为容器 */
    main{
      width: 500px;
      height: 400px;
      display: flex;
      /* 默认,不换行 */
      flex-wrap: nowrap;
      /* 允许换行 */
      flex-wrap: wrap;
      margin: 50px auto;
      border: 5px solid rebeccapurple;
    }

 默认属性,不换行 flex-wrap: nowrap;当容器宽度比里面项目宽度小的时候,项目会等比缩放而不会换行显示

                                                允许换行  flex-wrap: wrap; 

 项目属性

    section:nth-of-type(1){
      order: 2;
    }
    section:nth-of-type(2){
      order: 3;
    }
    section:nth-of-type(3){
      order: 1;
    }

order属性后面跟数字,数字越大,项目越靠前排列

 

    section:nth-of-type(1){
      order: 1;
    }
    section:nth-of-type(2){
      order: 2;
      /* 居中 */
      align-self: center;
      /* 靠底部 */
      align-self: flex-end;
      /* 靠顶部 */
      align-self: flex-start;
    }
    section:nth-of-type(3){
      order: 3;
    }

align-self单个项目样式设定center为居中

 

    section:nth-of-type(1){
      width: 200px;
    }
    section:nth-of-type(2){
      flex-grow: 1;
    }
    section:nth-of-type(3){
      flex-grow: 4;
    }

flex-grow占当前同行剩余空间的等份数,如果容器没有设置固定的宽度,则设置了该属性的项目会随着容器宽度改变而改变,体现出了弹性盒子的“弹性”

 

 /* 此时main为容器 */
    main{
      width: 500px;
      height: 400px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 50px auto;
      border: 5px solid rebeccapurple;
    }
    /* section为项目 */
    section{
      width: 200px;
      height: 200px;
      background-color: red;
      border: 1px solid blue;
    }
    section:nth-of-type(1){
      flex-shrink: 0;
    }
    section:nth-of-type(2){
      flex-shrink: 1;
    }
    section:nth-of-type(3){
      flex-shrink: 0;
    }

flex-shrink为容器不足的情况下,该项目是否等比缩小0为不允许等比缩小,1为默认值,允许等比缩小

 

    /* section为项目 */
    section{
      width: 200px;
      height: 200px;
      background-color: red;
      border: 1px solid blue;
    }
    section:nth-of-type(1){
      flex-basis: 100px;
    }
    section:nth-of-type(2){
      flex-basis: 300px;
    }
    section:nth-of-type(3){
      flex-basis: 50px;
    }

flex-basis为项目宽度,比width权重高

 

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
display:flexCSS中的一个属性,用于设置一个容器元素(flex container)为弹性布局容器。该属性能够改变元素的默认布局行为,使其具有弹性布局的特性。 flex容器内部的子元素(flex items)可以通过设置不同的flex属性来决定它们在容器内的分布和对齐方式。这些flex属性包括: 1. flex-direction:决定flex items在容器内的排列方向。它可以取四个值,分别是row、row-reverse、column和column-reverse。默认值是row,表示水平方向排列。 2. flex-wrap:当flex items在容器内的空间不足时,决定是否换行显示。它可以取五个值,分别是nowrap、wrap、wrap-reverse、initial和inherit。默认值是nowrap,表示不换行。 3. flex-flow:是flex-direction和flex-wrap的缩写,可以同时设置这两个属性的值。 4. justify-content:决定flex items在主轴上的对齐方式。它可以取五个值,分别是flex-start、flex-end、center、space-between和space-around。 5. align-items:决定flex items在交叉轴上的对齐方式。它可以取六个值,分别是flex-start、flex-end、center、baseline、stretch和inherit。 6. align-content:当flex items有多行时,决定它们在交叉轴上的对齐方式。它可以取六个值,分别是flex-start、flex-end、center、space-between、space-around和stretch。 以上是display:flex的一些常用属性,通过灵活地设置这些属性,可以实现各种复杂的布局效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [详解CSS中的display:flex||inline-flex属性](https://download.csdn.net/download/weixin_38501751/12891643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [display:flex详解](https://blog.csdn.net/Anony_me/article/details/125524220)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值