Flex 布局

1.Flex 布局 

Flex 布局即弹性布局,是一种用于在容器中排列项目的布局模型,它提供了一种更灵活、高效的方式来控制页面元素的布局。

2.基本概念 

  • 容器和项目:采用 Flex 布局的元素称为 Flex 容器,容器内的直接子元素称为 Flex 项目。
  • 主轴和交叉轴:Flex 容器有两根轴,水平的主轴和垂直的交叉轴,主轴的方向可以通过属性设置为水平或垂直,交叉轴则与主轴垂直。

3.容器的属性 

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

 3.1 flex-direction属性

 flex-direction属性决定主轴的方向。

.box {
  flex-direction: row | row-reverse | column | column-reverse;
}
  • row(默认值):主轴为水平方向,起点在左端。
  • row-reverse:主轴为水平方向,起点在右端。
  • column:主轴为垂直方向,起点在上沿。
  • column-reverse:主轴为垂直方向,起点在下沿。

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">
        <div class="zi">1</div>
        <div class="zi">2</div>
        <div class="zi">3</div>
    </div>
</body>
<style>
    .box {
        display: flex;
        flex-direction: column-reverse ;
        width: 150px;
        height: 150px;
        background-color: #dcdcdc;
    }

    .zi {
        width: 30px;
        height: 30px;
        background-color: #ffe105;
    }
</style>

</html>

3.2 flex-wrap属性 

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

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

nowrap(默认):不换行。

wrap:换行,第一行在上方。 

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

   nowrap不换行,宽度不够的话,会自动缩减 

 wrap换行的话,纵轴会平均分列

 

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">
        <div class="zi">1</div>
        <div class="zi">2</div>
        <div class="zi">3</div>
        <div class="zi">4</div>
        <div class="zi">5</div>
        <div class="zi">6</div>
    </div>
</body>
<style>
    .box {
        display: flex;
        flex-wrap: wrap-reverse;
        width: 150px;
        height: 150px;
        background-color: #dcdcdc;
    }

    .zi {
        width: 30px;
        height: 30px;
        background-color: #ffe105;
    }
</style>

</html>

3.3 flex-flow 

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

.box {
  flex-flow: <flex-direction> <flex-wrap>;
}

3.4 justify-content属性 

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

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

 

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">
        <div class="zi">1</div>
        <div class="zi">2</div>
        <div class="zi">3</div>
    </div>
</body>
<style>
    .box {
        display: flex;
        justify-content: space-around;
        width: 150px;
        height: 150px;
        background-color: #dcdcdc;
    }

    .zi {
        width: 30px;
        height: 30px;
        background-color: #ffe105;
    }
</style>

</html>

3.5 align-items属性 

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

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

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

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

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">
        <div class="zi1">1</div>
        <div class="zi2">2</div>
        <div class="zi3">3</div>
        <div class="zi4">4</div>
    </div>
</body>
<style>
    .box {
        display: flex;
        align-items: stretch;
        width: 150px;
        height: 150px;
        background-color: #dcdcdc;
    }

    .zi1 {
        width: 30px;
        height: auto;
        background-color: #ffe105;
        font-size: 15px;
    }

    .zi2 {
        width: 35px;
        height: auto;
        background-color: #ffe105;
        font-size: 25px;
    }

    .zi3 {
        width: 30px;
        height: auto;
        background-color: #ffe105;
        font-size: 20px;
    }

    .zi4 {
        width: 30px;
        height: auto;
        background-color: #ffe105;
        font-size: 12px;
    }
</style>

</html>

3.6 align-content属性 

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

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

 

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="box">
        <div class="zi">1</div>
        <div class="zi">2</div>
        <div class="zi">3</div>
        <div class="zi">4</div>
        <div class="zi">5</div>
        <div class="zi">6</div>
        <div class="zi">7</div>
        <div class="zi">8</div>
        <div class="zi">9</div>
        <div class="zi">10</div>
        <div class="zi">11</div>
        <div class="zi">12</div>
    </div>
</body>
<style>
    .box {
        display: flex;
        flex-wrap: wrap;
        align-content: stretch;
        width: 150px;
        height: 150px;
        background-color: #dcdcdc;
    }

    .zi {
        width: 30px;
        height: 30px;
        background-color: #ffe105;
    }
</style>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值