Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Up to date
Published
2 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

CSS content-visibility: The Web Performance Boost You Might Be Missing

The content-visibility CSS property delays rendering an element, including layout and painting, until it is needed

Web performance optimization can be a real headache. Shaving off milliseconds here and there, minifying everything in sight, and praying to the performance gods.

Picture this: you’re scrolling through a massive webpage with hundreds of images, complex layouts, and enough content to fill a book. Without content-visibility, browsers dutifully render every single pixel, whether you can see it or not. It’s like a restaurant cooking every dish on the menu just in case someone orders it.

The content-visibility property tells browsers they can skip rendering elements until they’re about to enter the viewport. This might sound simple, but it’s a game-changer for long scrolling pages.

The contain-intrinsic-size property is content-visibility’s partner in crime. It provides a size estimate so the browser can reserve space before rendering, preventing layout shifts.

content-visibility works best on pages with lots of complex, off-screen content. News feeds, product listings, and documentation sites are perfect candidates. I’ve seen initial render times improve by 30-50% on content-heavy pages.

One important detail: content-visibility doesn’t just defer paint operations - it skips layout calculations entirely. This means the browser doesn’t waste time figuring out how to position elements nobody can see yet.

content-visibility isn’t free magic though. Each element with content-visibility: auto becomes a separate containing block. This can affect positioning and scroll anchoring. Here’s what to watch for:

After experimenting with content-visibility across different scenarios, I’ve found it works best when:

  1. Sections are clearly defined and independent
  2. Content below the fold is complex
  3. Users typically don’t need immediate access to hidden content

Feeds are a great example - users rarely jump straight to the bottom, and each post is self-contained.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Webdev
12 min read

Frontend Security Checklist

Tips for Keeping All Frontend Applications Secure

Jul 30, 2024
Read article
Webdev
8 min read

Stop Using localStorage for Sensitive Data: Here's Why and What to Use Instead

Understanding the security risks of localStorage and what to use instead for tokens, secrets, and sensitive user data

Oct 28, 2024
Read article
Webdev
3 min read

align-content: The Simplest Way to Center Content with CSS

Finally, we can center things in block layouts without flexbox gymnastics

Dec 13, 2024
Read article
Webdev
4 min read

The What, Why, and How of Using a Skeleton Loading Screen

Skeleton loading screens enhance user experience and make your app feel faster

Nov 12, 2020
Read article
Webdev
3 min read

CSS :has() - The Parent Selector We've Always Wanted

Transform your CSS with :has(), the game-changing selector that finally lets us style elements based on their children.

Dec 4, 2024
Read article
Webdev
4 min read

Mental Toughness is the Best Quality a Developer Can Have

Mental toughness gets developers through challenges like debugging, picking up new tools, and hitting tight deadlines. It’s about staying calm and pushing through when things get tough.

Sep 12, 2024
Read article
Webdev
6 min read

Inside the CSS Engine: CSSOM Explained

A deep dive into how browsers parse and manipulate CSS, its impact on web performance, and why it matters

Oct 25, 2024
Read article
Webdev
7 min read

How to Land Your First Tech Job

A developer's guide to tech interviews - from someone who sits on both sides of the table

Oct 24, 2024
Read article
Webdev
3 min read

HTML Details Element: The Native Accordion You're Not Using

Discover how the HTML details element can replace your JavaScript accordions and why it might be better than your current solution

Dec 10, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/css-content-visibility. It was written by a human and polished using grammar tools for clarity.