Bootstrap 5: ตารางขนาดใหญ่


สารบัญ

    แสดงสารบัญ

ตัวอย่างกริดขนาดใหญ่

  XSmall Small Medium Large Extra Large XXL
Class prefix .col- .col-sm- .col-md- .col-lg- .col-xl- .col-xxl-
Screen width <576px >=576px >=768px >=992px >=1200px >=1400px

ในบทที่แล้ว เราได้นำเสนอตัวอย่างกริดพร้อมคลาสสำหรับอุปกรณ์ขนาดเล็กและขนาดกลาง เราใช้สอง div (คอลัมน์) และเราได้แบ่ง 25%/75% บนอุปกรณ์ขนาดเล็ก และ 50%/50% บนอุปกรณ์ขนาดกลาง:

<div class="col-sm-3 col-md-6">....</div>
<div class="col-sm-9 col-md-6">....</div>

แต่ในอุปกรณ์ขนาดใหญ่ การออกแบบอาจจะดีกว่าหากแบ่ง 33%/66%

อุปกรณ์ขนาดใหญ่ถูกกำหนดให้มีความกว้างหน้าจอตั้งแต่ 992 พิกเซลถึง 1199 พิกเซล

สำหรับอุปกรณ์ขนาดใหญ่ เราจะใช้คลาส .col-lg-*:

<div class="col-sm-3 col-md-6 col-lg-4">....</div>
<div class="col-sm-9 col-md-6 col-lg-8">....</div>

ตอนนี้ Bootstrap จะพูดว่า "ในขนาดที่เล็ก ให้ดูคลาสที่มี -sm- อยู่ในนั้นแล้วใช้พวกมัน ส่วนขนาดกลาง ให้ดูที่คลาสที่มี -md- > ในนั้นและใช้สิ่งเหล่านั้น ในขนาดใหญ่ ให้ดูคลาสที่มีคำว่า -lg- อยู่ในนั้นแล้วใช้สิ่งเหล่านั้น

ตัวอย่างต่อไปนี้จะส่งผลให้มีการแบ่ง 25%/75% บนอุปกรณ์ขนาดเล็ก การแยก 50%/50% บนอุปกรณ์ขนาดกลาง และการแบ่ง 33%/66% บนอุปกรณ์ขนาดใหญ่ xlarge และ xxlarge บนอุปกรณ์ขนาดเล็กพิเศษ มันจะซ้อนกันโดยอัตโนมัติ (100%):

.col-sm-3 .col-md-6 .col-lg-4
.col-sm-9 .col-md-6 .col-lg-8

ตัวอย่าง

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-3 col-md-6 col-lg-4">
      <p>Lorem ipsum...</p>
    </div>
    <div class="col-sm-9 col-md-6 col-lg-8">
      <p>Sed ut perspiciatis...</p>
    </div>
  </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>Large Grid</h1>
  <p>The following example will result in a 25%/75% split on small devices, a 50%/50% split on medium devices, and a 33%/66% split on large, xlarge and xxlarge devices. On extra small devices, it will automatically stack (100%).</p>
  <p>Resize the browser window to see the effect.</p>
  <div class="container-fluid">
    <div class="row">
      <div class="col-sm-3 col-md-6 col-lg-4 bg-primary text-white">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
      </div>
      <div class="col-sm-9 col-md-6 col-lg-8 bg-dark text-white">
        Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
      </div>
    </div>
  </div>
</div>

</body>
</html>

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

ใช้ขนาดใหญ่เท่านั้น

ในตัวอย่างด้านล่าง เราระบุเฉพาะคลาส .col-lg-6 (โดยไม่มี .col-md-* และ/หรือ .col-sm-*) ซึ่งหมายความว่าอุปกรณ์ขนาดใหญ่ xlarge และ xxlarge จะแบ่ง 50%/50% อย่างไรก็ตาม สำหรับอุปกรณ์ขนาดกลาง ขนาดเล็ก และขนาดเล็กพิเศษ อุปกรณ์จะซ้อนกันในแนวตั้ง (ความกว้าง 100%):

ตัวอย่าง

<div class="container-fluid">
  <div class="row">
    <div class="col-lg-6">
      <p>Lorem ipsum...</p>
    </div>
    <div class="col-lg-6">
      <p>Sed ut perspiciatis...</p>
    </div>
  </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>Large Grid</h1>
  <p>The following example will result in a 50%/50% split on large, xlarge and xxlarge devices (<strong>992px and above</strong>). On medium, small and extra small devices, it will automatically stack (100%).</p>
  <p>Resize the browser window to see the effect.</p>
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-6 bg-primary text-white">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br>
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
      </div>
      <div class="col-lg-6 bg-dark text-white">
        Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
      </div>
    </div>
  </div>
</div>

</body>
</html>

คอลัมน์เค้าโครงอัตโนมัติ

ใน Bootstrap 5 มีวิธีง่ายๆ ในการสร้างคอลัมน์ที่มีความกว้างเท่ากันสำหรับอุปกรณ์ทั้งหมด: เพียงลบตัวเลขออกจาก .col-lg-* และใช้เฉพาะ .col-lg ในจำนวน องค์ประกอบ col ที่ระบุ Bootstrap จะรับรู้ว่ามีกี่คอลัมน์ และแต่ละคอลัมน์จะมีความกว้างเท่ากัน

หากขนาดหน้าจอ น้อยกว่า 992px คอลัมน์จะเรียงซ้อนกันในแนวนอน:

<!-- Two columns: 50% width on large and up-->
<div class="row">
  <div class="col-lg">1 of 2</div>
  <div class="col-lg">2 of 2</div>
</div>
<!-- Four columns: 25% width on large and up -->
<div class="row">
  <div class="col-lg">1 of 4</div>
  <div class="col-lg">2 of 4</div>
  <div class="col-lg">3 of 4</div>
  <div class="col-lg">4 of 4</div>
</div>
1 of 2
2 of 2

1 of 4
2 of 4
3 of 4
4 of 4

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

<!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>Responsive Auto Layout Columns</h1>
  <p>In Bootstrap 5, there is an easy way to create equal width columns: just use the <code>.col-lg</code> class on a specified number of col elements. Bootstrap will recognize how many columns there are, and each column will get the same width.</p>
  <p>If the screen size is <strong>less than 992px</strong>, the columns will stack horizontally.</p>
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg bg-primary text-white">1 of 2</div>
      <div class="col-lg bg-dark text-white">2 of 2</div>
    </div>  
  </div>
  <br>
  
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg bg-primary text-white">1 of 4</div>
      <div class="col-lg bg-dark text-white">2 of 4</div>
      <div class="col-lg bg-primary text-white">3 of 4</div>
      <div class="col-lg bg-dark text-white">4 of 4</div>
    </div>  
  </div>
</div>

</body>
</html>