Skip to content
Tutorials Center
My learning

Topics

Lesson 58 of 139 ยท Layout Fundamentals

Opacity and Visibility

Compare opacity with alpha colors, and visibility hidden with display none, content-visibility and the hidden attribute. Hide content accessibly and fade it smoothly.

CSS gives you many ways to make something see-through or invisible, and they behave very differently. Some keep the element's space, some remove it. Some still let people click and tab to the invisible element, and some hide it from screen readers too. Picking the wrong one leads to classic bugs: a faded card whose text is unreadable, an invisible menu that still catches clicks, or a hidden button that keyboard users can tab to.

In this lesson you will compare opacity with transparent colors, visibility with display: none, meet content-visibility and the hidden attribute, and learn how to fade elements in and out without leaving invisible traps behind. You already saw a short comparison table in The display Property; here you go deeper.

opacity

opacity sets how opaque a whole element is, from 1 (fully opaque, the default) to 0 (fully transparent). Percentages work too: opacity: 40% is the same as 0.4.

CSS
.disabled-card { opacity: 0.5; }

Three facts about opacity matter in practice:

  • It applies to everything inside the element: text, borders, images, children. The element and its descendants are painted together and then made transparent as one picture.
  • A child cannot undo it. Setting opacity: 1 on a child does nothing, because the child is already part of the faded picture.
  • Any value below 1 creates a stacking context, which can change how z-index behaves for its children. You met this in z-index and Stacking Contexts.

An element with opacity: 0 is invisible but still there: it takes up space, it still receives clicks and focus, and screen readers still read it.

Transparent colors instead of opacity

Often you don't want the whole element to fade, only its background. A semi-transparent card over a photo should have readable, fully opaque text. For that, use a color with an alpha channel instead of opacity:

CSS
.glass {
  background: rgb(255 255 255 / 0.6);   /* only the background is translucent */
  color: #0f172a;                       /* text stays solid */
}

Every color notation has an alpha form: rgb(0 0 0 / 50%), hsl(220 90% 50% / 0.3), oklch(70% 0.15 250 / 0.5), and eight-digit hex like #0f172a80. The transparent keyword is simply a fully transparent black. You learned these notations in Colors.

opacity versus alpha colors

Look at the middle card: the text and the dark border are washed out along with the background, and the child's opacity: 1 can't rescue them. The third card keeps crisp text. As a rule, use alpha colors to make a surface translucent and opacity to fade a whole element, for example when animating it in or out.

visibility

visibility makes an element invisible while keeping its space in the layout. It has three values:

  • visible: the default.
  • hidden: the element is not painted, cannot be clicked or focused, and is removed from the accessibility tree. Its box still takes up room.
  • collapse: for table rows, columns and row groups, and for flex items, it removes the item like display: none would, but the table keeps its column widths. On other elements it acts like hidden.

Unlike opacity and display: none, visibility is inherited and can be overridden. A child with visibility: visible inside a hidden parent shows up again. That makes it useful for tricks like hiding a card but keeping one badge visible, and it is also a trap: a hidden menu whose links say visibility: visible is not really hidden.

Here are the three techniques side by side. Click where each hidden button would be, and press Tab to see which ones can still get focus:

display, visibility and opacity compared

B leaves no gap at all. C leaves a gap but can't be clicked or focused. D leaves a gap and can be clicked and focused: the console logs it, even though nobody can see it. The last row shows a child overriding its parent's visibility: hidden.

The hidden attribute

HTML's hidden attribute hides an element without any CSS of your own: the browser's built-in stylesheet gives it display: none.

HTML
<p id="error" hidden>Please choose a size.</p>
JavaScript
document.querySelector("#error").hidden = false; // show it

It is a clean way to toggle visibility from JavaScript, because it describes state in the HTML. It has one well-known weakness: the browser's display: none has the lowest priority in the cascade, so any display rule of yours wins. If .message { display: flex; } matches the same element, the hidden attribute stops working. Many resets add this rule to protect it:

CSS
[hidden] { display: none !important; }

There is also hidden="until-found". The content is hidden, but the browser's find-in-page (Ctrl+F) and links to a fragment inside it can reveal it, and the element fires a beforematch event when that happens. It is a nice fit for collapsed sections. Chromium-based browsers and recent Firefox support it; elsewhere, the attribute falls back to ordinary hidden.

content-visibility

content-visibility controls whether the browser renders an element's contents at all. It is mainly a performance tool.

  • visible: the default.
  • auto: the browser skips layout and painting of the contents while the element is far off-screen, and renders them as it approaches the viewport. On long pages with many sections, this can noticeably speed up the first render.
  • hidden: the contents are not rendered, like display: none on the children, but the element's own box stays, and the browser can keep cached rendering work for when you show it again.

Skipped content has no size, so pair auto with contain-intrinsic-size to give the browser a size estimate. Otherwise the scrollbar jumps as sections render:

CSS
.article-section {
  content-visibility: auto;
  contain-intrinsic-size: auto 600px;  /* guess 600px tall; remember the real size once rendered */
}

Content skipped by auto stays in the accessibility tree and find-in-page still searches it, so it is safe for long articles. content-visibility is supported in all major browsers since 2024. Learn more about rendering cost in CSS Performance.

Hiding things accessibly

The right technique depends on who should be able to perceive the content:

Technique Visible? Takes space? Clickable / focusable? Screen readers?
display: none / hidden No No No No
visibility: hidden No Yes No No
opacity: 0 No Yes Yes Yes
.visually-hidden class No No Yes, if focusable Yes
aria-hidden="true" (HTML) Yes Yes Yes No
inert (HTML) Yes Yes No No

Some guidelines that follow from the table:

  • To hide something from everyone, use display: none or hidden.
  • To hide something only visually (a label for an icon button, "opens in a new tab" hints), use a visually hidden class, as shown in Accessible CSS.
  • To hide something only from screen readers (a decorative icon next to text), use aria-hidden="true", but never on anything focusable.
  • To keep something visible but unusable, such as the page behind an open panel, use the inert attribute.
  • Never leave interactive elements at opacity: 0 when they are "closed". Keyboard users can still tab into them, and nobody can see where focus went.

Fading in and out

opacity animates smoothly, which makes it the usual choice for fades. The trouble is the end state: after fading out, the element is still there, still clickable and still focusable. The fix is to transition visibility along with it:

CSS
.menu {
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s, visibility 0.25s;
}
.menu.is-open {
  opacity: 1;
  visibility: visible;
}

visibility can't be "half visible", so the browser treats it specially during a transition: it stays visible for as long as either end of the transition is visible. When opening, the menu becomes visible at once and fades in; when closing, it fades out first and only becomes hidden at the very end.

Fading with opacity and visibility

Try removing visibility from both rules and the transition. The fade still looks the same, but now the closed menu's links can be tabbed to and clicked, even though they are invisible.

Animating to and from display: none

display can't be interpolated, so a plain transition on it just switches instantly, and an element that appears with display: block has no "before" state to fade from. Two newer features fix both problems:

  • transition-behavior: allow-discrete (or the allow-discrete keyword in the transition shorthand) lets display take part in a transition. Like visibility, it flips at the right moment: at the start when showing, at the end when hiding.
  • @starting-style defines the styles an element starts from the first time it is rendered, so the browser has something to transition from.
Fading in from display: none

@starting-style and transition-behavior have been supported in all major browsers since 2024. In older browsers the notice simply appears and disappears without a fade, which is a perfectly acceptable fallback. You will use the same techniques with transitions in Transitions.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You want a card with a see-through white background but fully readable text. What should you use?

    Opacity fades the whole element including its children, and children can't undo it. An alpha color only affects the background.
  2. 2. Which hidden element can still be clicked and focused?

    An element at opacity 0 is invisible but otherwise fully present. It keeps its space and still receives clicks, focus and screen-reader attention.
  3. 3. Your element has the hidden attribute, but it is still visible. What is the most likely cause?

    The browser's display: none for [hidden] has the lowest priority. Any author display rule overrides it, which is why resets add [hidden] { display: none !important; }.
  4. 4. Why does transition: opacity 0.3s, visibility 0.3s work for fading a menu out?

    Visibility switches discretely, and while transitioning it stays visible whenever one end is visible. So the menu fades out first and becomes hidden, unclickable and unfocusable at the end.
  5. 5. What is content-visibility: auto mainly used for?

    The browser skips layout and paint for far-off content and renders it as it nears the viewport. Pair it with contain-intrinsic-size to keep scrollbars stable.

Summary

  • opacity fades an element and everything inside it; children can't undo it, and values below 1 create a stacking context.
  • To make only a background translucent, use an alpha color like rgb(255 255 255 / 0.6).
  • display: none removes the box; visibility: hidden keeps the space but hides and disables it; opacity: 0 hides it visually only.
  • visibility is inherited and children can override it; collapse removes table rows and columns cleanly.
  • The hidden attribute is overridden by any author display rule, so protect it with [hidden] { display: none !important; }.
  • content-visibility: auto with contain-intrinsic-size skips rendering off-screen content for speed.
  • Fade with opacity plus visibility, or use allow-discrete and @starting-style to animate to and from display: none.

Next: Positioning โ€” move boxes out of the normal flow with relative, absolute, fixed and sticky positioning.