Hiding and Showing: display: none, visibility, display: contents and content-visibility
The display property's outer and inner types and two-keyword syntax, list-item and display: contents, and every way to hide content compared: display: none, visibility, opacity, hidden, inert, visually hidden text and content-visibility.
kvi5hnuCode Sprout team member10 min readOn this page
display is the property that decides what kind of box an element generates, or whether it generates one at all. It's behind every layout mode in CSS, and display: none is the most common way to hide something. But there are several ways to hide content, and they differ in layout, interactivity and what screen readers announce. This article covers display itself, then compares all of them.
Outer and inner display
Every display value combines two things:
- the outer display type: how the box takes part in its parent's layout, as a block (starting a new line, as in normal flow, article 44) or inline (flowing within a line);
- the inner display type: how it lays out its own children: flow (normal flow), flow-root (normal flow in a new block formatting context), flex, grid, table or ruby.
The familiar one-word values are shorthands for pairs, and the two-keyword forms work in all browsers since 2023:
| One word | Two keywords | Outer | Children laid out as |
|---|---|---|---|
block | block flow | block | normal flow |
inline | inline flow | inline | normal flow (inline content) |
inline-block | inline flow-root | inline | normal flow, new formatting context |
flow-root | block flow-root | block | normal flow, new formatting context |
flex | block flex | block | flex items |
inline-flex | inline flex | inline | flex items |
grid | block grid | block | grid items |
inline-grid | inline grid | inline | grid items |
list-item | block flow list-item | block, with a marker | normal flow |
So an inline-flex button is laid out inline with the surrounding text, while arranging its own icon and label with flexbox. Browsers compute the two-keyword forms back to the one-word names: getComputedStyle() reports inline flow-root as inline-block. The one-word values are fine to keep using; the two-keyword syntax mostly helps explain them.
Other values include the table parts (table, table-row, table-cell and so on, article 74), ruby, and list-item, which generates a ::marker box like an <li> (article 94): display: list-item on a <div> gives it a bullet.
Elements versus boxes
display changes the box, not the element. A <span> with display: block is still a span in the HTML, so it still can't legally contain a <div>, and a <li> with display: flex is still a list item to assistive technology. Choose elements for their meaning, and display for their layout.
Some elements don't respond to every value: <img> and other replaced elements are always laid out as atomic boxes, and form controls such as <select> or <input type="checkbox"> ignore many inner display types.
display: contents
display: contents removes the element's own box, while keeping its children. They're laid out as if they were children of the element's parent:
<nav class="tags"> <!-- display: flex -->
<a href="#">Fiction</a>
<div class="group"> <!-- display: contents: its children become flex items -->
<a href="#">Poetry</a>
<a href="#">History</a>
</div>
</nav>

The computed values on the right show the two-keyword forms being reported back as the one-word names.
That's useful when a wrapper is needed in the HTML, for example from a framework component, but gets in the way of a flex or grid layout.
- The element's own background, border, padding and margin disappear, since there's no box to draw them on. Inherited properties such as
colorstill pass to its children. - For a long time, browsers also dropped the semantics of elements with
display: contents: buttons stopped being buttons, and table and list structure was lost for screen readers. Current Chrome, Firefox and Safari have fixed most of these cases, but avoiddisplay: contentson interactive elements, headings and tables, and test with a screen reader when you use it.
Ways to hide content
body { font: 0.9rem system-ui, sans-serif; margin: 1rem; }
.row { display: flex; gap: 0.4rem; align-items: center; margin-bottom: 0.6rem; }
:where(.row) span { width: 2.5rem; height: 2.5rem; display: grid; place-items: center; background: #f4c95d; border: 2px solid #8a3b12; font-weight: 700; }
code { margin-left: 0.5rem; font-size: 0.8rem; }
.none { display: none; }
.hidden { visibility: hidden; }
.clear { opacity: 0; }
.cv { content-visibility: hidden; }
.sr-only {
position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
| Method | Takes space | Visible | Clickable and focusable | Read by screen readers |
|---|---|---|---|---|
display: none | no | no | no | no |
hidden attribute | no | no | no | no |
visibility: hidden | yes | no | no | no |
opacity: 0 | yes | no | yes | yes |
.sr-only (visually hidden) | no | no | links and buttons can still get focus | yes |
content-visibility: hidden | the box keeps its own size, contents skipped | the box, not its contents | contents no | contents no |
inert attribute | yes | yes | no | no |
aria-hidden="true" | yes | yes | yes, so use with care | no |

The clicks and focus were tested in the page with elementFromPoint() and focus(). The opacity: 0 row is the dangerous one: invisible, but still in the way.
display: none and hidden
The element and its whole subtree generate no boxes: no space, no rendering, no interaction, and nothing for screen readers. The HTML hidden attribute does the same through the browser's default stylesheet. As article 17 explains, any rule that sets display on the element overrides it, so many resets add:
[hidden] { display: none !important; }
Images inside a display: none element are usually still downloaded; use loading="lazy" for images that may stay hidden.
visibility
visibility: hidden makes the element invisible and non-interactive but keeps its space, as if it were transparent and unclickable. Unlike display: none, it's inherited and can be overridden: a child with visibility: visible inside a hidden parent shows up. Use it when the layout mustn't shift, such as a placeholder for content that appears later.
visibility: collapse hides table rows and columns without recalculating column widths. Firefox also collapses flex items with it; Chrome and Safari treat flex items, like everything else, as hidden, leaving the gap.
opacity: 0
opacity: 0 (article 31) only makes the element transparent. It still takes space, still receives clicks, and is still focusable and read by screen readers. It's mostly used as the start or end of a fade animation. Leaving invisible elements clickable is a common bug: add visibility: hidden or pointer-events: none once the fade ends.
Visually hidden text
Sometimes text should be read by screen readers but not shown, such as a label for an icon-only button, or "(opens in a new tab)". display: none would hide it from screen readers too, so use a visually hidden class:
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
The text is shrunk to a single clipped pixel, outside the flow, but stays in the accessibility tree. Older versions use clip: rect(0 0 0 0), which is deprecated in favor of clip-path. For "skip to content" links, show the element again when it's focused:
.skip-link:not(:focus) { /* the .sr-only declarations */ }
inert
The HTML inert attribute leaves an element visible, but makes it and its descendants unfocusable, unclickable, and hidden from assistive technology. It's what you want for the page behind a custom modal or an off-screen drawer. Browsers' native modal dialogs (article 60) make the rest of the page inert automatically. Style inert content to look disabled, since users otherwise can't tell why it doesn't respond:
[inert] { opacity: 0.5; }
A CSS property for the same thing, interactivity: inert, is in Chrome only.
content-visibility
content-visibility lets the browser skip rendering an element's contents, to save time on long pages:
| Value | Effect |
|---|---|
visible (initial) | normal rendering |
auto | skips layout and painting of the contents while the element is far off-screen; they're still in the page for find-in-page, focus and screen readers |
hidden | always skips the contents, like display: none for them, but the element's box remains |
Because skipped content has no size, pair it with contain-intrinsic-size, an estimated size used while the content is skipped:
.comment, .article-section {
content-visibility: auto;
contain-intrinsic-size: auto 600px; /* estimate 600px tall; "auto" remembers the real size once rendered */
}
Without it, skipped sections collapse to zero height, and the scrollbar jumps as they render. content-visibility: auto is most useful on long, repetitive pages, such as feeds, comment lists and documentation; on short pages the browser renders everything anyway. It's Baseline since September 2025.
The HTML attribute hidden="until-found" uses content-visibility: hidden: the content is hidden, but find-in-page and links to fragments inside it can reveal it, as <details> does. It's in Chrome and Firefox, and partly in Safari 26.2.
Animating display
display can't be animated smoothly, since it switches at a single point, but it can now be transitioned together with other properties, so that an element fades out before it's removed, using transition-behavior: allow-discrete and @starting-style. That's covered in article 108.
What's current
- Two-keyword
displayvalues (inline flex,block flow-root) work everywhere since 2023. content-visibilityis Baseline since September 2025.hidden="until-found"is in Chrome and Firefox; Safari's support is partial.inertworks everywhere; the CSSinteractivityproperty is Chrome only.- Most
display: contentsaccessibility bugs have been fixed, but test interactive elements.
Common questions
What's the difference between display: none and visibility: hidden?
display: none removes the box, so the space closes up. visibility: hidden keeps the space, and children can override it with visibility: visible. Neither is focusable or announced by screen readers.
How do I hide something visually but keep it for screen readers?
Use a visually hidden class like .sr-only. display: none, visibility: hidden and the hidden attribute hide it from screen readers too.
Why can I still click my faded-out menu?
opacity: 0 doesn't stop interaction. Add visibility: hidden (which can be transitioned with it) or pointer-events: none when it's hidden.
Why doesn't the hidden attribute hide my element?
Your CSS sets display on it, which overrides the browser's [hidden] { display: none }. Add [hidden] { display: none !important; }.
Does display: contents break accessibility?
It used to drop the semantics of buttons, headings, lists and tables. Current browsers have fixed most cases, but avoid it on interactive elements and test with a screen reader.
Summary
displaysets an outer type (block or inline) and an inner type (flow, flow-root, flex, grid, table). One-word values are shorthands for those pairs.displaychanges boxes, not elements or their meaning.display: contentsremoves an element's box and promotes its children; use it carefully on semantic elements.display: noneandhiddenremove content completely;visibility: hiddenkeeps the space;opacity: 0keeps everything except visibility.- Visually hidden classes hide content from sight only;
inertkeeps it visible but unusable. content-visibility: autowithcontain-intrinsic-sizeskips rendering off-screen content for speed.
Practice questions
A tooltip should keep its space in the layout while hidden, and must not be clickable. Which declaration do you use?
Answer
visibility: hidden. It keeps the space and blocks interaction, unlike opacity: 0, and unlike display: none it doesn't change the layout.
An icon-only button needs an accessible name in the HTML, without visible text. Write the markup.
Answer
<button><svg aria-hidden="true">…</svg><span class="sr-only">Search</span></button>
aria-label="Search" on the button also works.
What is inline flex as a one-word value, and how does it behave?
Answer
inline-flex. The element sits in a line like inline content, and lays out its own children as flex items.
A long documentation page renders slowly. Write CSS that lets the browser skip rendering off-screen sections, each about 800px tall, without the scrollbar jumping.
Answer
.doc-section { content-visibility: auto; contain-intrinsic-size: auto 800px; }
Previous: Inline Formatting: Line Boxes, line-height, vertical-align and text-box-trim
Next: Logical Properties and Writing Modes: inline, block, start and end

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.