CSS: CSS Grid Practice
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 Yourself2. 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
- Grid systems are a time-tested classic layout solution
- Implementing a 12-column grid with Flexbox isn't complex — key is understanding percentage widths and flex-wrap
- 12 columns offer flexible combinations with responsive breakpoints
- While many projects use Bootstrap or Tailwind grids, implementing once yourself truly teaches the underlying principles
📝 Exercises
- 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.).
- Use responsive grid to show single column on mobile, two on tablet, four on desktop.
- Use
col-offset-3to center a content block on the page. - Re-implement the 12-column grid using Grid, compare code differences.