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.

magazine.css Featured article spanning multiple cells

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.

✏️ Try It Yourself — Magazine Layout
Preview
Grid lines:
When you write 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.

product-grid.css Responsive cards without media queries

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!

✏️ Try It Yourself — Product Card Grid
Preview
Resize your browser:
Shrink the viewport width to see the grid reflow — cards automatically reorganize without any media queries. Try it in the preview above!

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.

layout-combined.css Grid for 2D layout, Flexbox for 1D components

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.

dense-packing.css Dense packing fills gaps automatically

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.

Important:
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 / 3 and grid-row: 1 / 3 to 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.
Build a News Website Homepage

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

Question 1 of 5
How do you create a featured article that spans 2 columns and 2 rows?
Question 2 of 5
When should you use Grid for layout inside components and Flexbox for alignment?
Question 3 of 5
What does grid-auto-flow: dense do?
Question 4 of 5
How do you make a grid responsive without media queries?
Question 5 of 5
Can you use both Grid AND Flexbox in the same design?
0
/5
Great work! You understand Grid layouts.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →