align-items和align-content的区别

最近在学习flex布局相关的内容将align-content和align-items的区别分享给大家


flex布局align-items和align-content属性的区别

align-items:

<!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: 800px;
            height: 800px;
            /* 主轴设置为y轴 */
            flex-direction: column;
            justify-content: space-around;
            /* 所以这里设置的侧轴是x轴,对齐方式为居中*/
            align-items: center;
            background-color: skyblue;
        }
        p {
            width: 200px;
            height: 200px;
            background-color: green;
        }
    </style>
</head>
<body>
    <div class="box">
        <p>a</p>
        <p>b</p>
        <p>c</p>
    </div>
</body>
</html>

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

align-content:

<!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: 800px;
            height: 800px;
            /* 主轴默认为x轴 */
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: space-between;
            /* 所以这里设置的侧轴是y轴,对齐方式为居中*/
            align-content: center;
            background-color: skyblue;
        }
        p {
            width: 200px;
            height: 200px;
            background-color: green;
            margin: 20px;
        }
    </style>
</head>
<body>
    <div class="box">
        <p>a</p>
        <p>b</p>
        <p>c</p>
        <p>d</p>
        <p>e</p>
        <p>f</p>
    </div>
</body>
</html>

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

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

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

所以单行使用align-items 多行找使用align-content


总结

以上就是今天要跟大家分享的flex布局align-items和align-content的区别。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值