Accessible card component
With pure (S)CSS (no JavaScript, the pseudo-content trick)

Web developer, UX consultant. Making stuff for the web since 2005 🦄
Search for a command to run...
With pure (S)CSS (no JavaScript, the pseudo-content trick)

Web developer, UX consultant. Making stuff for the web since 2005 🦄
No comments yet. Be the first to comment.
When creating the card component, sometimes it’s advisable (or required by design) to make the whole card clickable. But how to do so without compromising the usability? Below I share a useful pseudo-content trick to make the whole card clickable and maintain its accessibility.
How would you approach this task? Just a regular card component wrapped in an a element? Or maybe onclick in JavaScript directly on a div or article element (don’t do that!)?
How to handle such a case and maintain the accessibility in a simple and elegant way?
position: relative on the container elementposition: absolute on the link’s :after pseudo-content0 for top, right, bottom, and left properties on link’s :after pseudo-content:focus-within and :hover to style different statesz-indexcard__separate to make it selectable (and/or clickable).<div class="card">
<p>
<a href="#optional" class="card__separate">Optional</span>
</p>
<p>
<span class="card__separate">Lorem ipsum</span>
</p>
<a href="#card-link" class="card__link">Link</a>
</div>
.card {
position: relative;
border: 3px solid green;
// Style hover and focus states.
&:hover,
&:focus-within {
border-color: red;
}
// Make the content selectable.
&__separate {
position: relative;
z-index: 2;
}
// Make the whole card clickable.
&__link::after {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
content: "";
}
}
Check clickable area (start with "Button 1") and try tab keyboard navigation in the following example:
I first saw the pseudo-content trick technique on Inclusive Components website. Check it out for other solutions to card issues and more inclusive components examples.