Emmanuel Akinfulubi

HTML and CSSPractical III

Reusable sr-only (visually hidden) class in CSS

39 A visually hidden utility, and what to use it for

fair

Write the reusable class that hides an element from the screen but not from a screen reader, and use it twice: to name an icon-only button and to head a region.

Stuck? Three hints, smallest first
  1. 1. display: none and visibility: hidden remove the element from the accessibility tree as well.
  2. 2. Clip it to one pixel and take it out of the flow. Not zero pixels — some readers skip zero-sized elements.
  3. 3. An icon-only button needs a name from somewhere: hidden text, or aria-label. Hidden text also survives translation.

index.html

<h2 class="visually-hidden">Quick actions</h2>

<button class="icon" type="button">
  <span aria-hidden="true">&#9587;</span>
  <span class="visually-hidden">Close the fare panel</span>
</button>

<p class="note">The heading above and the button label are announced but not
  shown. The cross glyph is hidden the other way round.</p>

style.css

/* The standard recipe. One pixel, clipped, out of the flow. */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.icon {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid #8e8e98;
  border-radius: 8px;
  background: #ffffff;
  color: #17171a;
  font-size: 1rem;
  cursor: pointer;
}

.icon:hover { background: #f2f3f5; }

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

.note { margin: 10px 0 0; font-size: 0.8rem; color: #5c5c66; max-width: 42ch; }
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 39

test('the element is taken out of the flow', /position:\s*absolute/.test(css), true);
test('one pixel each way, not zero', /width:\s*1px/.test(css) && /height:\s*1px/.test(css), true);
test('and clipped away', /clip-path:\s*inset\(50%\)/.test(css), true);
test('display: none is not used, since it hides from readers too', /display:\s*none/.test(css), false);
test('nor visibility: hidden', /visibility:\s*hidden/.test(css), false);
test('the utility is reused twice', (html.match(/class="visually-hidden"/g) || []).length, 2);
test('the icon button has a text label, not just a glyph', /<span class="visually-hidden">Close the fare panel<\/span>/.test(html), true);
test('and the glyph itself is hidden from assistive technology', /aria-hidden="true"/.test(html), true);

Output


      

The two negatives are the exercise: display: none and visibility: hidden hide from eyes and from screen readers, so using either for a button label or a region heading means the people who needed it most get nothing. The pairing in the button is the pattern worth memorising — visible glyph marked aria-hidden, hidden text carrying the name — so exactly one of the two is announced. Hidden text is preferable to aria-label here because it is real text on the page: it gets translated by the browser’s translation feature, and aria-label often does not.

Newsletter

New writing, when it lands.

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