← Back to HTML & CSS, page 5

html-css-0099 · Hard senior

How do you make a whole card clickable without wrapping everything in a link and harming accessibility?

Use one real link, usually on the title, and expand its ::after to cover the card with position absolute and inset 0 inside a position: relative card. Raise other links with z-index, and draw the focus outline around the card. The link name stays short.

Full answer

A common request is that the whole card should be clickable. There are two simple but poor ways:

  • Wrap the entire card in one a. The link name becomes everything inside the card, so a screen reader reads a very long link. Nested buttons or links inside are invalid HTML.
  • Put a click handler on the whole card with JavaScript. Keyboard and screen reader users get nothing, and "open in new tab" does not work.

A better pattern is the stretched link. The card has one real link, usually on the title. A pseudo-element on that link expands to cover the whole card, so a click anywhere on the card follows the link.

Steps:

  1. The card gets position: relative.
  2. The link's ::after is content: ""; position: absolute; inset: 0;.
  3. Other interactive elements in the card, such as a secondary link or button, get position: relative; z-index: 1 so they sit above the stretched area and still work.
  4. Add a focus style on the card with :focus-within, or on the link. The focus ring of the link only wraps the title, so show it on the whole card with .card:has(a:focus-visible) or by styling the pseudo-element's outline.
  5. Text selection inside the card is blocked in the stretched area, which is a known trade-off.

The link name is only the title, such as "HTML for beginners", which is short and clear.

Clicking the empty area of the card follows the main link. Clicking the small tag link still goes to its own address, because it is raised above the stretched area. When the title link has keyboard focus, the outline is drawn around the whole card.

Test with a screen reader, because the link list should show short, clear names like the title.

html
<style>
  .card { position: relative; width: 220px; padding: 12px; border: 1px solid #999; font-family: sans-serif; }
  .card h3 { margin: 0 0 8px; }
  .card h3 a { color: inherit; text-decoration: none; }
  .card h3 a::after { content: ""; position: absolute; inset: 0; }
  .card a.tag { position: relative; z-index: 1; }
  .card:hover { background: #f5f8ff; }
  .card h3 a:focus-visible::after { outline: 3px solid #0a6cff; outline-offset: 2px; }
</style>
<div class="card" id="card">
  <h3><a href="#course" id="main">HTML for beginners</a></h3>
  <p>Learn tags, links and forms.</p>
  <a class="tag" href="#tag-html" id="tag">HTML</a>
</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Wrapping the whole card in a link gives a long, noisy link name.
  • A stretched link uses a ::after pseudo-element with inset: 0.
  • The card needs position: relative.
  • Inner links need position: relative and a z-index.
  • Show focus on the whole card.

Common mistakes

  • Wrapping the whole card in one link, so the link name is a long paragraph.
  • Forgetting z-index on inner links and buttons, so the stretched area blocks them.
  • Showing the focus ring only around the small title link.

Follow-ups

  • How do you keep a secondary link inside the card clickable?
  • What is the downside of a stretched link for text selection?

Tips

  • State the problem with a wrapped card: long link name and invalid nesting.
  • Describe the stretched link in two steps: relative card and absolute pseudo-element.