คุณได้เรียนรู้จากบทที่แล้วว่า Bootstrap จำเป็นต้องมีการประกอบด้วย องค์ประกอบเพื่อห่อเนื้อหาไซต์
ภาชนะใช้รองเนื้อหาภายใน และมีคลาสคอนเทนเนอร์อยู่ 2 คลาส:
คลาส .container
ให้ คอนเทนเนอร์ความกว้างคงที่ ที่ตอบสนอง
คลาส .container-fluid
จัดให้มี คอนเทนเนอร์แบบเต็มความกว้าง ซึ่งครอบคลุมความกว้างทั้งหมดของวิวพอร์ต
ใช้คลาส .container
เพื่อสร้างคอนเทนเนอร์ที่มีความกว้างคงที่และตอบสนอง
โปรดทราบว่าความกว้าง (max-width
) จะเปลี่ยนไปตามขนาดหน้าจอที่แตกต่างกัน:
Extra small <576px |
Small ≥576px |
Medium ≥768px |
Large ≥992px |
Extra Large ≥1200px |
XXL ≥1400px |
|
---|---|---|---|---|---|---|
max-width | 100% | 540px | 720px | 960px | 1140px | 1320px |
เปิดตัวอย่างด้านล่างและปรับขนาดหน้าต่างเบราว์เซอร์เพื่อดูว่าความกว้างของคอนเทนเนอร์จะเปลี่ยนที่เบรกพอยท์ต่างๆ:
<div class="container">
<h1>My First Bootstrap Page</h1>
<p>This is some text.</p>
</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">
<h1>My First Bootstrap Page</h1>
<p>This part is inside a .container class.</p>
<p>The .container class provides a responsive fixed width container.</p>
<p>Resize the browser window to see that the container width will change at different breakpoints.</p>
</div>
</body>
</html>
เบรกพอยต์ XXL (≥1400px) เป็น ใหม่ ใน Bootstrap 5 ในขณะที่เบรกพอยต์ที่ใหญ่ที่สุดใน Bootstrap 4 มีขนาดใหญ่พิเศษ (≥1200px)
ของไหล
(คอนเทนเนอร์เต็มความกว้าง) คอนเทนเนอร์ใช้คลาส .container-fluid
เพื่อสร้างคอนเทนเนอร์ที่มีความกว้างเต็ม ซึ่งจะขยายความกว้างทั้งหมดของหน้าจอเสมอ (width
อยู่เสมอ 100%
):
<div class="container-fluid">
<h1>My First Bootstrap Page</h1>
<p>This is some text.</p>
</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">
<h1>My First Bootstrap Page</h1>
<p>This part is inside a .container-fluid class.</p>
<p>The .container-fluid class provides a full width container, spanning the entire width of the viewport.</p>
</div>
</body>
</html>
ตามค่าเริ่มต้น คอนเทนเนอร์จะมีช่องว่างภายในด้านซ้ายและขวา โดยไม่มีช่องว่างภายในด้านบนหรือด้านล่าง ดังนั้นเราจึงมักใช้ยูทิลิตี้การเว้นวรรค เช่น ช่องว่างภายในเพิ่มเติมและระยะขอบเพื่อทำให้ดูดีขึ้น ตัวอย่างเช่น .pt-5
หมายถึง "เพิ่ม ช่องว่างด้านบน ขนาดใหญ่":
<div class="container pt-5"></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 pt-5">
<h1>My First Bootstrap Page</h1>
<p>This container has an extra top padding.</p>
<p>Try to remove the .pt-5 class to see the difference.</p>
</div>
</body>
</html>
โปรแกรมอรรถประโยชน์อื่นๆ เช่น เส้นขอบและสี มักใช้ร่วมกับคอนเทนเนอร์ด้วย:
<div class="container p-5 my-5 border"></div>
<div class="container p-5 my-5 bg-dark text-white"></div>
<div class="container p-5 my-5 bg-primary text-white"></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 p-5 my-5 border">
<h1>My First Bootstrap Page</h1>
<p>This container has a border and some extra padding and margins.</p>
</div>
<div class="container p-5 my-5 bg-dark text-white">
<h1>My First Bootstrap Page</h1>
<p>This container has a dark background color and a white text, and some extra padding and margins.</p>
</div>
<div class="container p-5 my-5 bg-primary text-white">
<h1>My First Bootstrap Page</h1>
<p>This container has a blue background color and a white text, and some extra padding and margins.</p>
</div>
</body>
</html>
คุณจะได้เรียนรู้เพิ่มเติมเกี่ยวกับยูทิลิตี้สีและเส้นขอบในบทต่อๆ ไป
คุณยังสามารถใช้คลาส .container-sm|md|lg|xl
เพื่อกำหนดว่าคอนเทนเนอร์ควรตอบสนองเมื่อใด
ความกว้างสูงสุด
ของคอนเทนเนอร์จะเปลี่ยนไปตามขนาดหน้าจอ/วิวพอร์ตที่แตกต่างกัน:
Class |
Extra small <576px |
Small ≥576px |
Medium ≥768px |
Large ≥992px |
Extra large ≥1200px |
XXL ≥1400px |
---|---|---|---|---|---|---|
.container-sm |
100% | 540px | 720px | 960px | 1140px | 1320px |
.container-md |
100% | 100% | 720px | 960px | 1140px | 1320px |
.container-lg |
100% | 100% | 100% | 960px | 1140px | 1320px |
.container-xl |
100% | 100% | 100% | 100% | 1140px | 1320px |
.container-xxl |
100% | 100% | 100% | 100% | 100% | 1320px |
<div class="container-sm">.container-sm</div>
<div class="container-md">.container-md</div>
<div class="container-lg">.container-lg</div>
<div class="container-xl">.container-xl</div>
<div class="container-xxl">.container-xxl</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 pt-3">
<h1>Responsive Containers</h1>
<p>Resize the browser window to see the effect.</p>
</div>
<div class="container-sm border">.container-sm</div>
<div class="container-md mt-3 border">.container-md</div>
<div class="container-lg mt-3 border">.container-lg</div>
<div class="container-xl mt-3 border">.container-xl</div>
<div class="container-xxl mt-3 border">.container-xxl</div>
</body>
</html>