content-visibility

Lets the browser skip rendering content that isn’t visible, speeding up long pages.

Initial value
visible
Inherited
No
Animatable
discretely, e.g. with transition-behavior: allow-discrete
Applies to
elements for which size containment applies (not plain inline elements or internal table elements)
JavaScript
element.style.contentVisibility = "auto"
On this page

Example

#
CSS
.comment {
  content-visibility: auto;
  contain-intrinsic-size: auto 44px;
}
Preview

In such a short thread you won’t see the effect, since all the comments fit on screen. In a long discussion, the browser only renders the comments near the viewport, and gives the rest 44px of placeholder height until it learns the real one.

Definition and usage

#

The content-visibility property decides whether the browser even bothers laying out and painting an element’s contents. The value auto skips work on pieces that are far off-screen, and comes back to them once the user scrolls closer. On long pages with many sections, such as articles, documentation, or comment lists, this can noticeably shorten the first render.

A skipped element doesn’t know its own height, so without a hint it would have zero pixels, and the scrollbar would jump around while scrolling. That’s why auto pairs with contain-intrinsic-size, which supplies a placeholder size. Writing contain-intrinsic-size: auto 500px means: start at 500px, and after the first render, remember the real size. One value applies to both dimensions, two mean width and height.

The value hidden always skips the content, similar to display: none on the children, but leaves the element itself with its border and padding, and the browser can keep the results of earlier renders, so showing it again is faster. It suits cards and panels that get hidden and shown often. Content hidden with hidden isn’t available to screen readers or on-page search, while with auto it stays available.

The value auto always turns on layout, style, and paint containment, just like contain: content. In practice, descendants that stick out past the element get clipped, and the element becomes a containing block for absolutely positioned children.

Syntax

#
Syntax
content-visibility: visible | hidden | auto;
contain-intrinsic-size: none | [ auto? <length> ]{1,2};

Values

#
  • visible

    Normal rendering. The badge sticks out past the card’s corner. This is the default value.
  • auto

    The card is on screen, so it renders normally, but look at the corner: the badge got clipped, because auto always turns on paint containment. Off-screen, the browser would skip the whole content.
  • hidden

    The content isn’t rendered. Only the card remains, with its padding and border, which has no height without content.
  • hidden + contain-intrinsic-size

    with a placeholder size

    The placeholder size holds the empty card’s height, so nothing around it jumps.

Values in practice

#

visible

Normal rendering. The badge sticks out past the card’s corner. This is the default value.
.demo { content-visibility: visible; }

auto

The card is on screen, so it renders normally, but look at the corner: the badge got clipped, because auto always turns on paint containment. Off-screen, the browser would skip the whole content.
.demo { content-visibility: auto; }

hidden

The content isn’t rendered. Only the card remains, with its padding and border, which has no height without content.
.demo { content-visibility: hidden; }

hidden + contain-intrinsic-size

The placeholder size holds the empty card’s height, so nothing around it jumps.
.demo { content-visibility: hidden; contain-intrinsic-size: auto 90px; }

Browser support

#

Newly available · since 2024

Works in the current versions of all major browsers. Older versions may not support it.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Use content-visibility: auto on repeating blocks with substantial content, such as article sections or comments in a long thread, not on single small elements. The gain comes from skipping many blocks at once.
  • Always add contain-intrinsic-size: auto with an estimated size, so the scrollbar doesn’t jump around. The word auto remembers the real size after the first render.
  • Don’t use auto on elements that need something to stick out, such as dropdown menus or speech bubbles. Paint containment will clip them.

Related entries

#

Spotted a mistake or missing an example? Write to us.