DAY 25 · PRACTICAL EXAMPLES

CSS Grid Examples -Website Layout Explained

Real layouts — gallery, dashboard, magazine, form, calendar and centering — each with the live result and the exact CSS behind it, finishing with a complete one-page website built on grid.

Day 25 of 30BeginnerHands-on~25 min

A Gallery With Spanning Items

A grid of equal tiles where some items stretch across more cells. grid-auto-rows sets the height of every row, span makes an item cover several tracks, and grid-auto-flow: dense backfills any gaps.

LIVE RESULT
style.css
.gallery {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 64px;      /* every row is 64px tall */
  grid-auto-flow: dense;     /* fill holes left by big items */
  gap: 8px;
}

.wide { grid-column: span 2; }
.tall { grid-row: span 2; }
.big  { grid-column: span 2; grid-row: span 2; }
NOTICE

You only define the columns. Rows are created automatically as items need them, and grid-auto-rows controls how tall those automatic rows are.

Dashboard Tiles With Column Spans

Four equal columns hold four stat tiles. The chart then spans three columns and the side panel takes the last one, so everything lines up on the same column lines.

LIVE RESULT
Users 2.4k
Sales 890
Orders 126
Rating 4.8
Chart (span 3)
Side (span 1)
style.css
.dashboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.chart { grid-column: span 3; }   /* wide panel */
.side  { grid-column: span 1; }   /* narrow panel */

@media (max-width: 560px) {
  .dashboard { grid-template-columns: repeat(2, 1fr); }
  .chart, .side { grid-column: span 2; }
}

Draw It With grid-template-areas

Name the regions, then draw the layout as text. Here the main story covers two rows on the left, two small stories stack on the right and a wide banner runs along the bottom.

LIVE RESULT
main story
story A
story B
banner
style.css
.magazine {
  display: grid;
  grid-template-columns: 2fr 1fr;
  grid-template-rows: 70px 70px 50px;
  grid-template-areas:
    "main a"
    "main b"
    "wide wide";
  gap: 8px;
}

.main { grid-area: main; }
.a    { grid-area: a; }
.b    { grid-area: b; }
.wide { grid-area: wide; }
TIP

To change the layout on mobile, you only rewrite the area strings inside a media query. The HTML never changes.

Aligned Labels and Inputs

A two-column grid with a fixed label column and a flexible input column keeps every field perfectly aligned, with no tables and no floats.

LIVE RESULT
style.css
.form {
  display: grid;
  grid-template-columns: 100px 1fr;  /* label | input */
  gap: 10px 14px;                    /* row gap | column gap */
  align-items: center;
}

.form button {
  grid-column: 2;                    /* sit under the inputs */
  justify-self: start;               /* don't stretch */
}

A Seven-Column Calendar

Seven equal columns, one per weekday. Items fill each row left to right and wrap on their own, so a whole month needs nothing more than repeat(7, 1fr). A single grid-column-start offsets the first day.

LIVE RESULT
style.css
.calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);  /* Mon to Sun */
  gap: 4px;
  text-align: center;
}

.day:first-of-type {
  grid-column-start: 4;   /* month starts on a Thursday */
}

Centering With place-items

Grid has a shortcut that flexbox does not: place-items: center centers an item both ways in a single line.

LIVE RESULT
I am centered
style.css
.box {
  display: grid;
  place-items: center;   /* align-items + justify-items */
  height: 130px;         /* the container needs a height */
}
REMEMBER

place-items is shorthand for align-items and justify-items. Use place-content to position the whole grid instead of the items inside it.

Putting It All Together: A Complete Grid Website

Here is a full one-page website, "Wanderly Travel", built with grid from top to bottom. The preview is generated from the exact code underneath it, so what you see is what the code does.

LIVE PREVIEW (SCROLL INSIDE THE FRAME)
WHERE EACH GRID FEATURE IS USED

Header: auto 1fr auto. Hero: two columns plus a mosaic with spans. Destinations: repeat(3, 1fr) with one big featured card. Features: auto-fit, minmax(). Stats: repeat(4, 1fr). Booking form: 1fr auto. Footer: 2fr 1fr 1fr.

index.html
style.css
HOW TO RUN IT

Download both files (or copy the code), save them in one folder as index.html and style.css, then open index.html in your browser. Try changing the colors in :root.

Assignment: Build the Full Page

HANDS-ON EXERCISE

Create grid.html and grid.css and combine the pieces above into one simple website.

  1. Build a gallery with repeat(4, 1fr), grid-auto-rows: 64px and one item that spans two columns and two rows.
  2. Add a stats row of four tiles using repeat(4, 1fr).
  3. Create a three-area layout with grid-template-areas (main, side, banner).
  4. Build a contact form with a 100px 1fr label and input grid.
  5. Add a card section using repeat(auto-fit, minmax(200px, 1fr)).
  6. Finish with a footer using 2fr 1fr 1fr.
  7. Bonus: add one media query that changes the layout to a single column under 700px.
Course Overview