🎨 CSS Grid Guide

1. พื้นฐาน Grid Container

HTML
<div class="grid-basic">
  <div class="grid-item">
    Item 1
  </div>
  <div class="grid-item">
    Item 2
  </div>
  <div class="grid-item">
    Item 3
  </div>
</div>
CSS
.grid-basic {
  display: grid;
  grid-template-columns: 
    1fr 1fr 1fr;
  gap: 10px;
}

.grid-item {
  padding: 20px;
  background: 
    linear-gradient(...);
}
Item 1
Item 2
Item 3

2. Grid Template Columns

CSS
/* กำหนดขนาดคอลัมน์ */

/* 3 คอลัมน์เท่ากัน */
grid-template-columns:
  1fr 1fr 1fr;

/* คอลัมน์คงที่ + ยืดหยุ่น */
grid-template-columns:
  200px 1fr;

/* ทำซ้ำ 3 ครั้ง */
grid-template-columns:
  repeat(3, 1fr);

/* Auto-fit responsive */
grid-template-columns:
  repeat(auto-fit,
  minmax(150px, 1fr));

gap: 15px;

2 columns: 200px 1fr

Fixed
Flexible

3 columns: 1fr 1fr 1fr

1
2
3

Auto-fit responsive

A
B
C
D
E

3. Grid Template Rows (แถว)

CSS
/* กำหนดความสูงแถว */

grid-template-columns:
  1fr 1fr;

/* กำหนดความสูง 3 แถว */
grid-template-rows:
  60px 100px 60px;

gap: 10px;

/* ผลลัพธ์: 2 × 3 grid */
/* (2 columns × 3 rows) */
R1 C1
R1 C2
R2 C1
R2 C2
R3 C1
R3 C2

4. Grid Span (ยืดไปหลายคอลัมน์/แถว)

HTML & CSS
<div class="grid-span">
  <div>Item 1</div>
  <div class="span-2">
    Item 2 (ยืด 2 columns)
  </div>
  <div>Item 3</div>
</div>

.span-2 {
  grid-column: span 2;
}

.span-2-rows {
  grid-row: span 2;
}
Item 1
Item 2 (span 2 columns)
Item 3
Item 4
Item 5
(span 2 rows)
Item 6

5. Grid Template Areas (พื้นที่ชื่อ)

CSS
.grid-areas {
  display: grid;
  grid-template-columns:
    200px 1fr;
  grid-template-areas:
    "header header"
    "sidebar content"
    "footer footer";
}

.grid-header {
  grid-area: header;
}

.grid-sidebar {
  grid-area: sidebar;
}

.grid-content {
  grid-area: content;
}
Header
Sidebar
Content

6. Grid Auto Flow (ทิศทางการจัดเรียง)

CSS
/* Row (ค่าเริ่มต้น) */
grid-template-columns:
  repeat(3, 1fr);
grid-auto-flow: row;

/* Column */
grid-template-rows:
  repeat(2, 80px);
grid-auto-flow: column;

/* Dense (เติมช่องว่าง) */
grid-auto-flow:
  row dense;

gap: 10px;

grid-auto-flow: row

1
2
3
4
5
6

grid-auto-flow: column

1
2
3
4
5
6

7. Grid Alignment (การจัดชิด)

CSS
/* จัดเรียงแนวนอน (column) */
justify-items: start;
justify-items: center;
justify-items: end;
justify-items: stretch;

/* จัดเรียงแนวตั้ง (row) */
align-items: start;
align-items: center;
align-items: end;
align-items: stretch;

/* จัด content ทั้งหมด */
justify-content: center;
align-content: center;

justify-items: center

Item
Item
Item
Item

align-items: center

Item
Item
Item
Item

8. Responsive Grid (ปรับตามขนาดจอ)

CSS
/* Auto-fit: ขึ้นแถวใหม่อัตโนมัติ */
grid-template-columns:
  repeat(auto-fit,
  minmax(200px, 1fr));

/* Auto-fill: เติมคอลัมน์ว่าง */
grid-template-columns:
  repeat(auto-fill,
  minmax(150px, 1fr));

gap: 15px;

/* Media query */
@media (max-width: 768px) {
  grid-template-columns:
    repeat(2, 1fr);
}

repeat(auto-fit, minmax(200px, 1fr))

📱
Card 1
💻
Card 2
🖥️
Card 3
Card 4

9. Dashboard Layout (โครงสร้างซับซ้อน)

CSS
.dashboard-grid {
  display: grid;
  grid-template-columns:
    repeat(4, 1fr);
  grid-template-rows:
    auto auto auto auto;
  gap: 15px;
}

.dashboard-large {
  grid-column: span 2;
  grid-row: span 2;
}

.dashboard-item {
  min-height: 80px;
}
Large Item
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8

📚 สรุป CSS Grid Properties

Property ค่า (Values) คำอธิบาย
display grid เปิดใช้ Grid
grid-template-columns 1fr 1fr 1fr, repeat(3, 1fr) กำหนดคอลัมน์
grid-template-rows 60px 100px 60px กำหนดแถว
grid-template-areas "header header" พื้นที่ชื่อ
grid-column span 2, 1 / 3 ยืดคอลัมน์
grid-row span 2, 1 / 3 ยืดแถว
gap 10px, 10px 20px ระยะห่าง
justify-items start, center, end จัดชิดแนวนอน
align-items start, center, end จัดชิดแนวตั้ง
auto-fit repeat(auto-fit, minmax(...)) Responsive อัตโนมัติ

⚖️ Flexbox vs Grid

Flexbox Grid
📏 มิติเดียว (1D) 🗂️ สองมิติ (2D)
⬅️➡️ Row หรือ Column ⬆️⬇️⬅️➡️ Row และ Column พร้อมกัน
🧩 ใช้เมื่อรายการอยู่ในแถวหรือคอลัมน์ 📊 ใช้สำหรับ layout ที่ซับซ้อน
🔀 ปรับขนาดอย่างยืดหยุ่น 📐 ควบคุมตำแหน่ง 2D ได้แม่นยำ
⚡ ใช้ง่าย สำหรับ navigation 🎯 ซับซ้อน แต่ใช้ layout ทั้งหน้า