Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

ปรับขนาด Gap ใน CSS Grid ด้วย grid-gap, grid-row-gap, grid-column-gap

ใช้ grid-column-gap, grid-row-gap หรือ Shorthand grid-gap ปรับช่องว่างระหว่าง Grid Tracks

grid-column-gap Property

ช่องว่างระหว่าง Columns:

<!DOCTYPE html>
<html>
<head>
    <style>
        .container {
            display: grid;
            background-color: green;
            grid-template-columns: auto auto;
            padding: 20px;
            grid-column-gap: 20px;
        }
        .ele {
            background-color: orange;
            border: 2px solid gray;
            padding: 35px;
            font-size: 30px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Game Board</h1>
    <div class="container">
        <div class="ele">1</div>
        <div class="ele">2</div>
        <div class="ele">3</div>
        <div class="ele">4</div>
        <div class="ele">5</div>
        <div class="ele">6</div>
    </div>
</body>
</html>

grid-row-gap Property

ช่องว่างระหว่าง Rows:

<!DOCTYPE html>
<html>
<head>
    <style>
        .container {
            display: grid;
            background-color: green;
            grid-template-columns: auto auto;
            padding: 20px;
            grid-column-gap: 20px;
            grid-row-gap: 20px;
        }
        .ele {
            background-color: orange;
            border: 2px solid gray;
            padding: 35px;
            font-size: 30px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Game Board</h1>
    <div class="container">
        <div class="ele">1</div>
        <div class="ele">2</div>
        <div class="ele">3</div>
        <div class="ele">4</div>
        <div class="ele">5</div>
        <div class="ele">6</div>
    </div>
</body>
</html>

grid-gap Property (Shorthand)

รวม Row Gap และ Column Gap:

<!DOCTYPE html>
<html>
<head>
    <style>
        .container {
            display: grid;
            background-color: green;
            grid-template-columns: auto auto;
            padding: 20px;
            grid-gap: 20px 20px;
        }
        .ele {
            background-color: orange;
            border: 2px solid gray;
            padding: 35px;
            font-size: 30px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Game Board</h1>
    <div class="container">
        <div class="ele">1</div>
        <div class="ele">2</div>
        <div class="ele">3</div>
        <div class="ele">4</div>
        <div class="ele">5</div>
        <div class="ele">6</div>
    </div>
</body>
</html>

หมายเหตุ: ปัจจุบันมาตรฐานใหม่ใช้ gap, row-gap, column-gap รองรับทั้ง Grid และ Flexbox