Grid Container ประกอบด้วยรายการกริดที่จัดเรียงเป็นคอลัมน์และแถว
องค์ประกอบลูกโดยตรงของคอนเทนเนอร์กริดจะกลายเป็นรายการกริดโดยอัตโนมัติ
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid black;
text-align: center;
font-size: 30px;
}
</style>
</head>
<body>
<h1>Grid Container</h1>
<p>A Grid Container consists of grid items arranged in columns and rows</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
<p>Direct child elements(s) of the grid container automatically becomes grid items.</p>
</body>
</html>
หากต้องการให้องค์ประกอบ HTML ทำงานเป็นคอนเทนเนอร์กริด คุณต้องตั้งค่าคุณสมบัติ display
เป็น grid
หรือ < รหัส class="w3-codespan">อินไลน์กริด.
คอนเทนเนอร์กริดประกอบด้วยรายการกริดที่วางอยู่ภายในคอลัมน์และแถว
คุณสมบัติ grid-template-columns
กำหนดจำนวนคอลัมน์ในเค้าโครงตารางของคุณ และสามารถกำหนดความกว้างของแต่ละคอลัมน์ได้
ค่านี้เป็นรายการที่คั่นด้วยช่องว่าง โดยแต่ละค่าจะกำหนดความกว้างของคอลัมน์ที่เกี่ยวข้อง
หากคุณต้องการให้เค้าโครงตารางกริดของคุณมี 4 คอลัมน์ ให้ระบุความกว้างของ 4 คอลัมน์ หรือ "อัตโนมัติ" หากคอลัมน์ทั้งหมดควรมีความกว้างเท่ากัน
สร้างตารางที่มี 4 คอลัมน์:
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The grid-template-columns Property</h1>
<p>You can use the <em>grid-template-columns</em> property to specify the number of columns in your grid layout.</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</body>
</html>
หมายเหตุ: หากคุณมีมากกว่า 4 รายการในตาราง 4 คอลัมน์ ตารางจะโดยอัตโนมัติ เพิ่มแถวใหม่เพื่อใส่รายการ
คุณสมบัติ grid-template-columns
ยังสามารถใช้เพื่อระบุขนาด (ความกว้าง) ของคอลัมน์ได้อีกด้วย
กำหนดขนาดสำหรับ 4 คอลัมน์:
.grid-container {
display: grid;
grid-template-columns: 80px 200px auto 40px;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: 80px 200px auto 30px;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The grid-template-columns Property</h1>
<p>Use the <em>grid-template-columns</em> property to specify the size of each column.</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</body>
</html>
grid-template-rows
คุณสมบัติกำหนดความสูงของแต่ละแถว
ค่านี้เป็นรายการที่คั่นด้วยช่องว่าง โดยแต่ละค่าจะกำหนดความสูงของแถวที่เกี่ยวข้อง:
.grid-container {
display: grid;
grid-template-rows: 80px 200px;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-template-rows: 80px 200px;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The grid-template-rows Property</h1>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
<p>Use the <em>grid-template-rows</em> property to specify the size (height) of each row.</p>
</body>
</html>
คุณสมบัติ justify-content
ใช้เพื่อจัดแนวตารางทั้งหมดภายในคอนเทนเนอร์
หมายเหตุ: ความกว้างรวมของตารางจะต้องน้อยกว่าความกว้างของคอนเทนเนอร์ เพื่อให้คุณสมบัติ justify-content
จึงจะมีผลใดๆ
.grid-container {
display: grid;
justify-content: space-evenly;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
justify-content: space-evenly;
grid-template-columns: 50px 50px 50px; /*Make the grid smaller than the container*/
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The justify-content Property</h1>
<p>Use the <em>justify-content</em> property to align the grid inside the container.</p>
<p>The value "space-evenly" will give the columns equal amount of space between, and around them:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
justify-content: space-around;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
justify-content: space-around;
grid-template-columns: 50px 50px 50px; /*Make the grid smaller than the container*/
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The justify-content Property</h1>
<p>Use the <em>justify-content</em> property to align the grid inside the container.</p>
<p>The value "space-around" will give the columns equal amount of space around them:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
justify-content: space-between;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
justify-content: space-between;
grid-template-columns: 50px 50px 50px; /*Make the grid smaller than the container*/
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The justify-content Property</h1>
<p>Use the <em>justify-content</em> property to align the grid inside the container.</p>
<p>The value "space-between" will give the columns equal amount of space between them:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
justify-content: center;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
justify-content: center;
grid-template-columns: 50px 50px 50px; /*Make the grid smaller than the container*/
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The justify-content Property</h1>
<p>Use the <em>justify-content</em> property to align the grid inside the container.</p>
<p>The value "center" will align the grid in the middle of the container:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
justify-content: start;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
justify-content: start;
grid-template-columns: 50px 50px 50px; /*Make the grid smaller than the container*/
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The justify-content Property</h1>
<p>Use the <em>justify-content</em> property to align the grid inside the container.</p>
<p>The value "start" will align the grid at the beginning of the container:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
justify-content: end;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
justify-content: end;
grid-template-columns: 50px 50px 50px; /*Make the grid smaller than the container*/
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The justify-content Property</h1>
<p>Use the <em>justify-content</em> property to align the grid inside the container.</p>
<p>The value "end" will align the grid at the end of the container:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
คุณสมบัติ align-content
ใช้เพื่อ แนวตั้ง จัดแนวตารางทั้งหมดภายในคอนเทนเนอร์
หมายเหตุ: ความสูงรวมของตารางจะต้องน้อยกว่าความสูงของคอนเทนเนอร์เพื่อให้คุณสมบัติ align-content
จึงจะมีผลใดๆ
.grid-container {
display: grid;
height: 400px;
align-content: center;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
height: 400px;
align-content: center;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The align-content Property</h1>
<p>Use the <em>align-content</em> property to vertically align the grid inside the container.</p>
<p>The value "center" will align the rows in the middle of the container:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
height: 400px;
align-content: space-evenly;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
height: 400px;
align-content: space-evenly;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The align-content Property</h1>
<p>Use the <em>align-content</em> property to vertically align the grid inside the container.</p>
<p>The value "space-evenly" will give the rows equal amount of space between, and around them:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
height: 400px;
align-content: space-around;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
height: 400px;
align-content: space-around;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The align-content Property</h1>
<p>Use the <em>align-content</em> property to vertically align the grid inside the container.</p>
<p>The value "space-around" will give the rows equal amount of space around them:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
height: 400px;
align-content: space-between;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
height: 400px;
align-content: space-between;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The align-content Property</h1>
<p>Use the <em>align-content</em> property to vertically align the grid inside the container.</p>
<p>The value "space-between" will give the rows equal amount of space between them:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
height: 400px;
align-content: start;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
height: 400px;
align-content: start;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The align-content Property</h1>
<p>Use the <em>align-content</em> property to vertically align the grid inside the container.</p>
<p>The value "start" will align the rows at the beginning of the container:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
.grid-container {
display: grid;
height: 400px;
align-content: end;
}
ลองด้วยตัวคุณเอง →
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
height: 400px;
align-content: end;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
</style>
</head>
<body>
<h1>The align-content Property</h1>
<p>Use the <em>align-content</em> property to vertically align the grid inside the container.</p>
<p>The value "end" will align the rows at the end of the container:</p>
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>