SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
CSS
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10
Try this code live
Lesson 27 of 35

CSS Grid Tutorial — Columns, Rows & Layouts Guide

4 min read · Free · No login needed

Flexbox is brilliant for a row or a column. But a whole page is not a row or a column — it is both at once. A page has a header across the top, a sidebar and main content side by side, and a footer across the bottom. That is a two-dimensional layout, and that is exactly what CSS Grid is for.

Diagram — see how it fits together
Diagram: CSS Grid

Grid works like a table drawn with CSS: you tell the container how many columns and rows it has, and each item drops into a cell. You can make an item span two columns, or place it by name using grid areas — which is how most modern page layouts are built.

The grid container

Like Flexbox, Grid starts with display: grid on the container. Then grid-template-columns defines the columns. The fr unit means "one share of the free space": 1fr 1fr 1fr gives three equal columns, and 2fr 1fr gives a wide column and a narrow one. gap puts even space between all the cells.

Syntax — three equal columns

.gallery {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 15px;
}

There is no need to set widths on the items at all. The grid decides the columns, and each item takes the next free cell automatically, left to right, row by row.

Grid areas — a whole page in five lines

For a page layout, named areas are the clearest way to work. You draw a little map with grid-template-areas, giving each part a name — header, sidebar, main, footer — and then each element says which area it belongs to with grid-area. The map itself shows the layout at a glance.

.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}
header { grid-area: header; }
aside { grid-area: sidebar; }

Each row of the map is one line of text in quotes, and each word is one cell. Writing "header header" means the header spans both columns. The number of words in each row must match the number of columns.

Note: repeat(3, 1fr) is a shortcut for 1fr 1fr 1fr. It is handy for galleries: grid-template-columns: repeat(3, 1fr) makes three equal columns, and changing the 3 to a 4 changes the whole gallery at once.
Tip: Use Grid when you need rows and columns together — a page layout or a photo gallery. Use Flexbox when you only need one line — a row of buttons or a navigation bar. Many real pages use both: Grid for the page, Flexbox inside the parts.

Common mistakes

  • Setting display: grid on the items instead of the container — like Flexbox, Grid only works from the parent.
  • Writing a different number of words in each grid-template-areas row — every row must have exactly as many words as there are columns.
  • Using fixed pixel columns like 200px 200px 200px for a gallery — on a phone the grid overflows. Use 1fr or repeat() so the columns shrink.
  • Forgetting grid-area on an element — the element lands in the next free cell instead of the area you drew for it.
  • Choosing Grid for a single row of links — Flexbox is simpler there; save Grid for layouts that need rows and columns together.

Remember

  • Grid is two-dimensional: rows and columns together.
  • The container gets display: grid; grid-template-columns defines the columns.
  • 1fr means one share of the free space; repeat(3, 1fr) makes three equal columns.
  • grid-template-areas draws a map of the layout; grid-area places each element on it.
  • Use Grid for page layouts and galleries, Flexbox for single rows.

Example explained

  • .page { display: grid; } — the whole page becomes a grid container, so its four parts can be placed on a map instead of stacking one after another.
  • grid-template-columns: 200px 1fr — the sidebar gets a fixed 200px column and the main content takes whatever space is left.
  • grid-template-areas — the three-row map: the header spans both columns, then sidebar and main sit side by side, then the footer spans both columns again.
  • header { grid-area: header; } and the matching rules for the sidebar, main, and footer — each element is told which named area on the map it belongs to.
  • .gallery { grid-template-columns: repeat(3, 1fr); } — inside the main area, a second smaller grid makes three equal photo boxes with a 10px gap between them.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>School Page with CSS Grid</title>
<style>
* { box-sizing: border-box; }
body { font-family: Arial, sans-serif; margin: 0; }
.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}
header { grid-area: header; background-color: #04aa6d; color: white; padding: 20px; text-align: center; }
aside { grid-area: sidebar; background-color: #f1f1f1; padding: 20px; }
main { grid-area: main; padding: 20px; }
footer { grid-area: footer; background-color: #333; color: white; padding: 15px; text-align: center; }
.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.gallery div {
  background-color: #ddd;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
</head>
<body>

<div class="page">

<header>
<h1>KAMALDNP Academy</h1>
<p>Learn by doing</p>
</header>

<aside>
<h3>Subjects</h3>
<p>HTML</p>
<p>CSS</p>
<p>Python</p>
<p>Java</p>
</aside>

<main>
<h2>Welcome Students</h2>
<p>This page is built with CSS Grid. The header and footer stretch across both columns, while the sidebar and main content sit side by side.</p>
<div class="gallery">
<div>Photo 1</div>
<div>Photo 2</div>
<div>Photo 3</div>
</div>
</main>

<footer>
<p>support@kamaldnp.com | Varanasi, UP</p>
</footer>

</div>

</body>
</html>

Output

Output — how it looks in the browser
Output of CSS Grid Tutorial — Columns, Rows & Layouts Guide

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What is the difference between Grid and Flexbox?

Grid is two-dimensional — rows and columns together — while Flexbox handles one row or column at a time. Use Grid for page layouts, Flexbox for single rows.

2. What does 1fr mean?

One share of the free space. repeat(3, 1fr) makes three equal columns.

3. What does grid-template-areas do?

It draws a map of the layout in words, and grid-area places each element on that map.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

WordPress Website Development Course (10 Days)
₹399
View
Master Python + AI Skills in 30 Days
₹499
View
AI Power Skills in 2 Days | Learn AI Text, Image, Video and Voice Tools
View
Browse all courses
CSS — Lessons
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →