DevToolBoxFREE
BlogAdvertise

CSS Grid Mastery: คู่มือสมบูรณ์พร้อมตัวอย่างจริง 2026

14 นาทีโดย DevToolBox

CSS Grid คือระบบ layout ที่ทรงพลังที่สุดที่เคยเพิ่มเข้ามาใน CSS ในปี 2026 ฟีเจอร์ Grid Level 2 เช่น subgrid ได้รับการรองรับอย่างสมบูรณ์

พื้นฐาน Grid: Columns, Rows และการวางตำแหน่ง

Grid สร้างระบบพิกัดสองมิติ

/* CSS Grid fundamentals */
.grid-container {
    display: grid;

    /* Define columns */
    grid-template-columns: 200px 1fr 1fr;        /* fixed + flexible */
    grid-template-columns: repeat(3, 1fr);        /* 3 equal columns */
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); /* responsive */

    /* Define rows */
    grid-template-rows: auto 1fr auto;            /* header, content, footer */

    /* Gaps */
    gap: 1rem;            /* both row and column gap */
    row-gap: 0.5rem;
    column-gap: 1.5rem;

    /* Alignment */
    justify-items: stretch;   /* horizontal alignment of cells */
    align-items: start;       /* vertical alignment of cells */
    justify-content: center;  /* horizontal alignment of the grid */
    align-content: center;    /* vertical alignment of the grid */
}

/* Place items */
.item {
    grid-column: 1 / 3;      /* span from column 1 to 3 */
    grid-row: 1 / 2;
    grid-column: span 2;      /* span 2 columns */
    grid-area: 1 / 1 / 3 / 3; /* row-start / col-start / row-end / col-end */
}

grid-template-areas: การกำหนด Layout แบบภาพ

grid-template-areas ให้กำหนด layout แบบภาพด้วย ASCII

/* grid-template-areas: name regions visually */
.page-layout {
    display: grid;
    grid-template-areas:
        "header  header  header"
        "sidebar main    main"
        "sidebar aside   aside"
        "footer  footer  footer";
    grid-template-columns: 250px 1fr 300px;
    grid-template-rows: 60px 1fr auto 80px;
    min-height: 100vh;
    gap: 1rem;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.aside   { grid-area: aside; }
.footer  { grid-area: footer; }

/* Responsive: stack on mobile */
@media (max-width: 768px) {
    .page-layout {
        grid-template-areas:
            "header"
            "main"
            "aside"
            "sidebar"
            "footer";
        grid-template-columns: 1fr;
        grid-template-rows: auto;
    }
}

auto-fill vs auto-fit: Responsive โดยไม่ต้องใช้ Media Queries

auto-fill และ auto-fit กับ minmax() สร้าง grids ที่ responsive โดยไม่มี media queries

/* auto-fill vs auto-fit — the most important Grid distinction */

/* auto-fill: creates as many columns as possible, even if empty */
.gallery-fill {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 1rem;
}
/* Result: always fills the row with tracks, even with empty ones */

/* auto-fit: collapses empty tracks, stretches filled items */
.gallery-fit {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
}
/* Result: items expand to fill available space */

/* Practical: responsive card grid (no media queries!) */
.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(300px, 100%), 1fr));
    gap: 1.5rem;
}
/* min(300px, 100%) prevents overflow on very small screens */

minmax() และการกำหนดขนาดในตัว

minmax(min, max) กำหนดขนาดขั้นต่ำและสูงสุดของ grid track

/* minmax() — set minimum and maximum track size */

/* Card layout: minimum 250px, maximum 1fr */
.cards {
    display: grid;
    grid-template-columns: repeat(3, minmax(250px, 1fr));
}

/* Intrinsic sizing with min-content / max-content */
.table-grid {
    display: grid;
    grid-template-columns: max-content 1fr max-content;
    /* First and last columns size to content, middle fills rest */
}

/* fit-content() — like minmax(auto, max-content) but capped */
.nav {
    display: grid;
    grid-template-columns: fit-content(200px) 1fr fit-content(200px);
}

/* Dense packing: fill holes left by spanning items */
.masonry-like {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    grid-auto-rows: 150px;
    grid-auto-flow: dense; /* fills gaps automatically */
}

.tall-item {
    grid-row: span 2; /* takes 2 rows */
}
.wide-item {
    grid-column: span 2; /* takes 2 columns */
}

Subgrid: CSS Grid Level 2 (2026)

Subgrid ให้ grid ที่ซ้อนกันเข้าร่วมในการกำหนด track ของ grid แม่

/* Subgrid — CSS Grid Level 2, fully supported in 2026 */
/* Allows nested grids to align to parent grid lines */

/* The problem subgrid solves: */
.card-container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
}

/* Without subgrid: each card's internal layout is independent */
/* Card titles and content don't align across cards */

/* With subgrid: */
.card {
    display: grid;
    grid-row: span 3; /* card spans 3 implicit rows */
    grid-template-rows: subgrid; /* inherits parent row tracks */
}
/* Now all cards share the same row grid — titles align! */

.card .card-header { grid-row: 1; }
.card .card-body   { grid-row: 2; }
.card .card-footer { grid-row: 3; }

/* Subgrid for columns too */
.sidebar-layout {
    display: grid;
    grid-template-columns: 1fr 3fr;
}

.main-content {
    display: grid;
    grid-column: 2;
    grid-template-columns: subgrid; /* aligns to parent columns */
}

/* Browser support: Chrome 117+, Firefox 71+, Safari 16+ — all major browsers */

Grid Animations และ Transitions

เบราว์เซอร์สมัยใหม่รองรับ transition สำหรับ grid-template-columns และ grid-template-rows

/* Grid transitions and animation */

/* Animate grid column width on hover */
.sidebar-layout {
    display: grid;
    grid-template-columns: 250px 1fr;
    transition: grid-template-columns 0.3s ease;
}

.sidebar-layout:has(.sidebar:hover) {
    grid-template-columns: 350px 1fr; /* expand on hover */
}

/* CSS-only accordion with Grid */
.accordion-item {
    display: grid;
    grid-template-rows: auto 0fr; /* collapsed: 0 fraction */
    transition: grid-template-rows 0.3s ease;
    overflow: hidden;
}

.accordion-item.open {
    grid-template-rows: auto 1fr; /* expanded: 1 fraction */
}

.accordion-content {
    min-height: 0; /* required for 0fr to work */
}

CSS Grid vs Flexbox: ควรใช้อะไรเมื่อไหร่

ScenarioCSS GridFlexbox
Page layout (header/footer/sidebar)PerfectAwkward
Navigation bar itemsWorksPerfect
Card grid (equal columns)PerfectNeeds JS for equal height
Centering contentEasy (place-items)Easy (align/justify)
Vertical list of itemsWorksPerfect
Complex form layoutPerfectComplex
Responsive without breakpointsauto-fill/fitLimited

แนวปฏิบัติที่ดีที่สุด

  • ใช้ Grid สำหรับ layout ระดับหน้า Flexbox สำหรับ components
  • ใช้ repeat(auto-fill, minmax()) สำหรับ card grid แบบ responsive
  • ตั้งชื่อ zones ด้วย grid-template-areas สำหรับ layouts ที่ซับซ้อน
  • ใช้ subgrid เพื่อจัดตำแหน่งเนื้อหาใน grid elements แยกกัน
  • ใช้ gap แทน margin สำหรับระยะห่างของ grid

คำถามที่พบบ่อย

Grid กับ Flexbox ควรใช้อะไร?

CSS Grid เหมาะสำหรับ layout 2D Flexbox เหมาะสำหรับ layout 1D

grid-template-columns กับ grid-auto-columns ต่างกันอย่างไร?

grid-template-columns กำหนด columns แบบ explicit grid-auto-columns กำหนดขนาดของ columns แบบ implicit

Subgrid กับ nested grid ต่างกันอย่างไร?

Subgrid สืบทอด track definitions จากแม่ ทำให้ child items จัดตำแหน่งตาม grid lines ของแม่ได้

grid-auto-flow: dense คืออะไร?

พยายามเติมช่องว่างที่เหลือจาก items ที่ข้ามไป

สามารถผสม Grid และ Flexbox ได้ไหม?

ได้แน่นอน ทั้งสองทำงานในระดับต่างกันและเสริมกัน

เครื่องมือที่เกี่ยวข้อง

𝕏 Twitterin LinkedIn
บทความนี้มีประโยชน์ไหม?

Stay Updated

Get weekly dev tips and new tool announcements.

No spam. Unsubscribe anytime.

Partner Picks

Sponsor this article

Place your product next to this developer topic with tracked clicks.

Ask about article sponsorship

ลองเครื่องมือที่เกี่ยวข้อง

This site uses cookies for analytics and to display ads. By continuing to browse, you agree. Privacy Policy