CSS-弹性布局2-交叉轴

本文详细介绍了Flex布局中的align-items和align-content属性,解释了这些属性如何影响项目的垂直对齐及多轴线的对齐方式,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

4.5 align-items

该属性定义项目在交叉轴上如何对齐。

属性值说明
stretch(默认值)如果项目未设置高度或者设为auto,将占满整个容器高度
baseline项目的第一行文字的基线对齐
flex-start交叉轴的起点对齐
flex-end交叉轴的终点对齐
center交叉轴的中点对齐

源代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>flex-wrap</title>
    <style type="text/css">
    .wrap{
        width: 200px;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        height: 200px;
        align-items: stretch;
        border: 1px solid red;
    }
    div{
        width: 20px;
        margin : 5px;
    }
    .div1{
        padding-top: 10px;
        background: red;
    }
    .div2{
        background: blue;
        
    }
    .div3{
        background: yellow;
        
    }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="div1">测试文字1</div>
        <div class="div2">测试文字22</div>
        <div class="div3">3</div>
    </div>
</body>
</html>

stretch运行结果

stretch运行结果

baseline运行结果
baseline运行结果

flex-start运行结果
flex-start运行结果

flex-end运行结果
flex-end运行结果

center运行结果
center运行结果

**4.7 align-content **
  该属性定义了多跟轴线的对齐方式。如果项目只有一根轴线,则该属性不起作用。

属性值说明
stretch(默认值)轴线占满整个交叉轴
flex-start交叉轴的起点对齐
flex-end交叉轴的终点对齐
center交叉轴的中点对齐
space-between与交叉轴两端对齐,轴线之间的间隔平均分布
space-around每根轴线两侧的间隔都相等。所以轴线之间的间隔比边框的间隔大一倍

源代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>flex-wrap</title>
    <style type="text/css">
    .wrap{
        width: 200px;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        height: 200px;
        align-content: stretch;
        border: 1px solid red;
    }
    div{
        width: 50px;
        margin:5px;
    }
    .div1{
        background: red;
    }
    .div2{
        background: blue;
    }
    .div3{
        background: yellow;
    }
    .div4{
        background: green;
    }
    .div5{
        background: gray;
    }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="div1">1</div>
        <div class="div2">2</div>
        <div class="div3">3</div>
        <div class="div4">4</div>
        <div class="div5">5</div>
    </div>
</body>
</html>

stretch运行结果:


stretch运行结果

flex-start运行结果:


flex-start运行结果

flex-end运行结果:
flex-end运行结果

center运行结果:


center运行结果

space-between运行结果:
space-between运行结果

space-around运行结果:
space-around运行结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值