css之flex布局

flext弹性局部

1、传统布局与flex布局

1.1、传统布局

  • 兼容性好
  • 布局繁琐
  • 局限性,不能再移动端很好的布局

1.2、flex弹性布局

  • 操作方便,布局极为简单,移动端应用很广泛
  • PC端浏览器支持情况较差
  • IE 11或更低版本,不支持或仅部分支持

建议:

  1. 如果是PC端页面布局,我们还是传统布局。
  2. 如果是移动端布局或者是不考虑兼容性问题的 PC端页面布局,我们还是使用flex弹性布局。

1.3、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>
        .box {
            display: flex;
            width: 80%;
            height: 300px;
            background-color: aqua;
        }
        .box span {
            width: 150px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
</html>

2、子元素水平方向平分父元素空间显示
在这里插入图片描述
代码实现:

<!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 {
            display: flex;
            width: 80%;
            height: 300px;
            background-color: aqua;
            /* 水平方向平分空间 */
            justify-content: space-around;
        }
        .box span {
            width: 150px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
</html>

3、三等分
在这里插入图片描述
代码实现:

<!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 {
            display: flex;
            width: 80%;
            height: 300px;
            background-color: aqua;
            /* 水平方向平分空间 */
            justify-content: space-around;
        }
        .box span {
            flex: 1;
            height: 100px;
            margin-right: 1px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
</html>

2、flex布局原理

flex是flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性,任何一个容器都可以指定为flex布局。

  • 当我们为父盒子设为flex布局以后,子元素的float、clear和vertical-align属性将失效。
  • 伸缩布局=弹性布局=伸缩盒布局=弹性盒布局=flex布局

采用flex布局的元素,称为flex容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为flex项目(flex item),简称"项目"。

  • 初体验中div就是flex父容器
  • 体验中span就是子容器flex项目
  • 子容器可以横向排列也可以纵向排列

图示:
在这里插入图片描述
总结:就是通过给父盒子添加flex属性,来控制子盒子的位置和排列方式

3、flex布局父项常见属性

3.1、常见的父项属性

一下由6个属性是对父元素设置的:

  1. flex-direction:设置主轴的方向
  2. justify-content:设置主轴上的子元素排列方式
  3. flex-wrap:设置子元素是否换行
  4. align-content:设置侧轴上的子元素的排列方式(多行)
  5. align-items:设置侧轴上的子元素排列方式(单行)
  6. flex-flow:复合属性,相当于同时设置了flex-directionflex-wrap

3.2、flex-direction设置主轴的方向

1、主轴与侧轴

在flex布局中,是分为主轴和侧轴两个方向,同样的叫法有:行和列,x轴和y轴。

  • 默认主轴方向就是x轴方向,水平向右
  • 默认侧轴方向就是y轴方向,水平向下

注意flex-direction属性值的默认值就是主轴就是x轴即flex-direction: row;

在这里插入图片描述
** 2、属性值**
flex-direction属性决定了主轴的方向(即项目的排列方式)

注意:主轴和侧轴是会变化的,就看flex-direction设置谁为主轴剩下的就是侧轴,而我们的子元素是跟着主轴来排列的。

属性值说明
row默认值从左到右
row-reverse从右到左
column从上到下
column-reverse从下到上

如:

<!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 {
            /* 给父元素添加flex属性 */
            display: flex;
            flex-direction: row; /* 默认值主轴是x轴,那么y轴就是侧轴 */
            width: 80%;
            height: 300px;
            background-color: aqua;
            /* 注意:我们的元素是跟着主轴来排列的 */
        }
        .box span {
            width: 20%;
            height: 100px;
            margin-right: 1px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
</html>

效果图:
在这里插入图片描述
我们可以把我们的主轴设置为y轴,那么x轴就成了侧轴了。如:

<!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 {
            /* 给父元素添加flex属性 */
            display: flex;
            flex-direction: column; /* 设置y轴为主轴,此时x轴为侧轴 */
            width: 80%;
            height: 300px;
            background-color: aqua;
            /* 注意:我们的元素是跟着主轴来排列的 */
        }
        .box span {
            width: 20%;
            height: 100px;
            margin-top: 1px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
</html>

效果图:
在这里插入图片描述

3.3、justify-content设置主轴上的子元素排列方式

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

注意:使用这个属性之前一定要确定好主轴是哪个

属性值说明
flex-start默认值从头部开始,如果主轴是x轴,则从左到右
flex-end从尾部开始排列
center在主轴居中对齐(如果主轴是x轴则水平居中)
space-around平方剩余空间
space-between先两边贴边,再平方剩余空间(重要)

如:

<!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 {
            display: flex;
            width: 80%;
            height: 400px;
            background-color: pink;
            flex-direction: row;
            /* justify-content是设置主轴上子元素的排列方式 */
            /* 默认值是flex-start */
            /* justify-content: flex-start; */
            /* justify-content: flex-end; */
            /* 让我们的子元素居中对齐 */
            justify-content: center;
            /* 平分剩余空间 */
            /* justify-content: space-around; */
            /* 先两边贴边,在平分剩余空间 */
            /* justify-content: space-between; */

        }
        .box span {
            width: 20%;
            height: 30%;
            background-color: royalblue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
</html>

效果图:
在这里插入图片描述

3.4、flex-wrap设置子元素是否换行

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

属性值说明
nowrap默认值,不换行
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 {
            display: flex;
            width: 30%;
            height: 400px;
            background-color: pink;
            flex-direction: row;
            /* flex布局中,默认的子元素是不换行的,如果装不开,会缩小子元素的宽度,放到父元素里面 */
            /* flex-wrap: nowrap; 默认值 */
            flex-wrap: wrap; /* 换行 */
        }
        .box span {
            width: 25%;
            height: 30%;
            margin: 10px;
            background-color: royalblue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
    </div>
</body>
</html>

效果图:
在这里插入图片描述

3.5、align-items设置侧轴上的子元素排列方式(单行)

该属性是控制子项在侧轴(默认是y轴)上的排列方式,在子项为单项的时候使用。

属性值说明
flex-start从上到下
flex-end从下到上
center挤在一起居中(垂直居中)
stretch拉伸(默认值)

例如:元素在居中案例

<!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 {
            display: flex;
            width: 30%;
            height: 300px;
            background-color: pink;
            flex-direction: row;
            /* 主轴水平居中 */
            justify-content: center;
            /* 侧轴垂直居中 */
            align-items: center;
        }
        .box span {
            width: 25%;
            height: 30%;
            margin: 10px;
            background-color: royalblue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
    </div>
</body>
</html>

效果图:
在这里插入图片描述

3.6、align-content设置侧轴上的子元素的排列方式(多行)

设置子项在侧轴上的排列方式,并且只能用于子项出现换行的情况(多行),在单行下是没有效果的。

属性值说明
flex-start默认值在侧轴的头部开始排列
flex-end在侧轴的尾部开始排列
center在侧轴中间显示
space-around子项在侧轴水平平方剩余空间
space-between子项在侧轴先分布在两头,在平分剩余空间
stretch设置子项元素高度平分父元素高度

如:换行元素居中

<!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 {
            display: flex;
            width: 30%;
            height: 300px;
            background-color: pink;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: center;
            /* 因为有了换行,此时我们侧轴上控制子元素的对齐方式我们用 align-content */
            align-content: center;

        }
        .box span {
            width: 25%;
            height: 30%;
            margin: 10px;
            background-color: royalblue;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
        <span>5</span>
        <span>6</span>
    </div>
</body>
</html>

效果图:
在这里插入图片描述

3.7、align-content和align-items的区别

1、align-items适用于单行情况下,只有上对齐、下对齐、居中和拉伸

2、align-content适用于换行(多行)的 情况下(单行情况下无效),可以设置上对齐、下对齐、居中、拉伸以及平均分配剩余空间等属性值。

3、总结就是单行找align-items多行找align-content

在这里插入图片描述

3.8、flex-flow

flex-flow属性是flex-directionflex-wrap属性的复合属性。

/*flex-direction: column;
flex-wrap: wrap;*/

/* 把设置主轴方向和是否换行(换列)简写 */
flex-flow: column wrap;

4、flex布局子项常见属性

1、flex子项目占的份数

2、align-self控制子项目自己在侧轴的排列方式

3、order属性定义子项的排列顺序(前后顺序)

4.1、flex属性

flex属性定义子项目分配剩余空间,用flex来表示占多少份数

如:

<!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 {
            display: flex;
            width: 60%;
            height: 150px;
            background-color: pink;
            margin: 0 auto;
        }
        .box div:nth-child(1) {
            width: 100px;
            height: 150px;
            background-color: red;
        }
        
        .box div:nth-child(2) {
            flex: 1;
            background-color: seagreen;
        }
        .box div:nth-child(3) {
            flex: 2;
            background-color: springgreen;
        }
    </style>
</head>
<body>
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>

效果图:
在这里插入图片描述

4.2、align-self控制子项自己在侧轴上的排列方式

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

如:

<!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 {
            display: flex;
            width: 60%;
            height: 400px;
            background-color: pink;
            /* 让三个盒子沿着侧轴底侧对齐 */
            /* align-items: flex-end; */
        }
        .box span {
            width: 150px;
            height: 100px;
            background-color: purple;
            margin-left: 5px;
        }
        .box span:nth-child(3) {
            align-self: flex-end;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
</html>

效果图:
在这里插入图片描述

4.3、order属性定义项目的排列顺序

数值越小,排列越靠前,默认为0。注意:和z-index不一样。

如:

<!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 {
            display: flex;
            width: 60%;
            height: 400px;
            background-color: pink;
            /* 让三个盒子沿着侧轴底侧对齐 */
            /* align-items: flex-end; */
        }
        .box span {
            width: 150px;
            height: 100px;
            background-color: purple;
            margin-left: 5px;
        }
        .box span:nth-child(2) {
            /* 默认是0,-1比0小所以在前面 */
            order: -1;
        }
        .box span:nth-child(3) {
            align-self: flex-end;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
</body>
</html>

效果图:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lambda.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值