Chapter 11 of 17 · CSS for Beginners — Complete Free Tutorial

CSS Grid Essentials

Flexbox arranges along one axis; CSS Grid controls two dimensions at once — rows and columns together. It is the cleanest tool for page structure, dashboards, galleries, and card grids.

Your first grid

.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}
<div class="gallery">
  <img src="/img/1.jpg" alt="">
  <img src="/img/2.jpg" alt="">
  <img src="/img/3.jpg" alt="">
  <img src="/img/4.jpg" alt="">
</div>

Expected output: three equal columns; the fourth image wraps to a new row — rows are created automatically. 1fr means "one share of the available space"; repeat(3, 1fr) is shorthand for writing it three times.

Core container properties

PropertyDoes
grid-template-columns / -rowsDefine track sizes (200px, 1fr, auto, repeat())
gap / row-gap / column-gapSpace between tracks (never collapses)
justify-items / align-itemsDefault alignment of items inside their cells
justify-content / align-contentPositioning of the tracks themselves when extra space remains
grid-auto-rowsSize of implicitly created rows

Placing items precisely

Grid lines are numbered from 1 (or −1 from the end). You can place items with grid-column and grid-row:

.layout {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: minmax(90px, auto);
  gap: 1rem;
}

.hero      { grid-column: 1 / -1; }        /* full width */
.sidebar   { grid-column: 1 / 2; grid-row: 2 / 4; }
.main      { grid-column: 2 / -1; }
.feature   { grid-column: span 2; }        /* shorthand: start at next line, span 2 */

Expected output: a hero spanning the top, a sidebar taking one column across two rows, main content filling the rest, and two features side by side — pure CSS, source order untouched (good for accessibility).

The four placement forms

grid-column: 2 / 4;        /* line 2 to line 4 */
grid-column: 2 / span 2;   /* start at 2, span two tracks */
grid-column: span 2;       /* auto placement, span two */
grid-column: 1 / -1;       /* first line to last line */

auto vs fr vs min-content

Track sizeBehaviour
1frShare of leftover space; floors at min-content by default
autoSizes to content (min and max)
min-contentWidest unbreakable chunk (longest word)
minmax(200px, 1fr)At least 200, at most one share — the workhorse combo
fit-content(300px)Content-sized, capped at 300px

Row heights that just work

grid-auto-rows: minmax(120px, auto);  /* rows ≥ 120px, grow with content */
grid-template-rows: 80px 1fr 60px;    /* explicit: header, flexible body, footer */

Practical example: dashboard shell

.dashboard {
  display: grid;
  grid-template-columns: 220px 1fr;
  grid-template-rows: 64px 1fr;
  grid-template-areas:
    "sidebar header"
    "sidebar main";
  gap: 1rem;
  min-height: 100svh;
  padding: 1rem;
}
.dashboard-header { grid-area: header; }
.dashboard-side   { grid-area: sidebar; }
.dashboard-main   { grid-area: main; }

Named areas make the layout readable at a glance — the full technique is covered in Responsive Grid Patterns.

Grid vs Flexbox — choosing fast

  • One row of items with alignment → Flexbox.
  • Rows and columns, overlapping, or full page structure → Grid.
  • Components usually flex; page shells usually grid. They combine happily (grid parent, flex children).

Common mistakes

  • Hard-coding rows — let grid-auto-rows handle overflow instead of fixed heights that clip content.
  • Ignoring min-width — 1fr can still blow out from long words; add minmax(0, 1fr) for text-heavy columns (this is what prevents horizontal scrolling on phones — a bug class we fixed across this site).
  • Placing everything manually — auto-placement plus a few explicit spans is less brittle than numbering every cell.

Practice checklist

  1. Build a 3-column gallery with repeat(3, 1fr) and watch items wrap into rows.
  2. Create a header + sidebar + main layout using grid-template-areas.
  3. Use grid-column: span 2 to make one card double-width.

Next: make grids respond to screen size — Responsive Grid Patterns.

Quiz: CSS Grid Essentials

5 questions · 70% to pass

Frequently Asked Questions

CSS (Cascading Style Sheets) is the language that controls how HTML content looks — colours, fonts, spacing, layout, responsiveness and animation. HTML provides structure and meaning; CSS provides presentation. One stylesheet can restyle an entire website at once.

The fundamentals (selectors, box model, Flexbox, media queries) take about 2–3 weeks of daily practice. Grid, animations and architecture come with a little more practice. This 17-chapter series is designed for 3–4 weeks at a relaxed pace — faster if you type out every example.

Yes. HTML first, then CSS, then JavaScript. CSS and HTML together let you build complete, good-looking static pages, and almost every JavaScript framework assumes you already know CSS layout and selectors.

No — CSS is a styling language with selectors and property-value declarations, not variables, loops or conditionals. You can get far without any programming logic, and JavaScript adds the behaviour later.

Flexbox distributes space along one axis (a row or a column) and is ideal for components — toolbars, navbars, card rows. Grid controls rows and columns together and is ideal for page structure and card grids. They are complementary: many layouts use a grid page shell with flex components inside.

Usually one of three reasons: the selector does not match (check class names), a more specific rule wins (use the browser inspector to see computed styles and specificity), or the file is not linked/loads in the wrong order. The specificity chapter explains how the cascade decides.

Every element is a box of four layers: content, padding (inner space), border, and margin (outer space). With box-sizing: border-box, the width you set includes padding and border — the universal reset every project uses.

Use relative units, flexible grids like repeat(auto-fit, minmax(260px, 1fr)), images with max-width: 100%, and mobile-first media queries. The goal is zero horizontal scrolling from 320px up — the responsive chapters show the full workflow.

Not to learn. Frameworks are productivity tools built on the same CSS you are learning here; understanding the fundamentals makes any framework easier to use (and to debug when it fights you). Learn vanilla CSS first, then evaluate frameworks with an informed eye.

JavaScript for interactivity, then a build tool and version control (Git). With HTML, CSS and JavaScript you can build complete websites; after that, frameworks like React or server-side tools like Laravel Blade build on these fundamentals.