前端-html,css弹性盒子模型

弹性盒子模型

1.传统布局和flex布局对比

传统布局

传统布局依靠display ,float ,position 来完成布局,

但是遇到一些特殊的布局就不方便布局

flex布局

操作方便,布局及其简单,移动端使用比较广泛

pc端浏览器支持情况比较差

IE11或者更低版本不支持flex或仅支持部分

小结

如果是pc端页面布局,还是采用传统方式

如果是移动端或者是不考虑兼容的pc则采用flex

2.flex布局原理

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

2.flex布局又叫伸缩布局 、弹性布局 、伸缩盒布局 、弹性盒布局

3.采用 Flex 布局的元素,称为 Flex 容器(flex

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

小结

就是通过给父盒子添加flex属性,来控制子盒子的位置和排列方式

3.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=w, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            /* 在父级添加flex */
            display: flex;
            width: 1000px;
            background-color: orange;
        }

        .box div {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: red;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            border: 2px solid #fff;
        }
    </style>
</head>

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

    </div>
</body>

</html>

4.flex-dirdction主轴方向

1.在flex布局中,注意主轴喝侧轴的问题,相当如x , y轴 。行。列

2.默认主轴方向就是 x 轴方向,水平向右,

默认侧轴方向就是 y 轴方向,水平向下

3.注意: 主轴和侧轴是会变化的,就看 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>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            /*   使用flex布局的前提是给父元素设置display: flex;*/
            display: flex;
            /* 设置主轴方向 */
            /* row 默认值 从左往右 */
            /* flex-direction: row; */
            /*row-reverse  从右到左 */
            /* flex-direction: row-reverse; */
            /* column 从上到下 */
            /* flex-direction: column; */
            /*column-reverse 从下到上 */
            flex-direction: column-reverse;
            width: 1000px;
            height: 600px;
            background-color: orange;
            /* 换行 */
            /* 默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,flex布局中默认是不换行的。 */
            /* nowrap  不换行*/
            /* flex-wrap: wrap; */

        }

        .box span {
            width: 200px;
            height: 200px;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            line-height: 150px;
            background-color: #0a0;
            border: 1px solid #fff;
        }
    </style>
</head>

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

</html>

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

flex-start从左到右
flex-end去到尾部
center居中
space-between两端对齐中间等分间距
space-around所有子元素左右都会有一个空白缝隙
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=w, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            /* 在父级添加flex */
            display: flex;
            /* 主轴排列方式 justify-content 设置主轴上的子元素排列方式*/
            /*flex-start 从左到右 */
            /* justify-content: flex-start; */
            /* flex-end 去到尾巴 */
            /* justify-content: flex-end; */
            /* center 居中 常用*/
            /* justify-content: center; */
            /* space-between 两端对齐中间等分间距 */
            /* justify-content: space-between; */
            /*   space-around 所有子元素左右都会有一个空白缝隙 导致两侧的间隙 比 中间的要小,因为中间的是两个间隙加在一起的效果 */
            /* justify-content: space-around; */
            /* space-evenly  所有的间隙均等 */
            justify-content: space-evenly;
            width: 1000px;
            height: 600px;
            margin: 0 auto;
            background-color: orange;
        }

        .box span {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: red;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            border: 2px solid #fff;
        }
    </style>
</head>

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

</html>

6.更换主轴,子元素的排列方式

注意:注意主轴的切换

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

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

        .box {
            /* 在父级添加flex */
            display: flex;
            /* 更换主轴的方向    让主轴的方向从上到下 */
            flex-direction: column;
            /* 居中 */
            /* justify-content: center; */
            /* space-evenly  所有的间隙均等 */
            /* justify-content: space-evenly; */
            /* space-between 两端对齐中间等分间距  最常用*/
            justify-content: space-between;
            width: 1000px;
            height: 600px;
            background-color: orange;
        }

        .box div {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: red;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            border: 2px solid #fff;
        }
    </style>
</head>

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

    </div>
</body>

</html>

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

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

flex-start从头部开始
flex-end从尾部开始
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=w, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            /* 在父级添加flex */
            display: flex;
            /*  单行子元素在侧轴上的对齐方式只有3个*/
            /* flex-start 位于顶部 */
            /* align-items: flex-start; */
            /* flex-end 位于底部 */
            align-items: flex-end;
            width: 1000px;
            height: 600px;
            background-color: orange;
        }

        .box div {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: red;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            border: 2px solid #fff;
        }
    </style>
</head>

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

</html>

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

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

flex-start从上到下 【常用】
flex-end从下到上
center居中
space-between两端对齐 等分间距
space-around元素两端间隙相等
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=w, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            /* 在父级添加flex */
            display: flex;
            /*    多行子元素在侧轴上的对齐方式
                必须和flex-wrap: wrap搭配 */
            flex-wrap: wrap;
            /* flex-start 从上到下 【常用】 */
            /* align-content: flex-start; */
            /*  flex-end 从下到上 */
            /* align-content: flex-end; */
            /*   center 居中 */
            /* align-content: center; */
            /* space-between 两端对齐 等分间距 */
            /* align-content: space-between; */
            /* space-around 元素两端间隙相等 */
            /* align-content: space-around; */
            /* space-evenly 所有间隙都相等 */
            align-content: space-evenly;
            width: 1000px;
            height: 600px;
            background-color: orange;
        }

        .box div {
            width: 150px;
            height: 150px;
            line-height: 100px;
            background-color: red;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            border: 2px solid #fff;
        }
    </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>7</div>
        <div>9</div>
        <div>10</div>

    </div>
</body>

</html>

区别

  • align-items 适用于单行情况下, 只有上对齐、下对齐、居中和 拉伸
  • align-content适应于换行(多行)的情况下(单行情况下无效), 可以设置 上对齐、下对齐、居中、拉伸以及平均分配剩余空间等属性值。
  • 总结就是单行找align-items 多行找 align-content

9.连写属性/复合属性 flex-flow 属性是 flex-direction 和 flex-wrap

flex-flow:row wrap;

10.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=w, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            /* 在父级添加flex */
            display: flex;
            width: 1000px;
            height: 600px;
            background-color: orange;
        }

        .box div {
            /* 用flex来表示占多少份数 */
            flex: 1;
            height: 100px;
            line-height: 100px;
            background-color: red;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            border: 2px solid #fff;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>1</div>
        <div style="flex: 2;">2</div>
        <div style="flex: 2;">3</div>
        <div>4</div>
        <div>4</div>

    </div>
</body>

</html>

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

默认值为 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=w, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            /* 在父级添加flex */
            display: flex;
            /* 侧轴居中 */
            /* align-items: center; */
            width: 1000px;
            height: 600px;
            background-color: orange;
        }

        .box div {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: red;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            border: 2px solid #fff;
        }

        .box div:nth-child(2) {
            /* 设置自己在侧轴上的排列方式 */
            align-self: flex-end;
        }
    </style>
</head>

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

    </div>
</body>

</html>

12.order 属性定义项目的排列顺序

数值越小,排列越靠前,默认为0。、

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

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

        .box {
            /* 在父级添加flex */
            display: flex;
            width: 1000px;
            height: 600px;
            background-color: orange;
        }

        .box div {
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: red;
            color: #fff;
            font-size: 50px;
            font-weight: 700;
            text-align: center;
            border: 2px solid #fff;
        }
    </style>
</head>

<body>
    <div class="box">

        <div>1</div>
        <!-- /* order 值越小越靠前 默认所有的值0 值越大越靠后 */ -->
        <div style="order: -1;">2</div>
        <div>3</div>
        <div>4</div>

    </div>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值