UtilitiesPremium

Container Query Cards

A card that fills in as room allows: image and title first, a line and a button added at 280 px, then the image moving to the left of the text at 400 px. CSS only.

CSS@containerLayout

Updated

This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Utilities has 43 effects, including 3 free. Explore the category →

3 usage examples

Producer directory of an open-air market — rows or tiles view — Container Query Cards example 1

① Producer directory of an open-air market — rows or tiles view

WhenThe visitor picks “Rows”: one card per row, 516 px wide, image on the left, a line and a “See the stall” button. Switching to “Tiles”: three cards per row, 167 px each, only the image and the producer's name remain.
WhyThe switch only touches the page's grid; each card decides by itself what to show from the room it gets.
SettingsNine roots in a page grid (one or three columns), a two-button switch with aria-pressed. Until the first click, a scene timer alternates the two views every 1.1 s for the capture. Cream background, serif title, one gradient and one pictogram per producer.
Recipe card that tucks into a thumbnail while you read the steps — a flour mill's website — Container Query Cards example 2

② Recipe card that tucks into a thumbnail while you read the steps — a flour mill's website

WhenAt the top of the prune far recipe, the card spreads over 508 px: image on the left, servings and time, a “Shopping list” button. As soon as you scroll the steps, it slips to the bottom-right corner with only 208 px: the image and the name. Back at the top, it is large again.
WhyThe recipe stays at hand while reading without hiding the steps: as a thumbnail, the card keeps just enough to be recognised.
SettingsOne root in a frame that the page moves and shrinks (a class set once the reading area has scrolled more than 20 px; position and width animated over 0.35 s, the card passing through the middle step on the way). Until you scroll yourself, a timer scrolls the area every 0.7 s for the capture. Brown background, honey button, wheat ear drawn as a line.
This week's outings in a rotating band — a caving club's website — Container Query Cards example 3

③ This week's outings in a rotating band — a caving club's website

WhenThree outings share a band. The featured one takes 296 px: image, title, line and sign-up button. The other two wait as 104 px thumbnails, reduced to image and name. Every 1.4 s the next one takes the large slot.
WhyA single component works as thumbnail and as full card; the band only has to hand out widths.
SettingsThree roots in a row; their widths are animated by the page (4.2 s @keyframes offset by a third). The large card stays in the middle state: it would need 400 px to go to two columns. Yellow button, one gradient per outing.

How it works

The card has two parts: .acw-img, an 80 px indigo gradient band with a pictogram, and .acw-body, holding an h5, a paragraph and a button.acw-btn. In the base rule the paragraph and the button are display: none: in a narrow slot only the image and the title remain, 123 px tall (138 px under 180 px wide, where the demo title wraps onto two lines).

The root .fx-0755-root is the measured container (container: acw-card / inline-size). First threshold, 280 px: the paragraph and the button appear under the title, the card stays vertical. Second threshold, 400 px: .acw-card switches to grid-template-columns: 140px 1fr; the image becomes a 140 px column filling the full height (100 px at least) and the text sits to its right.

Going from one state to the next is immediate: showing an element or changing the column count does not interpolate in CSS. The component ships no script; the button has no action until you give it one.

Accessibility

  • No action when small: under 280 px the button is display: none. It is gone for everyone — mouse, keyboard, screen reader. If the action must stay possible in a narrow slot, put a link on the title, which is always there.
  • Keyboard: once displayed, the button takes focus with Tab and shows a light 2 px outline (:focus-visible).
  • Small button: its label is .7rem (11 px) and its height 20 px; that is under the 24 px recommended for a touch target. Raise padding to 8px 14px on a touch screen.
  • Decorative image: the pictogram carries aria-hidden="true". If you use a photo that informs, give it a text alternative (role="img" and aria-label on .acw-img).
  • Title: it is an h5; pick the level that suits your page.

Browser compatibility

Container queries and grid, nothing else. Same readings in Chromium 149, Firefox 151 and WebKit 26.5: button hidden at 279 px and shown at 280, one column at 399 px and two at 400; no overflow at 390 px, where the card takes the middle state. Under reduced motion the behaviour is the same since nothing is animated.

Chrome 105+✓ Full (measured in Chromium 149)
Firefox 110+✓ Full (measured in Firefox 151)
Safari 16+✓ Measured in WebKit 26.5; real Safari not verified
Edge 105+✓ Not verified (same engine as Chrome)
Mobile iOS 16+✓ Not verified on a device
Android Chrome 105+✓ Measured in 390 px emulation: middle state

Without @container the card stays in its smallest state: image and title, no line and no button, whatever the width. For those browsers, flip the logic — show everything by default and hide inside an @container (max-width: 279px) rule.

The code

HTML structure to paste into your page (the CSS + JS come with your purchase):

index.html — structure
<div class="fx-0755-root">
  <div class="acw-card">
    <div class="acw-img">
      <svg viewBox="0 0 24 24" aria-hidden="true">…</svg>
    </div>
    <div class="acw-body">
      <h5>Title</h5>
      <p>Text shown from 280 px</p>
      <button type="button" class="acw-btn">Action</button>
    </div>
  </div>
</div>

One-time payment, no subscription — the code is yours forever

Customize

Options passed to the API or data-* attributes:

Option / propertyDefaultEffect
Thresholds (CSS — @container acw-card (min-width: 280px) and (min-width: 400px)) 280 and 400 px Lower the first to 220 px to keep the button in a narrow column; raise the second to 480 px if your text is long.
Photo (HTML — style="background-image: url(…)" on .acw-img) #6366f1 → #8b5cf6 gradient and pictogram The photo is cropped and centred (cover); remove the svg then.
Band height (CSS — .acw-img { height }) 80 px 120 px for a real photo; aspect-ratio: 16 / 9 instead of the height for a constant format.
Image column (CSS — 140px 1fr in the 400 px rule) 140 px 1fr 1fr for an image taking half the card.
Button (CSS — .acw-btn) #6063f1, white label, 6 px corners Your colours; keep a 4.5 ratio between label and background.
Card background (CSS — .fx-0755-root, .acw-card) #0a0a0f and a 5 % white veil On a light page: white root, transparent .acw-card, dark title and text.

FAQ

Adaptive Card has a single threshold, at 280 px: it flips from stack to row and always shows all its content, tags included. Container Query Cards has two thresholds and works in two stages: it first adds content (line and button at 280 px) and only changes direction at 400 px. Pick this one when the card must also exist as a thumbnail.
Write style="background-image: url('photo.jpg')" on .acw-img and delete the svg. Cropping (background-size: cover) and centring are already in the CSS; at 400 px the photo fills the left column over the full height.
Not with this component: the text appearing and the columns changing are instant. If you want a softened change on the visual, look at Breakpoint Morphing Cards, whose band and pictogram change size in 0.4 s.