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.
.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; }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.
.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.
.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; }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.
.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.
.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.
.box {
display: grid;
place-items: center; /* align-items + justify-items */
height: 130px; /* the container needs a height */
}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.
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.
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
Create grid.html and grid.css and combine the pieces above into one simple website.
- Build a gallery with
repeat(4, 1fr),grid-auto-rows: 64pxand one item that spans two columns and two rows. - Add a stats row of four tiles using
repeat(4, 1fr). - Create a three-area layout with
grid-template-areas(main, side, banner). - Build a contact form with a
100px 1frlabel and input grid. - Add a card section using
repeat(auto-fit, minmax(200px, 1fr)). - Finish with a footer using
2fr 1fr 1fr. - Bonus: add one media query that changes the layout to a single column under 700px.