Frontend development · Intermediate · 17 min
Make a whole card clickable without breaking accessibility
By Padding · Updated 18 Nov 2026
Modern browsers · HTML and CSS positioning · tested with Playwright 1.65.1 (Chromium) on Node.js 26
You will build a list of service cards where the whole card opens the booking page, the link is announced by its short name, a second link inside still works and the text can still be selected, with a Playwright test for each behaviour.
On this page
A card that only reacts when you hit its small title is annoying on a phone, so designers ask for the whole card to be clickable. The two usual ways of building that both cause problems. Wrapping the card in one <a> turns everything inside into the link's name, so a screen reader announces "GP appointment 10 min See a doctor about a new or ongoing health problem, link" for every card, and you can no longer put a second link inside, because HTML does not allow a link inside a link. Putting an onclick on a <div> gives you something a mouse can use and a keyboard cannot reach, with no "open in new tab" and nothing for assistive technology to call a link.
The approach here keeps one ordinary link around the service name and stretches an invisible pseudo-element from that link over the whole card. The link's name stays short, the page keeps working without JavaScript, and the browser handles keyboard focus, middle-click and the context menu as it does for any link. Then you fix the two things the overlay breaks: other links inside the card and selecting text.
The example is the Choose a service screen of the Larkfield Clinic booking app, a fictional community clinic, with its five services. A large tap target matters most on small, low-end Android phones, and the technique adds a few lines of CSS and a dozen lines of script, so the page stays light on prepaid data.
Step 1 · Build the service list
Create a folder with the Choose a service page, two pages for the links to open, and a stylesheet. Each service is a list item holding a heading, the duration and a short summary. Only the service name is a link. The Blood test card also has a second link, to a page about preparing for the test.
mkdir -p ~/card-demo
cd ~/card-demo
cat > index.html <<'HTML'
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Choose a service · Larkfield Clinic</title>
<link rel="stylesheet" href="style.css">
<script src="cards.js" defer></script>
</head>
<body>
<main>
<h1>Choose a service</h1>
<p>What do you need an appointment for?</p>
<ul class="services">
<li class="card">
<h2><a class="card-link" href="book.html?service=gp-appointment">GP appointment</a></h2>
<p class="duration">10 min</p>
<p class="summary">See a doctor about a new or ongoing health problem.</p>
</li>
<li class="card">
<h2><a class="card-link" href="book.html?service=nurse-appointment">Nurse appointment</a></h2>
<p class="duration">15 min</p>
<p class="summary">Dressings, blood pressure checks, contraception and travel advice.</p>
</li>
<li class="card">
<h2><a class="card-link" href="book.html?service=blood-test">Blood test</a></h2>
<p class="duration">10 min</p>
<p class="summary">A sample taken by a healthcare assistant. Some tests need you to fast first.</p>
<p class="card-extra"><a href="prepare.html">Preparing for a blood test</a></p>
</li>
<li class="card">
<h2><a class="card-link" href="book.html?service=vaccination">Vaccination</a></h2>
<p class="duration">15 min</p>
<p class="summary">Flu, travel and routine vaccines for adults and children.</p>
</li>
<li class="card">
<h2><a class="card-link" href="book.html?service=medication-review">Medication review</a></h2>
<p class="duration">20 min</p>
<p class="summary">Go through your repeat prescriptions with a pharmacist.</p>
</li>
</ul>
</main>
</body>
</html>
HTML
cat > book.html <<'HTML'
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Pick a time · Larkfield Clinic</title></head>
<body><main><h1>Pick a time</h1><p><a href="index.html">Back to services</a></p></main></body>
</html>
HTML
cat > prepare.html <<'HTML'
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Preparing for a blood test · Larkfield Clinic</title></head>
<body><main><h1>Preparing for a blood test</h1><p><a href="index.html">Back to services</a></p></main></body>
</html>
HTMLThe page loads cards.js with defer, so the script runs after the HTML is parsed. You write that file in a later step. Until then the browser reports a missing file in the console, which does no harm.
The stylesheet uses the Larkfield colour variables: dark text, a muted grey for durations, the teal accent for links and a darker border (3.72:1 on white) so the card edge stays visible, in line with the 3:1 that WCAG 2.2 SC 1.4.11 (Non-text Contrast) asks of component boundaries. The card's link is already identified by its teal text, so the border is not strictly required to pass, but a visible edge shows where the large target starts and ends. The corner radius goes in a custom property, --card-radius, because the next step needs it too.
cat > style.css <<'CSS'
:root {
--colour-text: #1B1F24;
--colour-text-muted: #5A6270;
--colour-surface: #FFFFFF;
--colour-canvas: #F6F7F8;
--colour-border: #7D8592;
--colour-accent: #0F5E5A;
--card-radius: 0.75rem;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.5;
color: var(--colour-text);
background: var(--colour-canvas);
}
main { max-width: 40rem; margin: 0 auto; padding: 1rem; }
.services { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.75rem; }
.card {
background: var(--colour-surface);
border: 1px solid var(--colour-border);
border-radius: var(--card-radius);
padding: 1rem;
}
.card h2 { margin: 0; font-size: 1.125rem; }
.card-link { color: var(--colour-accent); }
.card p { margin: 0.25rem 0 0; }
.duration { color: var(--colour-text-muted); font-size: 0.875rem; }
.card-extra a { color: var(--colour-accent); }
CSSCheck it worked
Open index.html in your browser (open index.html on macOS, xdg-open index.html on Linux, or drag the file into a window). You see the heading Choose a service and five bordered cards in one column. Hover over the cards: the pointer only turns into a hand over the five service names and over Preparing for a blood test. Clicking the white space of a card does nothing yet.
Step 2 · Stretch the link over the card
Give the card position: relative, then give the service link an empty ::after pseudo-element that is absolutely positioned with inset: 0. An absolutely positioned box is placed against its nearest positioned ancestor. The link and the heading are not positioned, so that ancestor is the card, and the pseudo-element covers the card edge to edge. It takes the same corner radius from --card-radius. border-radius: inherit would not work here: a pseudo-element inherits from the link it belongs to, which has square corners, so the overlay and the focus ring in step 5 would poke out past the rounded card.
cat >> style.css <<'CSS'
/* Step 2: stretch the service link over the whole card */
.card { position: relative; }
.card-link::after {
content: "";
position: absolute;
inset: 0;
border-radius: var(--card-radius);
}
CSSA pseudo-element belongs to its element, so a click on it is a click on the link. Nothing changes in the accessibility tree either: the link's name is still only its text, "GP appointment".
Keep one rule in mind as the card grows. Any element between the card and the link that becomes positioned, or gets a transform, becomes the new containing block and the overlay shrinks to that element. If a card stops being clickable after a styling change, look for a position or transform on the heading.
Check it worked
Reload the page and hover anywhere on a card: the pointer is a hand everywhere inside the border. Click the empty space to the right of Nurse appointment. The Pick a time page opens and the address bar ends in book.html?service=nurse-appointment. Go back and click Preparing for a blood test on the Blood test card: it also opens Pick a time, not the preparation page. The overlay is covering the second link. The next step fixes that.
Step 3 · Lift the second link above the overlay
The overlay is a positioned box, and browsers paint positioned boxes on top of ordinary ones that come before or after them in the page. The Preparing for a blood test link is not positioned, so the overlay sits over it and takes its clicks. Give that link's paragraph a position and a z-index above the overlay's:
cat >> style.css <<'CSS'
/* Step 3: keep other links in the card clickable */
.card-extra { position: relative; z-index: 1; }
CSSAny interactive element you add to a card later, such as a second link or a "Save" button, needs the same treatment. Keep these to one or two per card. Each one is a hole in the large target, and a card crowded with controls is better built as a plain block with separate links.
Check it worked
Reload and click Preparing for a blood test. The Preparing for a blood test page opens. Go back and click the white space beside it in the same card: Pick a time opens with service=blood-test, so the rest of the card still works.
Step 4 · Let people select the card text
Try to select the words "blood pressure checks" on the Nurse appointment card by dragging across them. You cannot. The drag lands on the overlay, which is a link, so the browser starts dragging the link instead of selecting text. People copy text from cards more often than you might expect, to paste a service name into a message or to translate a sentence.
Lift the duration and summary above the overlay as well, so a drag on them selects text:
cat >> style.css <<'CSS'
/* Step 4: keep card text selectable */
.duration, .summary { position: relative; z-index: 1; }
CSSThose two paragraphs now take their own clicks, so clicking the summary no longer opens the booking page. Add a small script that passes those clicks on to the card's link, unless the person has just selected some text:
cat > cards.js <<'JS'
// Forward clicks on card text to the card's main link.
// Clicks on the overlay or on other links are left to the browser.
for (const card of document.querySelectorAll('.card')) {
const link = card.querySelector('.card-link');
if (!link) continue;
card.addEventListener('click', (event) => {
if (event.target.closest('a, button')) return;
if (event.ctrlKey || event.metaKey || event.shiftKey) return;
if (!window.getSelection().isCollapsed) return;
link.click();
});
}
JSA drag that selects text still fires a click when the mouse is released. The isCollapsed check tells the two apart: after a drag the selection has some length and the script does nothing, after a plain click the selection is empty and the script follows the link. Clicks on any link, the overlay included, return early so the browser handles them in the usual way. Clicks on the text with Ctrl, Cmd or Shift held are not forwarded either, because link.click() would open the booking page in the same tab. To open a service in a new tab, use the name or the white space of the card, which are the real link.
Without JavaScript the page still works: the name, the white space and the second link all behave as before, and only clicks on the duration and summary do nothing. Over the text the pointer becomes a text cursor, which is a fair hint that the words can be selected.
Check it worked
Reload. Drag across "blood pressure checks" on the Nurse appointment card: the words are highlighted and you stay on Choose a service. Click somewhere else to clear the selection, then click once on the Vaccination summary. Pick a time opens with service=vaccination in the address bar.
Step 5 · Show focus and hover on the whole card
Keyboard users press Tab to reach each service link. The browser's focus ring is drawn around the small link text, while the target is the whole card. Draw the ring on the overlay instead, so it outlines the card, and give the card a hover state:
cat >> style.css <<'CSS'
/* Step 5: focus and hover on the whole card */
.card-link:focus-visible { outline: none; }
.card-link:focus-visible::after {
outline: 3px solid var(--colour-accent);
outline-offset: 2px;
}
.card:hover { border-color: var(--colour-accent); }
.card:hover .card-link { text-decoration-thickness: 2px; }
CSS:focus-visible matches when the browser decides a focus ring is useful, which includes focus from the keyboard but usually not a mouse click. The outline moves from the link to its own pseudo-element, so there is still exactly one ring, and it now surrounds the card. The ring uses the accent colour, which has well over 3:1 contrast against both the white card and the grey page.
Check it worked
Reload and press Tab. A thick teal outline appears around the whole GP appointment card, not just its name. Keep pressing Tab: focus moves to Nurse appointment, Blood test, Preparing for a blood test (with the browser's usual small ring, because it is a separate link), Vaccination and Medication review. Press Enter on any service and Pick a time opens for that service. Hover a card with the mouse and its border turns teal.
Step 6 · Write a test for each behaviour
Clicking around by hand once is fine. A test keeps the behaviour from breaking the next time someone adds a badge or a transform to the card. Install Playwright in the demo folder:
cd ~/card-demo
npm init -y > /dev/null
npm install playwright@1.65.1Then download the Chromium build that this Playwright version drives. On some Linux machines you also need npx playwright install-deps chromium first, which installs the system libraries with sudo.
cd ~/card-demo
npx playwright install chromiumThe test uses Node's built-in test runner and Playwright's library. Each test opens the page fresh at a phone-sized 400 pixel width. Two details matter. Clicks at a point use page.mouse.click, which clicks whatever is on top at that point, the same as a person would, so the test catches an overlay covering the wrong thing. And the names are found with getByRole, which uses the same accessible name a screen reader announces.
cd ~/card-demo
cat > cards.test.mjs <<'JS'
import { test, before, after, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { chromium } from 'playwright';
const home = pathToFileURL(join(import.meta.dirname, 'index.html')).href;
const services = ['GP appointment', 'Nurse appointment', 'Blood test', 'Vaccination', 'Medication review'];
let browser, page;
before(async () => { browser = await chromium.launch(); });
after(async () => { await browser.close(); });
beforeEach(async () => {
page = await browser.newPage({ viewport: { width: 400, height: 900 } });
await page.goto(home);
});
afterEach(async () => { await page.close(); });
const card = (name) => page.locator('.card').filter({ hasText: name });
test('each card link is named by the service name only', async () => {
assert.equal(await page.locator('.card-link').count(), services.length);
for (const name of services) {
assert.equal(await page.getByRole('link', { name, exact: true }).count(), 1, name);
}
assert.equal(await page.getByRole('link', { name: /min|See a doctor/ }).count(), 0);
});
test('clicking empty space in a card opens booking', async () => {
const box = await card('Nurse appointment').boundingBox();
await page.mouse.click(box.x + box.width - 10, box.y + 10);
await page.waitForURL(/book\.html\?service=nurse-appointment$/);
});
test('clicking the summary text opens booking', async () => {
const box = await card('Vaccination').locator('.summary').boundingBox();
await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2);
await page.waitForURL(/book\.html\?service=vaccination$/);
});
test('the second link opens its own page', async () => {
await page.getByRole('link', { name: 'Preparing for a blood test' }).click();
await page.waitForURL(/prepare\.html$/);
});
test('summary text can be selected without leaving the page', async () => {
const box = await card('Nurse appointment').locator('.summary').boundingBox();
const y = box.y + 8;
await page.mouse.move(box.x + 2, y);
await page.mouse.down();
await page.mouse.move(box.x + box.width - 2, y, { steps: 10 });
await page.mouse.up();
await page.waitForTimeout(300);
const selected = await page.evaluate(() => window.getSelection().toString());
assert.ok(selected.length > 10, `selected "${selected}"`);
assert.equal(page.url(), home);
});
test('keyboard focus outlines the card and Enter opens booking', async () => {
const order = [...services.slice(0, 3), 'Preparing for a blood test', ...services.slice(3)];
for (const name of order) {
await page.keyboard.press('Tab');
assert.equal(await page.evaluate(() => document.activeElement.textContent), name);
}
await page.keyboard.press('Shift+Tab');
const ring = await page.evaluate(() => getComputedStyle(document.activeElement, '::after').outlineStyle);
assert.equal(ring, 'solid');
await page.keyboard.press('Enter');
await page.waitForURL(/book\.html\?service=vaccination$/);
});
JS
node --test cards.test.mjsThe tests map to what you built: the short names from step 1, the overlay from step 2, the lifted link from step 3, the selectable text and forwarded click from step 4, and the focus ring from step 5. The keyboard test tabs through all six links in order, steps back to Vaccination, checks its outline and presses Enter.
Check it worked
The run lists six tests, each with a tick, and ends with a summary showing pass 6 and fail 0. To see a test catch a real mistake, delete the line .card-extra { position: relative; z-index: 1; } from style.css (leave the .card-extra a colour rule alone) and run node --test cards.test.mjs again. The run seems to pause: Playwright keeps retrying the click until its default 30-second timeout, then the second-link test fails with an error saying another element intercepts pointer events. That element is the overlay, which now takes the click. Put the line back afterwards.
Step 7 · Clean up
Remove the demo folder, including the Playwright package in it. The Chromium download lives in Playwright's shared cache, so delete that as well if no other project uses it: ~/.cache/ms-playwright on Linux, ~/Library/Caches/ms-playwright on macOS.
cd ~
rm -rf ~/card-demoCheck it worked
ls ~/card-demo reports that there is no such file or directory.
Next steps
- Before reaching for cards at all, check that they suit the content: choose between cards, lists and tables.
- Give the second link and other controls a focus style that fits the card ring: style keyboard focus with :focus-visible.
- Build the next part of the booking flow in code: build the Larkfield button in HTML and CSS.
- The Your details screen needs labelled fields and clear errors: build accessible forms in HTML.
Part of these paths
Related tutorials
- Choose between cards, lists and tablesYou will have the same five clinic services laid out as cards, a list and a table, scored against four questions, with one layout chosen for patients, one for staff and a short written rule for when each container wins.
- Build the Larkfield button in HTML and CSSYou will build the Larkfield Clinic button as a real button element with primary, secondary and destructive variants drawn from colour tokens, hover, pressed, keyboard focus, disabled and loading states, a 48 pixel minimum height, and a Playwright script that checks every state's computed styles.
- Style keyboard focus with :focus-visibleYou will remove an outline reset that hides keyboard focus, add a two-colour focus ring with :focus-visible that stays visible on white, tinted and accent backgrounds, keep it clear of a sticky header with scroll-margin, check it in forced colours mode and prove it all with a Playwright keyboard test.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials