flex佈局父項常見屬性justify-content

_XinXinM_發表於2020-12-30

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>

在這裡插入圖片描述

相關文章