overflow: scroll vs auto vs hidden in CSS
27 scroll, auto, hidden, and the clipped focus ring
hardShow the three overflow behaviours side by side. Then deal with the accessibility trap: a focus ring drawn outside an element is cut off by an ancestor that clips.
Stuck? Three hints, smallest first
- 1.
scrollalways shows the scrollbar or reserves its gutter;autoonly when there is something to scroll. - 2.
hiddenclips with no way to reach what was cut off, so it must never hide focusable content. - 3. Where clipping is unavoidable, draw the ring inside with a negative
outline-offset.
<p class="cap">overflow-y: scroll — gutter always reserved</p>
<div class="pane always">
<p>06:30 Abuja</p><p>08:00 Kano</p><p>11:15 Enugu</p><p>14:45 Ibadan</p>
</div>
<p class="cap">overflow-y: auto — scrolls, and reachable by keyboard</p>
<div class="pane demand" tabindex="0" role="region" aria-label="Departures">
<p><a href="#a">06:30 Abuja</a></p>
<p><a href="#k">08:00 Kano</a></p>
<p><a href="#e">11:15 Enugu</a></p>
<p><a href="#i">14:45 Ibadan</a></p>
</div>
<p class="cap">overflow: hidden — clipped, so nothing focusable goes in here</p>
<div class="pane clip">
<p>Decorative text only. The rest is cut off and cannot be reached.</p>
<p>Second line, clipped away.</p>
</div>
.cap { margin: 10px 0 4px; font-size: 0.75rem; color: #5c5c66; }
.pane {
height: 60px;
padding: 0 10px;
border: 1px solid #c9cad1;
border-radius: 6px;
font-size: 0.8rem;
}
.pane p { margin: 6px 0; }
.always { overflow-y: scroll; }
.demand { overflow-y: auto; }
/* Clips with no scrollbar, so anything past the edge is unreachable. */
.clip { overflow: hidden; }
/* The scrollable pane is focusable, so it needs its own ring. */
.demand:focus-visible {
outline: 2px solid #3055d8;
outline-offset: 2px;
}
/* Drawn INSIDE the link, so a clipping ancestor cannot cut it off. */
.demand a:focus-visible {
outline: 2px solid #3055d8;
outline-offset: -2px;
}
.demand a { color: #17171a; }
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.
// The clipping pane is last, so everything from its class attribute onwards is
// the part that gets cut off.
const clipped = html.slice(html.indexOf('pane clip'));
test('all three behaviours are shown', ['overflow-y:\\s*scroll', 'overflow-y:\\s*auto', 'overflow:\\s*hidden'].every((p) => new RegExp(p).test(css)), true);
test('the clipping pane holds nothing focusable', /<a\b|<button\b|tabindex/.test(clipped), false);
test('the scrolling pane is reachable by keyboard', /class="pane demand" tabindex="0"/.test(html), true);
test('and named, since it is now a focusable region', /aria-label="Departures"/.test(html), true);
test('it has its own focus ring', /\.demand:focus-visible/.test(css), true);
test('the links inside draw their ring inwards', /outline-offset:\s*-2px/.test(css), true);
test('no outline is simply removed anywhere', /outline:\s*none/.test(css), false);
test('every pane has a height, or nothing would overflow', /\.pane\s*\{[^}]*height:\s*60px/.test(css), true);
Output
Three separate traps. overflow: hidden makes content unreachable, not merely invisible — there is no scrollbar and no keyboard route — so putting a link inside one is a way of hiding a control from some users and not others. A container that scrolls needs tabindex="0" to be scrollable by keyboard at all, and once it is focusable it needs a name and a focus ring of its own. And a focus ring is painted outside the element by default, so an ancestor that clips will shave it off; a negative outline-offset draws it inside instead. None of that is measurable here, so the preview is where you check it — but the source conditions are, and those are what is asserted.