Everyday Programmer
Work with Jimmy
Systems

The Same Card Needs Two Layouts on Desktop

A card can have 720px in the main column and 320px in a sidebar, even in the same browser window. Here’s how to make it adapt to the space it actually gets.

4 min readCSS, container queries, responsive design
At the same desktop viewport width, a sample card shows its image beside the text in a 720-pixel main column and above the text in a 320-pixel sidebar.

Test the card twice without resizing the browser

Imagine a dashboard with a 720px main column and a 320px sidebar. Both show the same resource card: a cover image, a title, and an “Open guide” link. The wide card can put the image alongside the content. The sidebar version needs the image above the title so the text and link have room.

It’s easy to miss this in a component preview. Resize the window and the card looks good on desktop and mobile. Then drop it into a sidebar on desktop and it breaks. A viewport breakpoint has the same answer for both instances because they share one browser window.

I’d start by rendering both cards side by side in the same story or local fixture. That makes the failure obvious and gives you somewhere to prove the fix.

Give the card a width it can use

Let the page decide how wide each column should be. Let the card decide what fits inside the space it receives.

Use a block wrapper as the named query container and style the card inside it. Start with the stacked layout. If the wrapper is wide enough, switch the card to two columns.

resource-card.html
html

The sample cover is decorative, so its alt text is empty. If the image adds information a reader needs, describe it instead. The image and guide paths are illustrative.

resource-card.css
css

The threshold is for the wrapper’s content box. At 32rem, this example spends 9rem on the image, 1rem on the gap, and 2rem on the card’s padding. That leaves roughly 20rem for the text. It’s a starting point: adjust it using real headlines, translations and enlarged text, rather than matching a device breakpoint.

One detail that’s easy to trip over: a size query applies styles to descendants of the queried container. If you put the container declaration on the card itself, you can query that size to change its children, but you can’t use that same query to restyle the card’s own grid. That’s why this example has a wrapper. Keep the wrapper in a slot with an allocated width; inline-size containment changes how it contributes to intrinsic sizing, so don’t rely on the card to size a shrink-to-fit parent.

Know what to test

  • Same viewport, different placements. Render the 720px and 320px versions together. Both should use exactly the same markup and keep the same link.
  • Either side of the breakpoint. At a 16px root font size, try 511px and 512px. Check where the title and link wrap, not just whether the image moves.
  • More demanding content. Try a long translated headline, an unbroken string, and browser zoom. Inspect the whole page for unintended horizontal scrolling. For WCAG 2.2’s reflow criterion, test at a width equivalent to 320 CSS pixels for vertically scrolling content, except where a two-dimensional layout is essential.
  • Keyboard and fallback. Tab to the link in both placements and make sure the focus ring is visible. Disable the container rule and check that the stacked layout remains usable.

I’d still use media queries for page-level decisions like when the dashboard itself goes from two columns to one. Keep that separate from when the card puts its image beside the text. Then a third team can put the same card in a drawer without asking for another “mobile” prop.

References

Keep reading

Related articles