CSS flex布局(详解)

14 篇文章 0 订阅

前面我们学了很多基本的布局,现在我们将学习一种新的布局方式,这种布局方式更为常用,并且可以缩减很多不必要的代码。

我们来看一个实际中的布局。

代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .head {
      width: 400px;
      height: 400px;
      display: flex;
      flex-direction: column;
    }

    .head .top {
      display: flex;
      flex: 1;
      background-color: red;
    }

    .top-Top {
      flex: 1;
      background-color: yellow;
    }

    .top-Bottom {
      flex: 1;
    }

    .head .bottom {
      background-color: pink;
      flex: 1;
      display: flex;
      flex-direction: column;
    }
    .bottom-Top{
      flex: 1;
      background-color: blue;
    }
    .bottom-Bottom{
      flex: 1;
    }
  </style>
</head>

<body>
  <div class="head">
    <div class="top">
      <div class="top-Top">
        上边的左面
      </div>
      <div class="top-Bottom">
        上边的右面
      </div>
    </div>
    <div class="bottom">
      <div class="bottom-Top">
        下边的上面
      </div>
      <div class="bottom-Bottom">
        下边的下面
      </div>
    </div>
  </div>
</body>

</html>
是不是发现比传统写法定位这种方便并且当你调整大的盒子的大小时,其他的小盒子跟着一起变化

flex布局

我们来复习一下css中的坐标轴方向:注意这个坐标轴的方向是不用于数学中的直角坐标,他是以页面左上角为原点的坐标轴。其中主轴默认的是x轴

属性:

  1. flex-direction:指定 flex 容器内项目的排列方向。

    • 可选值:
      • row:水平方向,从左到右排列(默认值)。
      • row-reverse:水平方向,从右到左排列。
      • column:垂直方向,从上到下排列。
      • column-reverse:垂直方向,从下到上排列。
  2. justify-content:定义 flex 容器内项目沿主轴的对齐方式。

    • 可选值:
      • flex-start:靠近主轴起点对齐(默认值)。
      • flex-end:靠近主轴末尾对齐。
      • center:居中对齐。
      • space-between:两端对齐,项目之间的间隔相等。
      • space-around:每个项目两侧的间隔相等,项目之间的间隔是它们的两倍。
  3. align-items:定义 flex 容器内项目沿交叉轴的对齐方式。

    • 可选值:
      • flex-start:靠近交叉轴起点对齐。
      • flex-end:靠近交叉轴末尾对齐。
      • center:居中对齐(默认值)。
      • baseline:基线对齐。
      • stretch:拉伸以填充容器。
  4. flex-wrap:定义 flex 容器内项目是否换行。

    • 可选值:
      • nowrap:不换行,所有项目在一行内排列(默认值)。
      • wrap:换行,超出容器宽度时进行换行。
      • wrap-reverse:反向换行,超出容器宽度时从底部开始换行。
  5. align-content:定义多行或多列的 flex 容器内项目的对齐方式,只有在存在多行或多列时才生效。

    • 可选值:
      • flex-start:靠近交叉轴起点对齐。
      • flex-end:靠近交叉轴末尾对齐。
      • center:居中对齐。
      • space-between:两端对齐,项目之间的间隔相等。
      • space-around:每个项目两侧的间隔相等,项目之间的间隔是它们的两倍。
      • stretch:拉伸以填充容器。

flex-direction

flex-direction设置主轴方向,默认按照水平布局

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS</title>
  <style>
    .contains{
      width: 300px;
      height: 300px;
      background-color: pink;
      display: flex;
    }
    .left{
      flex:1;
      background-color: yellow;
    }
    .right{
      flex:1;
    }
  </style>
</head>
<body>
  <div class="contains">
    <div class="left">
      左边
    </div>
    <div class="right">
      右边
    </div>
  </div>
</body>
</html>

当我改变flex-direction:column此时显示得是按照垂直排列

reserve主要是写排列顺序的。正常时先写得先排,但是加上revser的时候会颠倒顺序。如

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS</title>
  <style>
    .contains{
      width: 300px;
      height: 300px;
      background-color: pink;
      display: flex;
      flex-direction:column-reverse;
    }
    .left{
      flex:1;
      background-color: yellow;
    }
    .right{
      flex:1;
    }
  </style>
</head>
<body>
  <div class="contains">
    <div class="left">
      左边
    </div>
    <div class="right">
      右边
    </div>
  </div>
</body>
</html>

justify-content:定义 flex 容器内项目沿主轴的对齐方式。

justify-content::center水平居中对齐

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS</title>
  <style>
    body{
      display: flex;
      justify-content: center;
    }
    .contains{
      width: 300px;
      height: 300px;
      background-color: pink;
      display: flex;
      flex-direction:column-reverse;
    }
    .left{
      flex:1;
      background-color: yellow;
    }
    .right{
      flex:1;
    }
  </style>
</head>
<body>
  <div class="contains">
    <div class="left">
      左边
    </div>
    <div class="right">
      右边
    </div>
  </div>
</body>
</html>

justify-content: start;水平最左边,初始位置

justify-content: end;水平最右端

justify-content: space-between;按照默认的鹅direction排列,保证间隔相同,左右贴近边框

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS</title>
  <style>
    .contains{
      width: 300px;
      height: 300px;
      display: flex;
      justify-content: space-between;
    }
    .left{
      width: 50px;
      background-color: yellow;
    }
    .right{
      width: 50px;
      background-color: pink;
    }
  .right1{
      width: 50px;
      background-color: blue;
    }

  </style>
</head>
<body>
  <div class="contains">
    <div class="left">
      左边
    </div>
    <div class="right">
      右边
    </div>
    <div class="right1">
      右边
    </div>
  </div>
</body>
</html>

align-items指的时垂直方向上的排列,使用和justify-content一样。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .head{
      width: 100px;
      height: 100px;
      background-color: pink;
    }
    .top{
      justify-content: center;
      align-items: center ;
      display: flex;
      width: 1200px;
      height: 500px;
      background-color: #ccc;
    }
  </style>
</head>
<body>
  <div class="top">
    <div class="head">
    
    </div>
  </div>
</body>
</html>

flex-wrap:定义 flex 容器内项目是否换行。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .head{
      width: 100px;
      height: 100px;
      background-color: pink;
      display: flex;
      flex-wrap: wrap;
    }
    .l{
      width: 10px;
      height: 10px;
      background-color: red;
      margin:3px;
    }
  </style>
</head>
<body>
  <div class="top">
    <div class="head">
      <div class="l"></div>
      <div class="l"></div>
      <div class="l"></div>
      <div class="l"></div>
      <div class="l"></div>
      <div class="l"></div>
      <div class="l"></div>
      <div class="l"></div>
    </div>
  </div>
</body>
</html>

align-content:定义多行或多列的 flex 容器内项目的对齐方式,只有在存在多行或多列时才生效。

align-contentalign-items 是 Flexbox 布局中用于控制项目在交叉轴上对齐的两个属性,它们之间有一些区别:

  • align-content

    • 只在 flex 容器有多行时生效,用来定义多行的对齐方式。
    • 如果容器内只有一行,align-content 不会生效。
    • 适用于多行的情况,可以控制多行之间的对齐方式,比如居中对齐、沿着交叉轴均匀分布等。
  • align-items

    • 用来定义单行内项目在交叉轴上的对齐方式。
    • 如果 flex 容器是单行的,align-items 控制整个项目集合在交叉轴上的对齐方式。
    • 适用于单行的情况,可以控制项目在交叉轴上的对齐方式,比如居中对齐、顶部对齐、底部对齐等。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值