GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

`content-visibility` CSS property

The content-visibility CSS property controls whether or not an element renders its contents at all, along with forcing a strong set of containments, allowing user agents to potentially omit large swathes of layout and rendering work until it becomes needed. It enables the user agent to skip an element's rendering work (including layout and painting) until it is needed — which makes the initial pag

The **`content-visibility`** [CSS](/docs/Web/CSS) property controls whether or not an element renders its contents at all, along with forcing a strong set of containments, allowing user agents to potentially omit large swathes of layout and rendering work until it becomes needed. It enables the user agent to skip an element's rendering work (including layout and painting) until it is needed — which makes the initial page load much faster.

> [!NOTE]
> The contentvisibilityautostatechange event fires on any element with `content-visibility: auto` set on it when its rendering work starts or stops being skipped. This provides a convenient way for an app's code to start or stop rendering processes (e.g., drawing on a canvas) when they are not needed, thereby conserving processing power.

CSS Demo: content-visibility

```css interactive-example-choice
content-visibility: visible;
```

```css interactive-example-choice
content-visibility: hidden;
```

```html interactive-example
<section class="default-example" id="default-example">
<div class="container" id="example-element">
<div class="child">
<span>This is an inner div</span>
</div>
</div>
</section>
```

```css interactive-example
.container {
width: 140px;
height: 140px;
border: 3px solid rgb(64 28 163);
background-color: rgb(135 136 184);
display: flex;
align-items: center;
justify-content: center;
}

.child {
border: 3px solid rgb(64 28 163);
background-color: wheat;
color: black;
width: 80%;
height: 80%;
display: flex;
align-items: center;
justify-content: center;
}
```

## Syntax

```css
/* Keyword values */
content-visibility: visible;
content-visibility: hidden;
content-visibility: auto;

/* Global values */
content-visibility: inherit;
content-visibility: initial;
content-visibility: revert;
content-visibility: revert-layer;
content-visibility: unset;
```

### Values

This property is specified as one of the following keyword values:

- `visible`
- : No effect. The element's contents are laid out and rendered as normal. This is the default value.
- `hidden`
- : The element [skips its contents](/docs/Web/CSS/Guides/Containment/Using#skips_its_contents). The skipped contents must not be accessible to user-agent features, such as find-in-page, tab-order navigation, etc., nor be selectable or focusable. This is similar to giving the contents `display: none`.
- `auto`
- : The element turns on layout containment, style containment, and paint containment. If the element is not [relevant to the user](/docs/Web/CSS/Guides/Containment/Using#relevant_to_the_user), it also skips its contents. Unlike hidden, the skipped contents must still be available as normal to user-agent features such as find-in-page, tab order navigation, etc., and must be focusable and selectable as normal.

## Description

### Animating and transitioning content-visibility

[Supporting browsers](#browser_compatibility) animate/transition `content-visibility` with a variation on the [discrete animation type](/docs/Web/CSS/Guides/Animations/Animatable_properties#discrete).

Discrete animation generally means that the property will flip between two values 50% of the way through the animation. In the case of `content-visibility`, however, the browser will flip between the two values to show the animated content for the entire animation duration. So, for example:

- When animating `content-visibility` from `hidden` to `visible`, the value will flip to `visible` at `0%` of the animation duration so it is visible throughout.
- When animating `content-visibility` from `visible` to `hidden`, the value will flip to `hidden` at `100%` of the animation duration so it is visible throughout.

This behavior is useful for creating entry/exit animations where you want to, for example, remove some content from the DOM with `content-visibility: hidden`, but you want a smooth transition (such as a fade-out) rather than it disappearing immediately.

When animating `content-visibility` with [CSS transitions](/docs/Web/CSS/Guides/Transitions), [`transition-behavior: allow-discrete`](/docs/Web/CSS/Reference/Properties/transition-behavior) needs to be set on `content-visibility`. This effectively enables `content-visibility` transitions.

> [!NOTE]
> When transitioning an element's `content-visibility` value, you don't need to provide a set of starting values for transitioned properties using a @starting-style block, like you do when [transitioning `display`](/docs/Web/CSS/Reference/Properties/display#animating_display). This is because `content-visibility` doesn't hide an element from the DOM like `display` does: it just skips rendering the element's content.

## Accessibility

Off-screen content within a `content-visibility: auto` property remains in the document object model and the accessibility tree. This allows improving page performance with `content-visibility: auto` without negatively impacting accessibility.

Since styles for off-screen content are not rendered, elements intentionally hidden with `display: none` or `visibility: hidden` _will still appear in the accessibility tree_.
If you don't want an element to appear in the accessibility tree, use `aria-hidden="true"`.

## Formal definition

## Formal syntax

## Examples

### Using auto to reduce rendering cost of long pages

The following example shows the use of `content-visibility: auto` to skip painting and rendering of off-screen sections.
When a `section` is out of the viewport then the painting of the content is skipped until the section comes close to the viewport, this helps with both load and interactions on the page.

#### HTML

```html
<section>
<!-- Content for each section… -->
</section>
<section>
<!-- Content for each section… -->
</section>
<section>
<!-- Content for each section… -->
</section>
<!-- … -->
```

#### CSS

The `contain-intrinsic-size` property adds a default size of 500px to the height and width of each `section` element. After a section is rendered, it will retain its rendered intrinsic size, even when it is scrolled out of the viewport.

```css
section {
content-visibility: auto;
contain-intrinsic-size: auto 500px;
}
```

### Using hidden to manage visibility

The following example shows how to manage content visibility with JavaScript.
Using `content-visibility: hidden;` instead of `display: none;` preserves the rendering state of content when hidden and rendering is faster.

#### HTML

```html
<div class="hidden">
<button class="toggle">Show</button>
<p>
This content is initially hidden and can be shown by clicking the button.
</p>
</div>
<div class="visible">
<button class="toggle">Hide</button>
<p>
This content is initially visible and can be hidden by clicking the button.
</p>
</div>
```

#### CSS

The `content-visibility` property is set on paragraphs that are direct children of elements with the `visible` and `hidden` classes. In our example, we can show and hide content in paragraphs depending on the CSS class of parent div elements.

The `contain-intrinsic-size` property is included to represent the content size. This helps to reduce layout shift when content is hidden.

```css
p {
contain-intrinsic-size: 0 1.1em;
border: dotted 2px;
}

.hidden > p {
content-visibility: hidden;
}

.visible > p {
content-visibility: visible;
}
```

#### JavaScript

```js
const handleClick = (event) => {
const button = event.target;
const div = button.parentElement;
button.textContent = div.classList.contains("visible") ? "Show" : "Hide";
div.classList.toggle("hidden");
div.classList.toggle("visible");
};

document.querySelectorAll("button.toggle").forEach((button) => {
button.addEventListener("click", handleClick);
});
```

#### Result

using_hidden_to_manage_visibility

### Animating content-visibility

In this example, we have a div element, the content of which can be toggled between shown and hidden by clicking or pressing any key.

#### HTML

```html
<p>
Click anywhere on the screen or press any key to toggle the
<code>&lt;div&gt;</code> content between hidden and showing.
</p>

<div>
This is a <code>&lt;div&gt;</code> element that animates between
<code>content-visibility: hidden;</code>and
<code>content-visibility: visible;</code>. We've also animated the text color
to create a smooth animation effect.
</div>
```

#### CSS

In the CSS we initially set `content-visibility: hidden;` on the `<div>` to hide its content. We then set up `@keyframes` animations and attach them to classes to show and hide the `<div>`, animating `content-visibility` and color so that you get a smooth animation effect as the content is shown/hidden.

```css
div {
font-size: 1.6rem;
padding: 20px;
border: 3px solid red;
border-radius: 20px;
width: 480px;

content-visibility: hidden;
}

/* Animation classes */

.show {
animation: show 0.7s ease-in forwards;
}

.hide {
animation: hide 0.7s ease-out forwards;
}

/* Animation keyframes */

@keyframes show {
0% {
content-visibility: hidden;
color: transparent;
}

100% {
content-visibility: visible;
color: black;
}
}

@keyframes hide {
0% {
content-visibility: visible;
color: black;
}

100% {
content-visibility: hidden;
color: transparent;
}
}
```

#### JavaScript

Finally, we use JavaScript to apply the `.show` and `.hide` classes to the `<div>` as appropriate to apply the animations as it is toggled between shown and hidden states.

```js
const divElem = document.querySelector("div");
const htmlElem = document.querySelector(":root");

htmlElem.addEventListener("click", showHide);
document.addEventListener("keydown", showHide);

function showHide() {
if (divElem.classList.contains("show")) {
divElem.classList.remove("show");
divElem.classList.add("hide");
} else {
divElem.classList.remove("hide");
divElem.classList.add("show");
}
}
```

#### Result

The rendered result looks like this:

100%

## Specifications

## Browser compatibility

## See also

- [CSS Containment](/docs/Web/CSS/Guides/Containment)
- contain-intrinsic-size
- contentvisibilityautostatechange
- [content-visibility: the new CSS property that boosts your rendering performance](https://web.dev/articles/content-visibility) (web.dev)

source: MDN Web Docs mirror · page

Cite this

"`content-visibility` CSS property". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/content-visibility

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·