หน้าเว็บจำนวนมากอิงตามมุมมองตาราง ซึ่งหมายความว่าหน้าเว็บจะถูกแบ่งออกเป็นคอลัมน์:
การใช้มุมมองกริดมีประโยชน์มากเมื่อออกแบบหน้าเว็บ ช่วยให้วางองค์ประกอบต่างๆ บนเพจได้ง่ายขึ้น
มุมมองตารางแบบตอบสนองมักจะมี 12 คอลัมน์ และมีความกว้างรวม 100% และจะย่อและขยายเมื่อคุณปรับขนาดหน้าต่างเบราว์เซอร์
ตัวอย่าง: มุมมองตารางที่ตอบสนอง:
มาเริ่มสร้างมุมมองกริดแบบตอบสนองกันดีกว่า
ขั้นแรกตรวจสอบให้แน่ใจว่าองค์ประกอบ HTML ทั้งหมดมีคุณสมบัติ box-sizing
ตั้งค่าเป็น เส้นขอบกล่อง
. เพื่อให้แน่ใจว่าช่องว่างภายในและเส้นขอบจะรวมอยู่ในความกว้างและความสูงรวมของ องค์ประกอบ
เพิ่มรหัสต่อไปนี้ใน CSS ของคุณ:
* {
box-sizing: border-box;
}
อ่านเพิ่มเติมเกี่ยวกับคุณสมบัติ box-sizing
ในบท CSS Box Sizing ของเรา
ตัวอย่างต่อไปนี้แสดงหน้าเว็บแบบตอบสนองอย่างง่าย โดยมีสองคอลัมน์:
.menu {
width: 25%;
float: left;
}
.main {
width: 75%;
float: left;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
box-sizing: border-box;
}
.header {
border: 1px solid red;
padding: 15px;
}
.menu {
width: 25%;
float: left;
padding: 15px;
border: 1px solid red;
}
.main {
width: 75%;
float: left;
padding: 15px;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="menu">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="main">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
<p>Resize the browser window to see how the content respond to the resizing.</p>
</div>
</body>
</html>
ตัวอย่างข้างต้นใช้ได้หากหน้าเว็บมีเพียงสองคอลัมน์เท่านั้น
อย่างไรก็ตาม เราต้องการใช้มุมมองกริดแบบตอบสนองที่มี 12 คอลัมน์เพื่อให้มีมากขึ้น ควบคุมหน้าเว็บ
ก่อนอื่นเราต้องคำนวณเปอร์เซ็นต์สำหรับหนึ่งคอลัมน์: 100%/12 คอลัมน์ = 8.33%.
แล้วเรา สร้างหนึ่งคลาสสำหรับแต่ละคอลัมน์ทั้ง 12 คอลัมน์ class="col-"
และตัวเลข กำหนดจำนวนคอลัมน์ที่ส่วนควรขยาย:
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width:
100%;}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
box-sizing: border-box;
}
.header {
border: 1px solid red;
padding: 15px;
}
.row::after {
content: "";
clear: both;
display: table;
}
[class*="col-"] {
float: left;
padding: 15px;
border: 1px solid red;
}
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
</style>
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="row">
<div class="col-3">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="col-9">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
<p>Resize the browser window to see how the content respond to the resizing.</p>
</div>
</div>
</body>
</html>
คอลัมน์ทั้งหมดเหล่านี้ควรลอยไปทางซ้าย และมีช่องว่างภายใน 15px:
[class*="col-"] {
float: left;
padding: 15px;
border: 1px solid red;
}
แต่ละแถวควรอยู่ใน <div>
จำนวนคอลัมน์ ภายในแถวควรรวมกันได้มากถึง 12 เสมอ:
<div class="row">
<div class="col-3">...</div> <!-- 25% -->
<div class="col-9">...</div> <!-- 75% -->
</div>
คอลัมน์ในแถวจะลอยไปทางซ้ายทั้งหมด ดังนั้น นำออกจากการไหลของหน้า และองค์ประกอบอื่นๆ จะถูกวางราวกับว่าไม่มีคอลัมน์อยู่ เพื่อป้องกันสิ่งนี้ เราจะทำ เพิ่มสไตล์ที่เคลียร์โฟลว์:
.row::after {
content: "";
clear: both;
display: table;
}
เรายังต้องการเพิ่มสไตล์และสีเพื่อทำให้ดูดีขึ้น:
html {
font-family: "Lucida Sans", sans-serif;
}
.header {
background-color: #9933cc;
color: #ffffff;
padding: 15px;
}
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.menu li {
padding: 8px;
margin-bottom: 7px;
background-color :#33b5e5;
color: #ffffff;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
.menu li:hover {
background-color: #0099cc;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
box-sizing: border-box;
}
.row::after {
content: "";
clear: both;
display: table;
}
[class*="col-"] {
float: left;
padding: 15px;
}
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
html {
font-family: "Lucida Sans", sans-serif;
}
.header {
background-color: #9933cc;
color: #ffffff;
padding: 15px;
}
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.menu li {
padding: 8px;
margin-bottom: 7px;
background-color: #33b5e5;
color: #ffffff;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
.menu li:hover {
background-color: #0099cc;
}
</style>
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="row">
<div class="col-3 menu">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="col-9">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
<p>Resize the browser window to see how the content respond to the resizing.</p>
</div>
</div>
</body>
</html>
โปรดสังเกตว่าหน้าเว็บในตัวอย่างดูไม่ดีเมื่อคุณปรับขนาดหน้าต่างเบราว์เซอร์ให้มีความกว้างน้อยมาก ในบทถัดไปคุณจะได้เรียนรู้วิธีแก้ไขปัญหานั้น