W3C flex学习笔记

本文参考的视频来自b站思学堂前端教程的前端知识粉碎机系列:

【前端教程】我是flex布局,6分钟彻底懂我、了解我?_哔哩哔哩_bilibili

【前端教程】我是flex,你真的懂我,了解我吗?_哔哩哔哩_bilibili

在最开头,原视频就有一个非常精妙的比喻,“我”的成长史是一部权力的游戏,这实际上是说flex对于内部元素的控制在不断加强。

整体方向单列 / 多列整体布局,行列对齐方向对单个元素的控制

目录

1.flex概述

2.布局方向 / flex-direction属性

3.换行 / flex-wrap

4.对齐方式 / justify-content & align-items & align-content

4.1.justify-content

4.2.align-items

4.3.align-content

5.单独控制元素

5.1.排序 / order

5.2.缩放 / flex-grow & flex-shrink

5.3.位置 / align-self


1.flex概述

布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性。它对于那些特殊布局,比如,垂直居中就不容易实现。

flex旨在提供一种更有效的方式来布局、对齐和分配容器中项目之间的空间,即使它们的大小未知或动态变化.

Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。

任何一个容器都可以指定为Flex布局。

.box{
  display: flex;
}

行内元素也可以使用Flex布局。

.box{
  display: inline-flex;
}

2.布局方向 / flex-direction属性

flex-direction属性决定主轴,也就是项目排列的方向,有4个值:row,row-reverse,column和column-reverse。

<div class="box">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

<style>
  .item{
    width: 100px;
    background-color: lightgray;
    height: 100px;
    margin: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>

图1.未添加flex的div块

未添加flex的时候,div作为最典型的块元素,自己独占一行。

2.1.row

靠左水平布局,自左向右

  .box{
    display: flex;
    flex-direction: row;
  }

图2.flex-direction: row

2.2.row-reverse

靠右水平布局,自右向左

  .box{
    display: flex;
    flex-direction: row-reverse;
  }

图3.flex-direction: row-reverse

2.3.column

垂直分布,贴页面顶部自上往下

  .box{
    display: flex;
    flex-direction: column;
  }

图4.flex-direction: column

2.4.column-reverse

垂直分布,自下往上,看上去是贴页面顶部,实际上是因为容器的高度等于div块高度之和。

    图5.flex-direction: column-reverse

设置容器的长度为页面长度,可以看到块从页面底部,从下到上

  .box{
    display: flex;
    flex-direction: column-reverse;
    height: 100vh;
  }

图6.flex-direction: column-reverse

3.换行 / flex-wrap

3.1.nowrap

不换行,每个div的宽度都被压缩了,强制显示在一行内

  .box{
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
  }

图7.flex-wrap: nowrap

3.2.wrap

换行

  .box{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
  }

图8.flex-wrap: wrap

3.3.wrap-reverse属性

反向换行

  .box{
    display: flex;
    flex-direction: row;
    height: 100vh;
    flex-wrap: wrap-reverse;
  }

图9.flex-wrap: wrap-reverse

4.对齐方式 / justify-content & align-items & align-content

图10.方向

4.1.justify-content

        4.1.1.flex-start

  .box{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
  }

        

图10.justify-content: flex-start

        4.1.2.flex-end

  .box{
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
  }

图10.justify-content: flex-end

        4.1.3.center

  .box{
    display: flex;
    flex-direction: row;
    justify-content: center;
  }

图11.justify-content: center

        4.1.4.space-between

  .box{
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }

图12.justify-content: space-between

        4.1.5.space-around

  .box{
    display: flex;
    flex-direction: row;
    justify-content: space-around;
  }

图13.justify-content: space-around

4.2.align-items

        4.2.1.flex-start

  .box{
    display: flex;
    flex-direction: row;
    align-items: flex-start;
  }

图14.align-items: flex-start

         4.2.2.flex-end:

  .box{
    display: flex;
    flex-direction: row;
    height: 100vh;
    align-items: flex-end;
  }

图15.align-items: flex-end

        4.2.3.center

  .box{
    display: flex;
    flex-direction: row;
    height: 100vh;
    align-items: center;
  }

 

图16.align-items: flex-center

        4.2.4.baseline

以第一行文字为基准线进行排列

<div class="box">
  <div class="item" style="height:100px">1</div>
  <div class="item" style="height:300px">2</div>
  <div class="item" style="height:200px">3<br/>3</div>

</div>

<style>
  .item{
    width: 100px;
    background-color: lightgray;
    /* height: 100px; */
    margin: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .box{
    display: flex;
    flex-direction: row;
    height: 100vh;
    align-items: baseline;
  }
</style>

图17.align-items: baseline

        4.2.5.stretch

      某个元素没有设置高度的时候自动撑满。

      注意先删除第二个div块高度的样式

<div class="box">
  <div class="item" style="height:100px">1</div>
  <div class="item">2</div>
  <div class="item" style="height:200px">3<br/>3</div>
</div>
  .box{
    display: flex;
    flex-direction: row;
    height: 100vh;
    align-items: stretch;
  }

图18.align-items: stretch

4.3.align-content

justify-content和align-content组成行与列之间的排列分布

        4.3.1.flex-start

图19.align-content: flex-start

        4.3.2.flex-end

  .box{
    display: flex;
    flex-direction: row;
    width:1000px;
    height: 100vh;
    flex-wrap: wrap;
    align-content: flex-end;
  }

图20.align-content: flex-start

        4.3.3.center

  .box{
    display: flex;
    flex-direction: row;
    width:1000px;
    height: 100vh;
    flex-wrap: wrap;
    align-content: center;
  }

图21.align-content: flex-start

        4.3.4.space-between

  .box{
    display: flex;
    flex-direction: row;
    width:1000px;
    height: 100vh;
    flex-wrap: wrap;
    align-content: space-between;
  }

图22.align-content: space-between

        4.3.5.space-around

  .box{
    display: flex;
    flex-direction: row;
    width:1000px;
    height: 100vh;
    flex-wrap: wrap;
    align-content: space-around;
  }

图23.align-content: space-around

        4.3.6. stretch

会把没有设置高度的元素自动撑满。

注释掉item样式的高度,为第5个div块增加高度样式作为对比。

<div class="box">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item" style="height: 100px">5</div>
  <div class="item">6</div>
  <div class="item">7</div>
  <div class="item">8</div>
  <div class="item">9</div>
  <div class="item">10</div>
  <div class="item">11</div>
  <div class="item">12</div>
  <div class="item">13</div>
  <div class="item">14</div>
  <div class="item">15</div>
  <div class="item">16</div>
  <div class="item">17</div>
  <div class="item">18</div>
</div>

<style>
  .item{
    width: 100px;
    background-color: lightgray;
    /* height: 100px; */
    margin: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .box{
    display: flex;
    flex-direction: row;
    width:1000px;
    height: 100vh;
    flex-wrap: wrap;
    align-content: stretch;
  }
</style>

图24.align-content: stretch

5.单独控制元素

5.1.排序 / order

未需要排序的块单独添加

<div class="box">
  <div class="item" style="order: 3;">1</div>
  <div class="item" style="order: 1;">2</div>
  <div class="item" style="order: 2;">3</div>
</div>

图25.元素排序

5.2.缩放 / flex-grow & flex-shrink

        5.2.1.flex-grow

        按照flex-grow的比例分配剩余空间

<div class="box">
  <div class="item" style="flex-grow: 3;">1</div>
  <div class="item" style="flex-grow: 1;">2</div>
  <div class="item" style="flex-grow: 2;">3</div>
</div>

       

图26.flex-grow

        5.2.2.flex-shrink

         按照flex-shrink的比例减少超出的空间 

         首先增大每个div块的宽度(注意因为没有要求换行,所以会自动调整div块的大小以匹配容器的宽)

<div class="box">
  <div class="item" style="flex-shrink: 1;">1</div>
  <div class="item" style="flex-shrink: 2;">2</div>
  <div class="item" style="flex-shrink: 3;">3</div>
</div>

图27.flex-shrink

5.3.位置 / align-self

通过align-self单独操纵一个元素的位置

        5.3.1.auto

flex项将使用其父容器的 align-items 属性值。如果父容器没有设置 align-items,那么默认为是 stretch。

<div class="box">
  <div class="item">1</div>
  <div class="item" style="align-self: auto;">2</div>
  <div class="item">3</div>
</div>

图28. auto

        5.3.2.flex-start

<div class="box">
  <div class="item">1</div>
  <div class="item" style="align-self: flex-start;">2</div>
  <div class="item">3</div>
</div>

图29. flex-start

        5.3.3.flex-end

<div class="box">
  <div class="item">1</div>
  <div class="item" style="align-self: flex-end;">2</div>
  <div class="item">3</div>
</div>

图30. flex-end

        5.3.4.center

<div class="box">
  <div class="item">1</div>
  <div class="item" style="align-self: center;">2</div>
  <div class="item">3</div>
</div>

图31. center

        5.3.5.baseline

<div class="box">
  <div class="item" style="height: 200px;">1</div>
  <div class="item" style="align-self: baseline;">2</div>
  <div class="item" style="height: 200px;">3</div>
</div>

图32. center

        5.3.6.stretch

<div class="box">
  <div class="item" style="height: 200px;">1</div>
  <div class="item" style="align-self: stretch;">2</div>
  <div class="item" style="height: 400px;">3</div>
</div>

图33. stretch

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值