Flex布局

在这里插入图片描述

1.1 flex的flex-direction属性

flex-direction属性决定主轴的方向(即项目的排列方向)。

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

它可能有4个值。

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

1.2 flex的align-items属性

align-items属性定义项目在交叉轴上如何对齐。

.box {
  align-items: flex-start | flex-end | center | baseline | stretch;
}

它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。

  • flex-start:交叉轴的起点对齐。
    在这里插入图片描述
  • flex-end:交叉轴的终点对齐。
    在这里插入图片描述
  • center:交叉轴的中点对齐。
    在这里插入图片描述
  • baseline: 项目的第一行文字的基线对齐。
    在这里插入图片描述
    stretch(默认值):如果项目未设置高度或设为auto将占满整个容器的高度。
    在这里插入图片描述

1.3 flex的justify-content属性

justify-content属性定义了项目在主轴上的对齐方式

.box {
  justify-content: flex-start | flex-end | center | space-between | space-around;
}

它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。

  • flex-start(默认值):左对齐
    在这里插入图片描述
  • flex-end:右对齐
    在这里插入图片描述
  • center: 居中
    在这里插入图片描述
  • space-between:两端对齐,项目之间的间隔都相等。
    在这里插入图片描述
  • space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
    在这里插入图片描述

1.4 flex的align-self属性

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。

.item {
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

在这里插入图片描述

	<div class="container boxs py-3">
      <div class="box1">
        <div class="box-inner">
          <div class="circle"></div>
          <div class="circle"></div>
          <div class="circle"></div>
        </div>
      </div>
    </div>
.boxs {
  background: #292929;
  margin: 100px auto;
  padding: 10px;
  width: 200px;
  display: flex;
  justify-content: center;
  .box1 {
    width: 150px;
    height: 150px;
    background-color: #fff;
    border-radius: 15px;
    border-bottom: 20px solid #bbbbbb;
    padding-top: 10px;
    .box-inner {
      height: 130px;
      box-sizing: border-box;
      background: #e7e7e7;
      border-radius: inherit;
      border-top: 0px;
      border-left: 10px solid #d7d7d7;
      border-right: 10px solid #d7d7d7;
      padding: 10px;
      /* here display flex */
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .circle {
        width: 34px;
        height: 34px;
        border-radius: 17px;
        background-color: #333333;
        border-bottom: 5px solid #555555;
      }
      & > :nth-child(2) {
        align-self: center;  /* here display flex */
      }
      & > :nth-child(3) {
        align-self: flex-end;  /* here display flex */
      }
    }
  }
}

1.6 flex的flex-warp属性

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

.box{
  flex-wrap: nowrap | wrap | wrap-reverse;
}

它可能取三个值。

(1)nowrap(默认):不换行。

(2)wrap:换行,第一行在上方。

(3)wrap-reverse:换行,第一行在下方。

1.7 flex的align-content属性

align-content属性定义了多根轴线的对齐方式(flex-wrap:wrap 且元素已换行的情况下)。如果项目只有一根轴线,该属性不起作用。

.box {
  align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}

该属性可能取6个值。

  • flex-start:与交叉轴的起点对齐。
  • flex-end:与交叉轴的终点对齐。
  • center:与交叉轴的中点对齐。
  • space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
  • space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
  • stretch(默认值):轴线占满整个交叉轴。

在这里插入图片描述

 <div class="box-inner">
          <div class="circle"></div>
          <div class="circle"></div>
          <div class="circle"></div>
          <div class="circle"></div>
          <div class="circle"></div>
          <div class="circle"></div>
 </div>
.box-inner {
	  .....,
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      align-content: space-between;
    }

两个元素换行的可以把这两个元素放入一个div中,即可wrap
在这里插入图片描述

 	<div class="box-inner">
          <div class="row">
            <div class="circle"></div>
            <div class="circle"></div>
          </div>
          <div class="row">
            <div class="circle"></div>
            <div class="circle"></div>
          </div>
    </div>
   .box-inner {
   	 .....,
      display: flex;
      flex-wrap: wrap;
      align-content: space-between;
      .row {
        flex-basis: 100%;
        display: flex;
        justify-content: space-between;
      }
    }

1.8 flex的flex-basis属性

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)
浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。

.item {
  flex-basis: <length> | auto; /* default auto */
}

它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间

    <div class="container flx2">
      <div>
        <h3>Box One</h3>
        <p>
          Flexbox CSS In 20 minutes.display:flex flex order flex-direction
          justify-content flex-basis align-items
        </p>
      </div>
      <div>
        <h3>Box Two</h3>
        <p>
          In this quick video we will go over the CSS Flexbox model. This is a
          quick overview, not an in-depth course. We will look at the basics
          such as
        </p>
      </div>
      <div>
        <h3>Box Three</h3>
        <p>Flexbox CSS In 20 minutes</p>
      </div>
    </div>
.flx2 {
  display: flex;
  margin-top: 50px;
  > div {
    border: 1px solid #ccc;
    padding: 10px;
  }
  & > :nth-child(3) {
    flex-basis: 80%;
  }
  & > :nth-child(1) {
    flex-basis: 10%;
  }
  & > :nth-child(2) {
    flex-basis: 10%;
  }
}

在这里插入图片描述

.flx2 {
  display: flex;
  margin-top: 50px;
  justify-content: space-around;
  > div {
    border: 1px solid #ccc;
    padding: 10px;
    flex-basis: 20%;
  }
}

在这里插入图片描述

1.9 flex的order属性

order属性定义项目的排列顺序。数值越小,排列越靠前默认为0

.flx {
  display: flex;
  justify-content: space-between;
  > div {
    border: 1px solid #ccc;
    padding: 10px;
  }
  & > :nth-child(1) {
    flex: 1;
    order: 1;
  }
  & > :nth-child(3) {
    flex: 2;
    order: 2;
  }
  & > :nth-child(2) {
    flex: 1;
    order: 3;
  }
}
    <div class="container flx">
      <div>
        <h3>Box One</h3>
        <p>
          Flexbox CSS In 20 minutes.display:flex flex order flex-direction
          justify-content flex-basis align-items
        </p>
      </div>
      <div>
        <h3>Box Two</h3>
        <p>
          In this quick video we will go over the CSS Flexbox model. This is a
          quick overview, not an in-depth course. We will look at the basics
          such as
        </p>
      </div>
      <div>
        <h3>Box Three</h3>
        <p>Flexbox CSS In 20 minutes</p>
      </div>
    </div>

在这里插入图片描述

参考 http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值