Magazine layouts with spanning items
Real-world layouts often feature a prominent featured item (like a featured article in a magazine) that spans multiple columns and rows, alongside smaller supporting items. CSS Grid makes this natural and responsive.
In a magazine layout, the featured article might span 2 columns and 2 rows, while 4 smaller articles fill the remaining space. This creates a visual hierarchy that draws readers to the main story first.
The key is using grid-column and grid-row to define how many cells an item spans. This approach works well for varying content sizes because you only need to modify one item's span — the rest auto-fill.
grid-column: 1 / 3, you're saying "start at line 1, end at line 3." This spans 2 columns (lines 1→2→3). Similarly, grid-row: 1 / 3 spans 2 rows.
Responsive card grids with auto-fill and auto-fit
For product listings or portfolios, you want cards to reflow based on screen size without media queries. The auto-fill and auto-fit keywords handle this automatically.
auto-fill creates as many tracks as fit the container, even if they're empty. auto-fit collapses empty tracks, making remaining cards stretch to fill space. Most of the time, auto-fit is what you want.
The formula repeat(auto-fit, minmax(250px, 1fr)) means: "Create as many tracks as fit, each with a minimum width of 250px and maximum of 1fr (equal share)." On desktop, you get 4+ columns; on tablet, 2–3; on mobile, 1 column. Responsive design without a single media query!
Combining Grid + Flexbox
The best layouts use both Grid and Flexbox together. Grid handles the overall 2D page layout; Flexbox handles 1D alignment within components. They're not competitors — they're complementary.
A perfect example: use Grid for the page layout (header, main content, sidebar), and Flexbox inside each card to stack the image, title, and button vertically.
Notice the pattern:
- Grid: Defines the overall structure (3 columns: sidebar, main, ads)
- Flexbox: Inside each card, stacks elements vertically (image, title, description, footer)
- Flexbox: Inside the footer, aligns buttons horizontally
This combination gives you the best of both worlds: precise 2D positioning with Grid, and flexible 1D alignment with Flexbox.
Grid auto-flow and dense packing
By default, grid-auto-flow: row places items left-to-right, top-to-bottom. But grid-auto-flow: column places items top-to-bottom, left-to-right. And grid-auto-flow: dense fills gaps by placing smaller items in empty spaces.
Without dense, the grid respects document order strictly, which can leave gaps. With dense, the grid looks ahead and fills empty cells with smaller items, creating a more compact, magazine-like layout. This is perfect for Pinterest-style masonry grids.
dense reorders the visual layout but not the DOM. Screen readers and keyboard navigation still follow document order. Use sparingly and test accessibility.
Key takeaways
- Grid spanning: Use
grid-column: 1 / 3andgrid-row: 1 / 3to create featured items in magazines or dashboards. - Responsive without media queries:
repeat(auto-fit, minmax(250px, 1fr))makes grids responsive automatically. - Combine Grid + Flexbox: Grid for 2D page layout, Flexbox for 1D component alignment. They work together beautifully.
- Grid auto-flow: dense: Fills gaps in the grid with smaller items, creating compact masonry layouts.
- Grid is 2D, Flexbox is 1D: Choose the right tool. If you're aligning in one direction, use Flexbox. If you need 2D control, use Grid.
Challenge: Create a news website homepage with the following layout:
- One featured article (2x2 grid cells) with a large purple gradient background and white text
- Four smaller article cards arranged around it in a 3-column grid
- A sidebar on the right spanning the full height (use grid-row: span)
- Each card uses Flexbox internally to align the title and description
- Make it responsive: on smaller screens, stack everything vertically
Start with the magazine layout editor above and extend it. Include a sidebar using grid-row: 1 / 5.
Hint: Create a 4-column grid. Let the featured article span grid-column: 1 / 3; grid-row: 1 / 3; to take up 2x2. The sidebar should be grid-column: 4; grid-row: 1 / 5; to span from top to bottom. Use grid-template-rows: repeat(3, auto) for the rows. Cards go in the remaining cells.
Test your knowledge
You just built professional layouts.
Imagine what you'll create next.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →