CSS: CSS Grid Practice

🗓 Last updated: 2026-11-30👤 Written by: Casey✓ Reviewed by: Dana🏷 Category: CSS Tutorial

1. 12-Column Grid System

The 12-column grid is a classic layout system in web development. Here's how to implement it with CSS Grid:

▶ Example

▶ Try it Yourself

2. Common Layout Patterns

(1) Holy Grail Layout

The most common page layout pattern:

▶ Example

▶ Try it Yourself

(2) Responsive Card Grid

▶ Example

▶ Try it Yourself

❓ FAQ

Q Why do grid systems use 12 columns?
A 12 is divisible by 2, 3, 4, and 6 — maximum flexibility. You can compose 1/2, 1/3, 1/4, 1/6, 2/3, 3/4 proportions from whole columns. Bootstrap and other major frameworks all use 12-column grids — it's the de facto standard.
Q Hand-written grid vs Bootstrap's grid — what's the difference?
A Hand-writing a grid is for understanding the principle; Bootstrap is the finished, packaged product. Real projects go straight to the framework, but knowing the internals means you can build your own when the framework falls short.
Q Flex-based grid or Grid-based grid — which is better?
A Flex grids handle one-dimensional rows easily but can't align across rows; Grid-based ones are strong in two dimensions and align rows and columns. Simple row distribution → Flex is enough; need grid-line alignment → grid-template-columns: repeat(12, 1fr) with grid-column: span 3.
Q How do I build the holy grail layout with CSS Grid?
A One grid-template-areas block does it: "header header header" "sidebar main aside" "footer footer footer" with grid-template-columns: 200px 1fr 200px, each region claimed via grid-area: header, and min-height: 100vh to fill the screen. No floats, no negative margins — three template lines are the whole skeleton.
Q How do I make a gallery / masonry layout with CSS Grid?
A Simple version: display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; with varied card heights for a staggered feel. A true "fill the shortest column first" masonry needs JS or CSS columns; the Grid version is tidier and suits galleries with similar card heights.
Q Practical CSS grid example — how do I lay out a dashboard?
A grid-template-columns: repeat(4, 1fr); gap: 15px; puts four stat cards in a row; inside each card a small h3 label plus a big number, and nth-child gives each card a different colored left border. To feature one card, give it grid-column: span 2 for double width.
Q What are col-offset classes? How do I center a content block?
A Offsets add margin-left placeholders: col-offset-3 leaves 3 columns (25%) empty on the left, and with col-6 the block sits centered — 3 empty + 6 content + 3 naturally left. Great for centered forms.
Q What do the col-sm / col-md / col-lg prefixes mean?
A Breakpoint prefixes: col-sm phone (<768px), col-md tablet (≥768px), col-lg desktop (≥1024px). Stack several on one element: class="col-sm-12 col-md-6 col-lg-3" means full width on phone, half on tablet, a quarter on desktop — whichever breakpoint matches wins.
Q How do I handle grid gutters? Why does .row need a negative margin?
A Gutters come from padding: each column gets padding: 0 10px, which also adds 10px at the outer edges — so the row container uses margin: 0 -10px to cancel it, or the percentage widths go wrong. The Grid version skips all that: gap: 20px in one line.

📖 Summary

📝 Exercises

  1. Implement a complete 12-column grid system, create an HTML page showing different column combinations (3-3-3-3, 4-4-4, 2-8-2, etc.).
  2. Use responsive grid to show single column on mobile, two on tablet, four on desktop.
  3. Use col-offset-3 to center a content block on the page.
  4. Re-implement the 12-column grid using Grid, compare code differences.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏