What is content-visibility in CSS: Explanation and Examples
content-visibility CSS property controls whether an element's content is rendered and affects layout and painting. It helps improve page performance by skipping rendering of off-screen or hidden content until needed.How It Works
The content-visibility property acts like a gatekeeper for the browser. When set, it tells the browser to skip rendering and layout calculations for an element's content until that content is needed, such as when it scrolls into view.
Think of it like a book with pages sealed shut. You don’t open or read the pages until you actually need to, saving time and effort. Similarly, the browser avoids processing hidden or off-screen parts of a page, which speeds up loading and scrolling.
This property can also create a new formatting context, isolating the element’s layout from the rest of the page, which helps with performance and prevents layout shifts.
Example
This example shows a long list where only visible items are rendered. The rest are skipped until scrolled into view, improving performance.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>content-visibility Example</title> <style> .container { height: 150px; overflow-y: scroll; border: 2px solid #333; } .item { height: 50px; margin: 5px; background-color: #a0c4ff; content-visibility: auto; contain-intrinsic-size: 60px; display: flex; align-items: center; justify-content: center; font-weight: bold; border-radius: 4px; } </style> </head> <body> <div class="container" aria-label="Scrollable list"> <div class="item">Item 1</div> <div class="item">Item 2</div> <div class="item">Item 3</div> <div class="item">Item 4</div> <div class="item">Item 5</div> <div class="item">Item 6</div> <div class="item">Item 7</div> <div class="item">Item 8</div> <div class="item">Item 9</div> <div class="item">Item 10</div> </div> </body> </html>
When to Use
Use content-visibility when you have large or complex content that is off-screen or hidden initially, like long lists, tabs, or expandable sections. It helps speed up page load and smooth scrolling by delaying work until content is needed.
For example, on a news site with many articles, you can apply it to article previews below the fold so the browser skips rendering them until the user scrolls down.
It is especially useful on mobile devices where performance and battery life matter.
Key Points
- content-visibility: auto; lets the browser skip rendering off-screen content.
- Improves page speed and reduces layout work.
- Creates a new formatting context to isolate layout.
- Works well for large, scrollable, or hidden content.
- Use
contain-intrinsic-sizeto reserve space and avoid layout jumps.
Key Takeaways
content-visibility improves performance by skipping rendering of off-screen content.content-visibility: auto; on large or hidden elements to speed up page load.contain-intrinsic-size to reserve space and prevent layout shifts.