CSS Expanding Cards
A row of image panels where the one you hover or focus grows to fill the space.
<div class="expand-cards">
<a href="#"><span>Mountains</span></a>
<a href="#"><span>Forest</span></a>
<a href="#"><span>Desert</span></a>
<a href="#"><span>Ocean</span></a>
</div>
.expand-cards {
display: flex;
gap: 8px;
width: 100%;
height: 300px;
}
/* Set each panel's background-image inline */
.expand-cards > * {
flex: 1;
display: flex;
align-items: flex-end;
min-width: 0;
padding: 12px;
overflow: hidden;
border-radius: 14px;
background: #d4d4d8 center / cover;
color: #fff;
font: 700 14px system-ui, sans-serif;
text-decoration: none;
white-space: nowrap;
transition: flex-grow 0.45s ease;
}
.expand-cards span {
opacity: 0;
text-shadow: 0 1px 6px rgba(0, 0, 0, 0.4);
transition: opacity 0.3s ease;
}
/* The hovered or focused panel grows. The first one is open by default. */
.expand-cards > :hover,
.expand-cards > :focus-visible,
.expand-cards:not(:hover, :focus-within) > :first-child {
flex-grow: 4;
}
.expand-cards > :hover span,
.expand-cards > :focus-visible span,
.expand-cards:not(:hover, :focus-within) > :first-child span {
opacity: 1;
}
.expand-cards > :focus-visible {
outline: 2px solid #4338ca;
outline-offset: 2px;
}
About this CSS Expanding Cards
This CSS Expanding Cards is 47 lines of pure CSS (1.0 KB) with no JavaScript and no dependencies. It uses transitions, text shadows, Flexbox and keyboard focus styles. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
Customize it
The colors in this snippet. Search and replace them to match your brand:
#d4d4d8#fff#4338ca
How to use this expanding cards
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.expand-cardsclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.