了解flex布局

 flex布局

display: flex;

一定要给亲父级加。

  1. 在flex眼中,标签不再分类。

    • 简单说就是没有块级元素,行内元素和行内块元素

    • 任何一个元素都可以直接给宽度和高度一行显示

  2. Flex不存在脱标的情况:也就是基本淘汰了浮动,更不用清除浮动

  3. 当然存在兼容性问题,如果不考虑兼容性可以大量使用,如果是移动端则不用考虑直接flex

 主轴对齐方式

 justify-content: flex-start;

 justify-content: flex-end;

 justify-content: center;

justify-content: space-around;

 justify-content: space-between;效果

 justify-content: space-evenly;

 完整代码

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 1200px;
            height: 400px;
            background-color: pink;
            margin: 100px auto;
            /* 弹性容器 */
            display: flex;
            /* 主轴对齐方式 */

            /*起点开始依次排序 从左到右 默认值*/
            justify-content: flex-start;

            /*终点开始依次排序 从右到左 */
            justify-content: flex-end;

            /* 主轴居中  重点*/
            justify-content: center;

            /* 盒子平均分,比例1:2 */
            justify-content: space-around;

            /* 盒子平均分,2边要靠边,中间平均分距离 重点*/
            justify-content: space-between;

            /* 盒子平均分,比例为1:1 */
            justify-content: space-evenly;
        }

        /* 父盒子里面的儿子弹性盒子 */
        .box>div {
            width: 250px;
            height: 200px;
            background-color: aquamarine;
        }

        .box>div:nth-child(2n+1) {
            background-color: yellowgreen;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
</body>

</html>

  侧轴对齐方式

 align-items: stretch;

  align-items: flex-start;

  align-items: flex-end;

 align-items: center;

 代码

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 1200px;
            height: 400px;
            background-color: pink;
            margin: 100px auto;
            /* 父变成弹性容器 */
            display: flex;
            /* 侧轴对齐方式 子元素默认和父元素高度一样 拉伸*/
            align-items: stretch;
            /* 从上边开始排列  默认起点开始排列在上面 */
            align-items: flex-start;
            /* 从下边开始排列  终点开始排列 */
            align-items: flex-end;

            /* 侧轴居中 ,垂直居中  重点*/
            align-items: center;
        }

        /* 子元素弹性盒子 */
        .box>div {
            width: 250px;
            height: 200px;
            background-color: aquamarine;
        }

        .box>div:nth-child(2n+1) {
            background-color: yellowgreen;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
</body>

</html>

可以运用主轴和侧轴的对齐方式使盒子垂直居中显示

 代码

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 400px;
            height: 400px;
            background-color: pink;
            /* 弹性容器 */
            display: flex;
            /* 主轴对齐 */
            justify-content: center;
            /* 侧轴对齐 */
            align-items: center;
        }

        /* 子盒子是弹性盒子 */
        .box .son {
            width: 200px;
            height: 200px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="son"></div>
    </div>
</body>

</html>

侧轴子元素单独对齐方式

代码

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 600px;
            height: 600px;
            background-color: pink;
            margin: 100px auto;
            /* 弹性容器 */
            display: flex;
        }

        /* 子元素弹性盒子 */
        .box>div {
            width: 250px;
            height: 200px;
            background-color: aquamarine;
            border-radius: 50%;
            text-align: center;
            line-height: 200px;
        }

        .box>div:nth-child(2n+1) {
            background-color: yellowgreen;
        }

        /* align-self 单独控制子元素 ,取值和align-items一致*/
        .box>div:first-child {
            align-self: flex-start;
        }

        .box>div:nth-child(2) {
            align-self: center;
        }

        .box>div:last-child {
            align-self: flex-end;
        }
    </style>
</head>

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

</html>

伸缩比

把父盒子分为若干份数,每个子盒子各占几份。

    flex:1; 一定给子盒子加

    分配父级剩余的空间

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 1200px;
            height: 400px;
            background-color: pink;
            margin: 100px auto;
            /* 弹性容器 */
            display: flex;
        }

        /* 弹性盒子 */
        .box>div {
            /* 伸缩比,占的是父盒子的宽度,把他分成了等份 */
            flex: 1;
            margin-right: 15px;
            background-color: aqua;
        }

        .box>div:nth-child(2n) {
            flex: 2;
            background-color: yellowgreen;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
</body>

</html>

 圣杯布局

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .search {
            width: 100%;
            height: 50px;
            background-color: pink;
            /* 弹性容器 */
            display: flex;
        }

        .search .left {
            width: 50px;
            height: 50px;
            background-color: aqua;
        }

        .search .center {
            flex: 1;
            background-color: red;
        }

        .search .right {
            width: 50px;
            height: 50px;
            background-color: aqua;
        }
    </style>
</head>

<body>
    <!-- 圣杯布局,2边固定宽高,中间自适应 -->

    <div class="search">
        <div class="left">左边</div>
        <div class="center">中间的内容</div>
        <div class="right">右边</div>
    </div>
</body>

</html>

调整主轴方向

主轴默认是水平方向, 侧轴默认是垂直方向

修改主轴方向属性: flex-direction

 flex-direction: column;

 代码

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 500px;
            height: 500px;
            background-color: pink;
            /* 弹性容器 */
            display: flex;
            /* 主轴默认是X轴 */
            /* flex-direction: row; */
            /* 主轴反方向排列 */
            /* flex-direction: row-reverse; */
            /* 调整主轴,换成Y轴 */
            flex-direction: column;
            /* 调整主轴,换成Y轴反方向排列 */
            /* flex-direction: column-reverse; */
            /* 侧轴居中 */
            align-items: center;
            /* 主轴居中 */
            justify-content: center;
        }

        .box>div {
            width: 200px;
            height: 200px;
            background-color: yellowgreen;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="son">老大</div>
        <div class="son2">老二</div>
    </div>
</body>

</html>

flex多行排列

 flex-wrap: wrap;  换行

特别是多行的情况下,我们需要给弹性盒子换行,给 父盒子 弹性容器加。

 代码

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 800px;
            height: 400px;
            background-color: pink;
            /* 弹性容器 */
            display: flex;
            /* flex弹性盒子换行显示 */
            flex-wrap: wrap;
            /* 侧轴居中 */
            align-items: center;
            align-items: flex-end;
        }

        /* flex布局默认是不换行 */
        .box>div {
            width: 200px;
            height: 100px;
            background-color: red;
        }

        .box>div:nth-child(2n) {
            background-color: yellowgreen;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
        <div>10</div>
    </div>
</body>

</html>

多行盒子侧轴排列

 代码

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 800px;
            height: 400px;
            background-color: pink;
            /* 弹性容器 */
            display: flex;
            /* 主轴居中 */
            justify-content: center;
            flex-wrap: wrap;

            /* 单行盒子侧轴的排列方式 */
            /* align-items: center; */
            /* 多行侧轴的排列方式 ,一般配合换行使用*/
            /* align-content: center; */
            /* align-content: flex-start; */
            /* align-content: flex-end; */
            /* align-content: space-around; */
            /* align-content: space-between; */
            align-content: space-evenly;
        }

        /* flex布局默认是不换行 */
        .box>div {
            width: 200px;
            height: 100px;
            background-color: red;
        }

        .box>div:nth-child(2n) {
            background-color: yellowgreen;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
    </div>
</body>

</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值