2、弹性盒子

容器属性——添加弹性容器上

  • flex-direction属性:设置主轴的方向,子元素的排列方向

    • flex-direction: row; 默认值,主轴方向为水平方向,起点在左端

    • flex-direction: row-reverse; 主轴方向为水平方向,起点在右端

    • flex-direction: column; 主轴方向为垂直方向,起点在上方

    • flex-direction: 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>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
    ​
            .warp,
            .warp1 {
                width: 300px;
                height: 300px;
                background-color: aqua;
                /* 设置弹性盒*/
                display: flex;
            }
    ​
            div {
                width: 100px;
                height: 100px;
            }
    ​
            /* X为主轴 */
            .warp {
                flex-direction: row;
                flex-direction: row-reverse;
            }
    ​
            /* Y为主轴 */
            .warp1 {
                flex-direction: column;
                flex-direction: column-reverse;
            }
    ​
            .warp .box1 {
                background-color: red;
            }
    ​
            .warp .box2 {
                background-color: pink;
            }
    ​
    ​
            .warp1 .box1 {
                background-color: purple;
            }
    ​
            .warp1 .box2 {
                background-color: blue;
            }
        </style>
    </head>
    ​
    <body>
        <div class="warp">
            <div class="box1">box1</div>
            <div class="box2">box2</div>
    ​
        </div>
        <hr>
        <div class="warp1">
            <div class="box1">box1</div>
            <div class="box2">box2</div>
        </div>
    </body>
    ​
    </html>

  • justify-content 属性:设置主轴的对齐方式,弹性子元素在主轴方向上的对齐方式,

    • justify-content: flex-start; 默认值,主轴顶端对齐

    • justify-content: flex-end; 主轴的末端对齐

    • justify-content: center; 居中对齐,子元素位于弹性容器的中心

    • justify-content: space-between; 两端对齐,子元素和子元素之间有空白空间,项目之间的间隔都相等。

    • justify-content: space-around; 子元素之前、之间、之后都留有空白空间,且空间自行分配,项目之间的间隔比项目与边框的间隔大一倍。

    • space-evenly:弹性项目平均分布在该行上,相邻项目的间隔,项目与容器之间空间相等

     /* X为主轴 */
    .warp {
              /* 设置弹性盒*/
                display: flex;
             flex-direction: row;
             justify-content: flex-start;
                justify-content: flex-end;
                justify-content: center;
                justify-content: space-between;
                justify-content: space-around;
    ​
            }
    ​
            /* Y为主轴 */
            .warp1 {
                /* 设置弹性盒*/
                display: flex;
                flex-direction: column;
                
                justify-content: flex-start;
                justify-content: flex-end;
                justify-content: center;
                justify-content: space-between;
                justify-content: space-around;
            }

  • align-items属性:弹性子元素在(侧轴)交叉轴上的对齐方式

    • align-items: stretch; 默认值,如果弹性子元素没有高度或高度为auto,将占满整个容器的高度

    • align-items: flex-start;子元素在侧轴顶端对齐

    • align-items: flex-end; 子元素在侧轴末端对齐

    • align-items: center; 子元素在侧轴中间对齐

    • align-items: baseline; 子元素在第一行文字的基线对齐

    .warp {
                /* 设置弹性盒*/
                display: flex;
                flex-direction: row;
    ​
                align-items: flex-start;
                align-items: flex-end;
                align-items: center;
                align-items: stretch;(height: auto;)
            }
    ​
    ​
     /*   align-items: baseline; */
      <style>
    ​
            /*   align-items: baseline; */
            .box {
                width: 400px;
                height: 400px;
                background-color: pink;
                display: flex;
                align-items: baseline;
                margin: 50px auto;
            }
    ​
            .box span {
                width: 100px;
                height: 100px;
                background-color: red;
                font-size: 20px;
                color: #fff;
            }
    ​
            .box img {
                height: 150px;
            }
        </style>
    </head>
    ​
    <body>
        <div class="box">
            <span>XJX</span>
            <img src="../img/bg1.jpg" alt="">
        </div>
    </body>
    ​
    </html>

  • flex-wrap属性:指定弹性盒子的子元素换行方式

    • flex-wrap: wrap; 换行,第一行显示在上方

    • flex-wrap: wrap-reverse; 换行,第一行显示在下方

    • flex-wrap: nowrap; 默认值,不换行

    注意:父元素有固定高度且高度大于子元素高之和换行中间有缝隙

    父元素高度有内容撑开换行没有缝隙

  • align-content属性:折行,行与行之间有间隙,去除间隙 ,控制侧轴对齐方式(去掉了中间的间隙)

    要设置: flex-wrap: wrap;

    • align-content: flex-start; 顶端没有行间距

    • align-content: flex-end; 底对齐没有行间距

    • align-content: center; 居中没有行间距

    • align-content: space-between; 两端对齐,中间自动分配

    • align-content: space-around; 自动分配距离

    注意:弹性盒项目为多行时有效

     .warp {
              display: flex;
                /* 设置后没有间隙 上端 下端*/
                align-content: flex-start;
                align-content: flex-end;
          
                /* 设置后没有间隙 中间*/
                align-content: center;
    ​
                /* 自动分配距离 */
                align-content: space-around;
                /* 两端对齐,中间自动分配 */
                align-content: space-between;
    ​
            }

<!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>
        * {
            margin: 0;
            padding: 0;
        }
​
        .box {
            height: 500px;
            background-color: chartreuse;
            display: flex;
        }
​
        .box p:nth-child(1),
        .box p:nth-child(3) {
            width: 200px;
            height: 480px;
            background-color: red;
        }
​
        .box p:nth-child(2) {
            /* 中间剩余空间分配 */
            flex: 1;
            height: 480px;
            background-color: green;
        }
    </style>
</head>
​
<body>
    <div class="box">
        <p>左边</p>
        <p>中间</p>
        <p>右边</p>
    </div>
</body>
​
</html>
  • 复合写法

    flex-flow:flex-direction  flex-wrap;
    例:
    flex-flow:cloumn wrap;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值