📡 You're offline — showing cached content
New version available!
Quick Access

CSS Grid: Two-Dimensional Layouts

Master CSS Grid — rows and columns, placing items, template areas, auto-fill, minmax, and responsive page layouts.

EzyCoders Admin August 1, 2026 8 min read 8 views
CSS Grid: Two-Dimensional Layouts
Share: Twitter LinkedIn WhatsApp

What is it?

CSS Grid is a two-dimensional layout system for placing elements into rows and columns simultaneously. It is the most powerful layout tool available in CSS.

Why does it matter?

CSS Grid replaces complex float hacks with clean, readable layout code. A 12-column responsive layout that took 100 lines of framework CSS takes 10 lines with Grid. Essential for any layout beyond a simple navbar.

Master CSS Grid — rows and columns, placing items, template areas, auto-fill, minmax, and responsive page layouts.

Real-World Use Cases

  • 📰 Newspaper-style layout - Grid creates a complex multi-column layout with some articles spanning 2 columns — impossible cleanly with Flexbox.
  • 🎨 Portfolio grid - Auto-fill columns create a responsive grid that adds columns as screen width increases, no media queries needed.
  • 📋 Dashboard layout - Named template areas define header, sidebar, main, and footer — readable and maintainable.
  • 🛒 Product catalogue - minmax(250px, 1fr) creates columns that are at least 250px wide and fill available space responsively.

Defining Grid Columns and Rows

.three-col { grid-template-columns: 200px 1fr 1fr; }

/* fr unit: fraction of available space */
.equal { grid-template-columns: repeat(3, 1fr); }

/* Gap between grid cells */
.container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 24px;
}

Placing Items and Template Areas

.featured {
    grid-column: 1 / 3;   /* spans 2 columns */
    grid-row:    1 / 2;
}
.wide { grid-column: span 2; }  /* shorthand */

/* Template areas — visual layout in CSS */
.page {
    display: grid;
    grid-template-areas:
        "header  header"
        "sidebar main"
        "footer  footer";
    grid-template-columns: 250px 1fr;
}
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }

Responsive Grid with auto-fill

/* Add as many columns as fit — no media queries needed! */
.cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 24px;
}
/* At 900px: 3 columns   At 600px: 2 columns   At 320px: 1 column */

/* auto-fill keeps empty column slots */
/* auto-fit collapses empty columns — items stretch to fill */

Q: What is the difference between auto-fill and auto-fit?

Both automatically create as many columns as fit. The difference appears when there are fewer items than fit on one row. auto-fill keeps empty column slots (items do not stretch); auto-fit collapses empty columns so existing items stretch to fill the row. Use auto-fill for fixed max-size items; use auto-fit when items should expand to fill space.

🧩
Test Your Knowledge
50 questions · Takes ~1500 seconds

Answer these questions to confirm you understood the article. Instant feedback on every answer.

Q1
What is the primary purpose of this topic in DevOps development?
Q2
In a DevOps technical interview, what do interviewers test?
Q3
What is the time/space complexity consideration for this DevOps topic?
Q4
When should a DevOps developer apply this technique?
Q5
What is the most common mistake when using this DevOps pattern?
Q6
Best practice for DevOps concept #6?
Q7
Best practice for DevOps concept #7?
Q8
Best practice for DevOps concept #8?
Q9
Best practice for DevOps concept #9?
Q10
Best practice for DevOps concept #10?
Q11
Best practice for DevOps concept #11?
Q12
Best practice for DevOps concept #12?
Q13
Best practice for DevOps concept #13?
Q14
Best practice for DevOps concept #14?
Q15
Best practice for DevOps concept #15?
Q16
Best practice for DevOps concept #16?
Q17
Best practice for DevOps concept #17?
Q18
Best practice for DevOps concept #18?
Q19
Best practice for DevOps concept #19?
Q20
Best practice for DevOps concept #20?
Q21
Best practice for DevOps concept #21?
Q22
Best practice for DevOps concept #22?
Q23
Best practice for DevOps concept #23?
Q24
Best practice for DevOps concept #24?
Q25
Best practice for DevOps concept #25?
Q26
Best practice for DevOps concept #26?
Q27
Best practice for DevOps concept #27?
Q28
Best practice for DevOps concept #28?
Q29
Best practice for DevOps concept #29?
Q30
Best practice for DevOps concept #30?
Q31
Best practice for DevOps concept #31?
Q32
Best practice for DevOps concept #32?
Q33
Best practice for DevOps concept #33?
Q34
Best practice for DevOps concept #34?
Q35
Best practice for DevOps concept #35?
Q36
Best practice for DevOps concept #36?
Q37
Best practice for DevOps concept #37?
Q38
Best practice for DevOps concept #38?
Q39
Best practice for DevOps concept #39?
Q40
Best practice for DevOps concept #40?
Q41
Best practice for DevOps concept #41?
Q42
Best practice for DevOps concept #42?
Q43
Best practice for DevOps concept #43?
Q44
Best practice for DevOps concept #44?
Q45
Best practice for DevOps concept #45?
Q46
Best practice for DevOps concept #46?
Q47
Best practice for DevOps concept #47?
Q48
Best practice for DevOps concept #48?
Q49
Best practice for DevOps concept #49?
Q50
Best practice for DevOps concept #50?
EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment