登录
原创

flex弹性盒模型

发布于 2020-11-18 阅读 606
  • CSS
原创

flex属性

 #box1 {
            background-color:yellow;
            height:100px;
            display: flex;
            justify-content: space-between;
        }
        #box1 p {
            width: 200px;
        }
        #box2 {
            background-color: gray;
        }
        #box3 {
            background-color: green;
        }
        #box4 {
            background-color: hotpink;
        }
   <div id="box1">
      <p id="box2">1</p>
      <p id="box3">2</p>
      <p id="box4">3</p>
   </div>

上面代码:
三个元素设置大小及背景色,在父容器中添加flex。

  • display:flex属性可以把块级元素在一排显示
  • flex需要添加在父元素上,改变子元素的排列顺序
  • 默认为从左往右依次排列,且和父元素左边没有间隙
    ab7dbf957fa5bae647175003285f2ed.png

横轴排列方式

  • justify-content:flex-start
    交叉轴的七点对齐
    ab7dbf957fa5bae647175003285f2ed.png
  • flex-end
    右对齐
    b259ca55bb6a8e5e41e40453e825902.png
  • center
    居中
    ee9c624850f087211b9b3b6eb1e6026.png
  • space-between
    两端对齐,项目之间的间隔都相等
    0e6c1d7e1bf9fd6dbfb3ca97f3df578.png
  • space-around
    每个项目两侧的间隔相等,项目之间的间隔比项目与边框的间隔大一倍
    986052ed30ff9166879fcd036b7c19c.png

竖轴排列方式

#box1 {
            background-color:yellow;
            height:500px;
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
        }
  • align-items:flex-start
    默认值,左对齐
    a3f4b7f3fbe76ea20b7d303eb796c6c.png
  • flex-end
    交叉轴的终点对齐
    8aba1da59a209f9f47116cbbe59abe7.png
  • center
    交叉轴的中点对齐
    32f1358c8c58d2228402a18709a1683.png
  • baseline
    项目的第一行文字的基线对齐
.box2 {
        font-size: 30px;
    }

.box3 {
        font-size: 50px;
    }

15788422784cd9cf883b85ff9fec331.png

  • stretch
    如果项目未设置高度或设为auto,将占满整个容器的高度(P标签自带样式,没有整个占满,换成div可以占满)
#box1 {
          background-color:yellow;
          height:500px;
          display: flex;
          justify-content: space-between;
          align-items: stretch;
      }
#box1 p {
          width: 200px;
          /*不设置高度*/
        }
#box2 {
          background-color: gray;
      }
#box3 {
          background-color: green;    
      }
#box4 {
          background-color: hotpink;      
      }

b31fbf4e8bcb77313fdc8f3c2fc8ae5.png

给子元素设置flex占比

 #box1 {
          background-color:yellow;
          height:100px;
          display: flex;
          align-items: stretch;
      }
#box1 p {
          width: 200px;
        }
#box2 {
          background-color: gray;
          flex:1;
      }
#box3 {
          background-color: green;
          flex:2;
      }
#box4 {
          background-color: hotpink;
          flex:3;
      }

b8e07d632c1f2dccade493e1c1f03a6.png

评论区

励志做一条安静的咸鱼,从此走上人生巅峰。

0

0

0

举报