30min学会flex布局

337 阅读5分钟

前言

flex自打问世以来。每次使用我都觉着我自己像个idiot,今天整理下flex布局

正文

了解flex布局

采用flex布局的元素称为容器,而且他的子元素自动称为容器成员。简称项目

容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。

容器的属性

容器上有6个属性,分别为:

  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

flex-direction属性

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

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

flex-direction: row; (默认)主轴为水平方向,起点在左端

flex-direction: row-reverse; 主轴为水平方向,起点在右端。 flex-direction: column; 主轴为垂直方向,起点在上沿。 flex-direction: column-reverse; 主轴为垂直方向,起点在下沿。

flex-wrap属性

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

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

flex-wrap: nowrap; (默认)不换行 flex-wrap: wrap; 换行,第一行在上方 flex-wrap: wrap-reverse; 换行,第一行在下方

flex-flow属性

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

.box {
  flex-flow: flex-direction || flex-wrap;
}

flex-flow: row-reverse wrap-reverse

justify-content属性

justify-content 决定了项目在水平主轴上的对齐方式.有五个不同的值

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

justify-content: flex-start;(默认) 左对齐 justify-content: flex-end; 右对齐 justify-content: center; 居中 justify-content: space-between; 两端对齐,项目之间的间隔都相等 justify-content: space-around; 每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

align-items属性

定义项目在交叉轴上如何对齐

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

align-items: flex-start; 交叉轴起点对齐 align-items: flex-end; 交叉轴终点对齐 align-items: center; 交叉轴中点对齐 align-items: baseline; 项目的第一行文字的基线对齐。 align-items: stretch; (默认值)如果项目未设置高度或设为auto,将占满整个容器的高度。

align-content属性

定义了多根x轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

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

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

项目的属性

项目上有六个属性

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self

order属性

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

.item {
  order: integer;
}

给最后一个项目 order:1

flex-grow属性

定义项目的放大比例。默认为0,即如果存在剩余空间也不放大。

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

.item {
  flex-grow: number ; /* default 0 */
}

奇数项设置为1 偶数项设置为2. list是一个0-9的数组

<li
  class="flex-item"
  v-for="item in list"
  :key="item"
  :style="{flexGrow: item %2 ===1 ? 1: 2}">{{item}}
</li>

flex-shrink属性

定义项目的缩小比例。默认为1,即如果存在剩余空间不足该项目将缩小。 如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。

负值对该属性无效。

.item {
  flex-shrink: number ; /* default 1 */
}

第一个元素设为0 其余的为1。 list是一个0-9的数组

<li
  class="flex-item"
  v-for="item in list"
  :key="item"
  :style="{flexShrink: item === 0 ? 0: 1}">{{item}}
</li>

flex-basis属性

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

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

第一项设置为100px 其余为auto。 list是一个0-9的数组

<li
  class="flex-item"
  v-for="item in list"
  :key="item"
  :style="{flexBasis: item === 0 ? '100px': 'auto'}">{{item}}
</li>

flex属性

flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

.item {
  flex: none |  'flex-grow' ['flex-shrink' || 'flex-basis']
}

该属性有三个快捷值:auto (1 1 auto) 、 none (0 0 auto) 和 1 (1 1 0%) 建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。 flex:1 常用于布局。左侧宽度固定,右侧占剩余全部

align-self属性

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

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

第六个元素 设置为flex-end

<li
  class="flex-item"
  v-for="item in list"
  :key="item"
  :style="{alignSelf: item===5? 'flex-end': 'auto',height: `${Math.random()*50 + 50}px`}">{{item}}
</li>

常见面试题

使用flex实现一个三点的色子

<ul class="saizi_box">
  <li class="saizi">1</li>
  <li class="saizi">2</li>
  <li class="saizi">3</li>
</ul>
<style>
.saizi_box{
  list-style: none;
  width:300px;
  height:300px;
  border: 1px solid #ddd;
  border-radius: 10px;
  display: flex;
  justify-content: space-between;
  padding:20px;
  .saizi{
    width:50px;
    height:50px;
    background: #ddd;
    color:#333;
    border-radius: 10px;
    text-align: center;
    line-height: 50px;
    &:nth-child(2){
      align-self: center;
    }
    &:nth-child(3){
       align-self: flex-end;
    }
  }
}
</style>