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

กำหนดความกว้างคอลัมน์ใน CSS Grid Layout ด้วย grid-template-columns

ใช้ grid-template-columns กำหนดความกว้างของแต่ละ Column ใน Grid Layout

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <style>
        .container {
            display: grid;
            background-color: gray;
            grid-template-columns: 120px 80px 50px; /* 3 Columns ความกว้างต่างกัน */
            padding: 20px;
            grid-gap: 20px;
        }
        .container > div {
            background-color: yellow;
            border: 2px solid gray;
            padding: 35px;
            font-size: 30px;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Game Board</h1>
    <div class="container">
        <div class="ele1">1</div>
        <div class="ele2">2</div>
        <div class="ele3">3</div>
        <div class="ele4">4</div>
        <div class="ele5">5</div>
        <div class="ele6">6</div>
    </div>
</body>
</html>

รูปแบบอื่น:

  • auto auto — ความกว้างตามเนื้อหา
  • 1fr 2fr 1fr — สัดส่วน Fraction Unit
  • repeat(3, 1fr) — 3 คอลัมน์เท่ากัน
  • minmax(100px, 1fr) — ขั้นต่ำ 100px สูงสุด 1fr