flex布局父项常见属性justify-content

博客介绍了flex布局父项常见属性,着重讲解了justify - content属性,该属性用于设置主轴上子元素的排列方式。包括从头排、尾部排、居中对齐、平均分配剩余空间、先两边贴边再平分剩余空间等情况,还提及主轴为y轴时的相关设置。

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

1.flex布局父项常见属性
justify-content设置主轴上的子元素排列方式
justify-content 属性定义了项目在主轴上的对齐方式
注意:使用这个属性之前一定要确定好主轴是 哪个
在这里插入图片描述
设置主轴上子元素的排列方式从头排

justify-content:flex-start;

<!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>
        div{
            /* 它的所有子元素自动成为容器成员 */
            display:flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            /* 默认的主轴式x轴 row */
            /* justify-content:是设置主轴上子元素的排列方式 */
            justify-content:flex-start;
        }
        div span {
            width: 150px;
            height:100px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
    </div>
</body>
</html>

在这里插入图片描述
设置主轴上子元素的排列方式尾部排
justify-content:flex-end;

<!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>
        div{
            /* 它的所有子元素自动成为容器成员 */
            display:flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            /* 默认的主轴式x轴 row */
            /* justify-content:是设置主轴上子元素的排列方式 */
            /* justify-content:flex-start; */
            justify-content:flex-end;
        }
        div span {
            width: 150px;
            height:100px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
    </div>
</body>
</html>

在这里插入图片描述
设置主轴上子元素的排列方式居中对齐
justify-content:center;

<!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>
        div{
            /* 它的所有子元素自动成为容器成员 */
            display:flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            /* 默认的主轴式x轴 row */
            /* justify-content:是设置主轴上子元素的排列方式 */
            /* justify-content:flex-start; */
            justify-content:center;
        }
        div span {
            width: 150px;
            height:100px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
    </div>
</body>
</html>

平均分配剩余空间
justify-content:space-around

<!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>
        div{
            /* 它的所有子元素自动成为容器成员 */
            display:flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            /* 默认的主轴式x轴 row */
            /* justify-content:是设置主轴上子元素的排列方式 */
            /* justify-content:flex-start; */
            /* justify-content:center; */
            /* 平分剩余空间 */
            justify-content:space-around;
        }
        div span {
            width: 150px;
            height:100px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
    </div>
</body>
</html>

在这里插入图片描述
先两边贴边再平分剩余空间

justify-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>
        div{
            /* 它的所有子元素自动成为容器成员 */
            display:flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            /* 默认的主轴式x轴 row */
            /* justify-content:是设置主轴上子元素的排列方式 */
            /* justify-content:flex-start; */
            /* justify-content:center; */
            /* 平分剩余空间 */
            /* justify-content:space-around; */
            /* 先两边贴边,再分配剩余的空间  */
            justify-content:space-between;
        }
        div span {
            width: 150px;
            height:100px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
    </div>
</body>
</html>

在这里插入图片描述

主轴为y轴:
先两边贴边再平分剩余空间(重要)

justify-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>
        div{
            /* 它的所有子元素自动成为容器成员 */
            display:flex;
            width: 800px;
            height: 400px;
            background-color: pink;
            /* 默认的主轴式x轴 row */
            /* justify-content:是设置主轴上子元素的排列方式 */
            /* justify-content:flex-start; */
            /* justify-content:center; */
            /* 平分剩余空间 */
            /* justify-content:space-around; */
            /* 先两边贴边,再分配剩余的空间  */
            flex-direction: column;
            justify-content:space-between;
        }
        div span {
            width: 150px;
            height:100px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <!-- <span>4</span> -->
    </div>
</body>
</html>

在这里插入图片描述

主轴为y轴:
居中对齐:

flex-direction: column;
justify-content:center;
<!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>
        div{
            /* 它的所有子元素自动成为容器成员 */
            display:flex;
            width: 800px;
            height: 400px;
            background-color: pink;
            /* 默认的主轴式x轴 row */
            /* justify-content:是设置主轴上子元素的排列方式 */
            /* justify-content:flex-start; */
            /* justify-content:center; */
            /* 平分剩余空间 */
            /* justify-content:space-around; */
            /* 先两边贴边,再分配剩余的空间  */
            flex-direction: column;
            justify-content:center;
        }
        div span {
            width: 150px;
            height:100px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
        <!-- <span>4</span> -->
    </div>
</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值