Emmanuel Akinfulubi

HTML and CSSPractical III

Add a skip to content link in HTML and CSS

40 A skip link that appears when you need it

fair

Add a link that lets a keyboard user jump past the navigation straight to the content. It should be out of the way until it has focus, and visible the instant it does.

Stuck? Three hints, smallest first
  1. 1. It must be the first focusable thing in the document, or it is skipped before it can help.
  2. 2. Hide it with the visually-hidden recipe, then undo that on :focus.
  3. 3. The target needs tabindex="-1", or focus moves to the fragment without landing on it in some browsers.

index.html

<a class="skip" href="#main">Skip to content</a>

<nav class="bar" aria-label="Main">
  <a href="#routes">Routes</a>
  <a href="#fares">Fares</a>
  <a href="#help">Help</a>
</nav>

<main class="content" id="main" tabindex="-1">
  <h2>Departures</h2>
  <p>Tab into the preview: the skip link appears first, above the bar.</p>
</main>

style.css

/* Hidden until focused. Note :focus, not :focus-visible: reaching this link is
   always a keyboard act, so it should show every time. */
.skip {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip:focus {
  position: static;
  width: auto;
  height: auto;
  clip-path: none;
  display: inline-block;
  margin-bottom: 8px;
  padding: 8px 12px;
  border-radius: 6px;
  background: #17171a;
  color: #ffffff;
  text-decoration: none;
  outline: 2px solid #3055d8;
  outline-offset: 2px;
  font-size: 0.85rem;
}

.bar {
  display: flex;
  gap: 14px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #f2f3f5;
  font-size: 0.85rem;
}

.bar a { color: #17171a; }

.content { padding: 10px 2px; }

.content:focus-visible {
  outline: 2px solid #3055d8;
  outline-offset: 2px;
}

.content h2 { margin: 0 0 6px; font-size: 1rem; }
.content p { margin: 0; font-size: 0.85rem; color: #5c5c66; }
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 40

test('the skip link is the first element in the document', html.trim().startsWith('<a class="skip"'), true);
test('it points at the main region', /href="#main"/.test(html), true);
test('and that id exists on main', /<main[^>]*id="main"/.test(html), true);
test('the target is focusable, so focus really lands there', /tabindex="-1"/.test(html), true);
test('the link is clipped away until focused', /\.skip\s*\{[^}]*clip-path:\s*inset\(50%\)/.test(css), true);
test('and revealed on :focus, not :focus-visible', /\.skip:focus\s*\{/.test(css) && /\.skip:focus-visible/.test(css) === false, true);
test('the revealed state undoes the clipping', /\.skip:focus\s*\{[\s\S]*?clip-path:\s*none/.test(css), true);
test('the nav landmark is named, since there could be several', /aria-label="Main"/.test(html), true);

Output


      

Order in the source is the whole mechanism: the link has to come before the navigation or the reader has already tabbed through everything it was meant to skip. :focus rather than :focus-visible is deliberate here — there is no way to reach this link except by keyboard, so there is no mouse case to suppress. And tabindex="-1" on the target is the part that gets left out: without it, several browsers scroll to the fragment but leave focus where it was, so the next Tab carries on from the navigation and the link achieved nothing.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.