IntermediateLesson 19 of 307 min read

CSS card layouts and reusable components

Build the most common component on the web: a responsive, accessible card, then place it in a grid and vary it.

In this lesson you will

  • Build a semantic card
  • Keep cards equal height and tidy
  • Make the whole card clickable
  • Place cards in a responsive grid

About 17 minutes

Cards are everywhere

Open a news site, an online shop, a course catalogue or a social feed. Almost everything is a card: a small box with a picture, a title, a few lines of text and a button. Cards are popular because they package a unit of content in a way that is easy to scan, rearrange and reuse. If you can build one good card and put it in a responsive grid, you can build most of the homepage of any site.

The anatomy of a card

<article class="card">
  <img src="images/css.jpg" alt="" width="640" height="360">
  <div class="card-body">
    <span class="tag">Beginner</span>
    <h3><a href="/skills/css">CSS in Minutes</a></h3>
    <p>Learn selectors, layout and responsive design step by step.</p>
    <p class="meta">30 lessons &middot; Free</p>
  </div>
</article>

Notice the choices. The card is an article, since it makes sense on its own. The title is a link inside a heading. The image has an empty alt because it is decorative here, and the link text already describes the card. Good structure comes first, the styling second.

The base styles

.card {
  display: flex;
  flex-direction: column;
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 6px 20px rgba(20, 17, 46, .08);
  transition: transform .2s, box-shadow .2s;
}
.card img {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}
.card-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 18px 20px 22px;
  flex: 1;
}
.card h3 { margin: 0; font-size: 1.15rem; line-height: 1.3; }
.card h3 a { color: inherit; text-decoration: none; }
.card .meta { margin-top: auto; color: #6B6890; font-size: .9rem; }
  • overflow: hidden on the card clips the image to the rounded corners.
  • aspect-ratio: 16 / 9 with object-fit: cover gives every card picture the same shape, whatever the original.
  • flex: 1 on the body and margin-top: auto on the meta pin the last line to the bottom, so cards of different lengths still line up.
  • inherit on the link colour keeps the heading colour instead of the default blue.

Style the card: white background, 16px radius, overflow hidden so the image follows the corners, a soft shadow, and a 16:9 cover image.

<style>
 body { background: #F4F1FC; padding: 20px; font-family: Arial; }
 .card { max-width: 300px; /* style me */ }
 .card img { /* 16:9 */ }
 .body { padding: 16px; }
</style>
<article class="card"><img src="https://picsum.photos/id/1015/600/400" alt="" width="600" height="400"><div class="body"><h3>CSS in Minutes</h3><p>Learn layout and design.</p></div></article>

.card { background:#fff; border-radius:16px; overflow:hidden; box-shadow:0 6px 20px rgba(20,17,46,.1); } .card img { width:100%; aspect-ratio:16/9; object-fit:cover; display:block; }

<style>
 body { background: #F4F1FC; padding: 20px; font-family: Arial; }
 .card { max-width: 300px; background:#fff; border-radius:16px; overflow:hidden; box-shadow:0 6px 20px rgba(20,17,46,.1); }
 .card img { width:100%; aspect-ratio:16/9; object-fit:cover; display:block; }
 .body { padding: 16px; }
</style>
<article class="card"><img src="https://picsum.photos/id/1015/600/400" alt="" width="600" height="400"><div class="body"><h3>CSS in Minutes</h3><p>Learn layout and design.</p></div></article>
Good to know

If the sample photo does not load in the practice area, a grey box appears, but the layout behaves the same. Real images on your own site will load normally.

Hover and focus

.card:hover { transform: translateY(-4px); box-shadow: 0 14px 30px rgba(20, 17, 46, .14); }
.card:focus-within { outline: 3px solid #46D3DC; outline-offset: 3px; }

A subtle lift on hover tells the visitor the card is interactive. :focus-within highlights the whole card when a link inside it is focused with the keyboard. Wrap these hover rules in @media (hover: hover) so touch screens do not keep a stuck hover state after a tap.

Making the whole card clickable

Users expect to click anywhere on a card. The accessible trick is to keep a single real link in the title and stretch its clickable area over the whole card with a pseudo-element.

.card { position: relative; }
.card h3 a::after {
  content: "";
  position: absolute;
  inset: 0;
}

The ::after box is positioned against the card and covers it completely, so a click anywhere hits the link. Screen readers still find one clear link with proper text, instead of a link wrapped around everything.

Putting cards in a grid

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 24px;
}

Because the grid makes every card in a row the same height, and each card is a column flex container that pins its meta line to the bottom, the whole set looks tidy even if the descriptions have different lengths.

Variations on the card

  • Horizontal card: image on the left and text on the right using display: grid; grid-template-columns: 140px 1fr. Switch to a single column on phones.
  • Overlay card: text placed over the image with a dark gradient behind it, using absolute positioning inside the card.
  • Featured card: the first item spans two columns with grid-column: span 2 and has a larger title.
  • Icon card: a round coloured icon, a short title and a sentence, with no photo at all, for feature lists.

Put three cards in a responsive grid with repeat(auto-fit, minmax(150px, 1fr)) and an 12px gap.

<style>
 .cards { font-family: Arial; /* grid here */ }
 .c { background:#fff; border-radius:12px; padding:16px; box-shadow:0 4px 14px rgba(0,0,0,.1); }
 body { background:#F4F1FC; padding:12px; }
</style>
<div class="cards"><div class="c"><h4>HTML</h4><p>Structure</p></div><div class="c"><h4>CSS</h4><p>Style</p></div><div class="c"><h4>JS</h4><p>Behaviour</p></div></div>

.cards { display:grid; grid-template-columns:repeat(auto-fit, minmax(150px,1fr)); gap:12px; }

<style>
 .cards { display:grid; grid-template-columns:repeat(auto-fit, minmax(150px,1fr)); gap:12px; font-family: Arial; }
 .c { background:#fff; border-radius:12px; padding:16px; box-shadow:0 4px 14px rgba(0,0,0,.1); }
 body { background:#F4F1FC; padding:12px; }
</style>
<div class="cards"><div class="c"><h4>HTML</h4><p>Structure</p></div><div class="c"><h4>CSS</h4><p>Style</p></div><div class="c"><h4>JS</h4><p>Behaviour</p></div></div>

Writing reusable components

The best habit this lesson can teach is thinking in components. A card, a button, a badge, a nav bar: each is a small, self-contained piece with its own class names that you can drop anywhere. Name the parts with a clear pattern, such as .card, .card-body and .card-title. Avoid styling the card based on where it sits. Then the same card works in a grid, a sidebar or an article. This is the thinking behind every design system.

Common mistake

Giving images in cards a fixed pixel height. Different photos then stretch or leave gaps. Use aspect-ratio and object-fit: cover so every picture keeps its shape.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Why use overflow: hidden on a card with rounded corners?

  1. To hide the text
  2. So the image is clipped to the rounded corners
  3. To remove the shadow
  4. To make it clickable

Without it, the square corners of the image poke out.

Which pair makes every card picture the same shape?

  1. aspect-ratio and object-fit: cover
  2. float and clear
  3. top and left
  4. opacity and filter

aspect-ratio sets the shape and object-fit crops.

How do you make a whole card clickable accessibly?

  1. Wrap the whole card in a link
  2. Stretch one real link with a ::after pseudo-element
  3. Use JavaScript only
  4. Use an image map

A pseudo-element over the card extends one link’s target.

Which grid declaration makes responsive cards?

  1. repeat(auto-fit, minmax(260px, 1fr))
  2. repeat(3, 300px)
  3. 1fr 1fr 1fr 1fr 1fr
  4. auto

It adjusts the number of columns to the width.

Why wrap hover effects in @media (hover: hover)?

  1. To speed up the page
  2. So touch screens do not get stuck hover states
  3. It is required
  4. It adds shadows

Touch devices have no true hover.

Summary

  • A card is a small, self-contained component.
  • aspect-ratio and object-fit keep pictures consistent.
  • A stretched pseudo-element makes the whole card clickable.
  • Auto-fit grids make card layouts responsive.

Next lesson

Last in this section: the intermediate project, a complete responsive landing page.

Questions

Should the whole card be a link?

Better to put one real link in the title and stretch it. That keeps one clear link for screen readers.

How do I make cards equal height?

Grid and flex rows stretch items by default. Use a column flex container inside to pin buttons or meta lines.

Can cards have different image sizes?

Yes, use aspect-ratio and object-fit so the shapes stay consistent.

Progress is saved in this browser.