Bootstrap 5: ระบบกริด


สารบัญ

    แสดงสารบัญ

ระบบกริด

ระบบกริดของ Bootstrap สร้างขึ้นด้วย flexbox และอนุญาตให้มีคอลัมน์ได้สูงสุด 12 คอลัมน์ทั่วทั้งหน้า

หากคุณไม่ต้องการใช้ทั้ง 12 คอลัมน์แยกกัน คุณสามารถจัดกลุ่มคอลัมน์เข้าด้วยกันเพื่อสร้างคอลัมน์ที่กว้างขึ้นได้:

span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1 span 1
 span 4  span 4  span 4
span 4 span 8
span 6 span 6
span 12

ระบบกริดตอบสนอง และคอลัมน์จะจัดเรียงใหม่โดยอัตโนมัติตามขนาดหน้าจอ

ตรวจสอบให้แน่ใจว่าผลรวมรวมกันได้ไม่เกิน 12 คอลัมน์ (ไม่จำเป็นว่าคุณต้องใช้คอลัมน์ที่มีอยู่ทั้งหมด 12 คอลัมน์)

คลาสกริด

ระบบกริด Bootstrap 5 มีหกคลาส:

  • .col- (อุปกรณ์ขนาดเล็กพิเศษ - ความกว้างหน้าจอน้อยกว่า 576px)

  • .col-sm- (อุปกรณ์ขนาดเล็ก - ความกว้างหน้าจอเท่ากับหรือมากกว่า 576px)

  • .col-md- (อุปกรณ์ขนาดกลาง - ความกว้างหน้าจอเท่ากับหรือมากกว่า 768px)

  • .col-lg- (อุปกรณ์ขนาดใหญ่ - ความกว้างหน้าจอเท่ากับหรือมากกว่า 992px)

  • .col-xl- (อุปกรณ์ xlarge - ความกว้างหน้าจอเท่ากับหรือมากกว่า 1200px)

  • .col-xxl- (อุปกรณ์ xxlarge - ความกว้างหน้าจอเท่ากับหรือมากกว่า 1400px)

คลาสข้างต้นสามารถนำมารวมกันเพื่อสร้างเลย์เอาต์แบบไดนามิกและยืดหยุ่นมากขึ้น

เคล็ดลับ: แต่ละคลาสจะขยายขนาด ดังนั้นหากคุณต้องการตั้งค่าความกว้างเท่ากันสำหรับ sm และ md คุณจะต้องระบุ sm เท่านั้น

โครงสร้างพื้นฐานของ Bootstrap 5 Grid

ต่อไปนี้เป็นโครงสร้างพื้นฐานของตาราง Bootstrap 5:

<!-- Control the column width, and how they should appear on different devices -->
<div class="row">
  <div class="col-*-*"></div>
  <div class="col-*-*"></div>
</div>
<div class="row">
  <div class="col-*-*"></div>
  <div class="col-*-*"></div>
  <div class="col-*-*"></div>
</div>
<!-- Or let Bootstrap automatically handle the layout -->
<div class="row">
  <div class="col"></div>
  <div class="col"></div>
  <div class="col"></div>
</div>

ลองด้วยตัวคุณเอง →

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container-fluid mt-3">
  <h1>Basic Grid Structure</h1>
  <p>Resize the browser window to see the effect.</p>
  <p>The first, second and third row will automatically stack on top of each other when the screen is less than 576px wide.</p>
  
  <!-- Control the column width, and how they should appear on different devices -->
  <div class="row">
    <div class="col-sm-6 bg-primary text-white">50%</div>
    <div class="col-sm-6 bg-dark text-white">50%</div>
  </div>
  <br>
    
  <div class="row">
    <div class="col-sm-4 bg-primary text-white">33.33%</div>
    <div class="col-sm-4 bg-dark text-white">33.33%</div>
    <div class="col-sm-4 bg-primary text-white">33.33%</div>
  </div>
  <br>

  <!-- Or let Bootstrap automatically handle the layout -->
  <div class="row">
    <div class="col-sm bg-primary text-white">25%</div>
    <div class="col-sm bg-dark text-white">25%</div>
    <div class="col-sm bg-primary text-white">25%</div>
    <div class="col-sm bg-dark text-white">25%</div>
  </div>
  <br>
    
  <div class="row">
    <div class="col bg-primary text-white">25%</div>
    <div class="col bg-dark text-white">25%</div>
    <div class="col bg-primary text-white">25%</div>
    <div class="col bg-dark text-white">25%</div>
  </div>
</div>

</body>
</html>

ตัวอย่างแรก: สร้างแถว (<div class="row">) จากนั้น เพิ่มจำนวนคอลัมน์ที่ต้องการ (แท็กที่มีคลาส .col-*-* ที่เหมาะสม) ดาวดวงแรก (*) แสดงถึงการตอบสนอง: sm, md, lg, xl หรือ xxl ในขณะที่ดาวดวงที่สองแทนตัวเลข ซึ่งควรบวกกันได้สูงสุด 12 ดวงสำหรับแต่ละแถว

ตัวอย่างที่สอง: แทนที่จะเพิ่มตัวเลขลงในแต่ละ col ให้บูตสแตรปจัดการเค้าโครง เพื่อสร้างคอลัมน์ที่มีความกว้างเท่ากัน: สอง " องค์ประกอบ col"=ความกว้าง 50% สำหรับแต่ละคอลัมน์ ในขณะที่สามคอลัมน์=ความกว้าง 33.33% สำหรับแต่ละคอลัมน์ สี่คอลัมน์=ความกว้าง 25% เป็นต้น คุณยังสามารถใช้ .col-sm|md|lg|xl|xxl เพื่อทำให้คอลัมน์ตอบสนองได้

ตัวเลือกตาราง

ตารางต่อไปนี้สรุปวิธีการทำงานของระบบกริด Bootstrap 5 ในขนาดหน้าจอต่างๆ:

  Extra small (<576px) Small (>=576px) Medium (>=768px) Large (>=992px) Extra Large (>=1200px) XXL (>=1400px)
Class prefix .col- .col-sm- .col-md- .col-lg- .col-xl- .col-xxl-
Grid behaviour Horizontal at all times Collapsed to start, horizontal above breakpoints Collapsed to start, horizontal above breakpoints Collapsed to start, horizontal above breakpoints Collapsed to start, horizontal above breakpoints Collapsed to start, horizontal above breakpoints
Container width None (auto) 540px 720px 960px 1140px 1320px
Suitable for Portrait phones Landscape phones Tablets Laptops Laptops and Desktops Laptops and Desktops
# of columns 12 12 12 12 12 12
Gutter width 1.5rem (.75rem on each side of a column) 1.5rem (.75rem on each side of a column) 1.5rem (.75rem on each side of a column) 1.5rem (.75rem on each side of a column) 1.5rem (.75rem on each side of a column) 1.5rem (.75rem on each side of a column)
Nestable Yes Yes Yes Yes Yes Yes
Offsets Yes Yes Yes Yes Yes Yes
Column ordering Yes Yes Yes Yes Yes Yes