CSS Performance
In this page:
Understanding Paint and Reflow Blocks
When an element's style changes, the browser runs two major rendering steps: Reflow (recalculating the physical layout positions of elements) and Paint (re-drawing pixels on the screen). Reflow is extremely resource-heavy and can slow down page speeds if triggered frequently.
Example: Understanding Paint and Reflow Blocks
<style>
.cheap {
background: royalblue;
color: white;
padding: 10px;
transition: transform 0.3s, opacity 0.3s;
}
.cheap:hover {
transform: scale(1.05);
opacity: 0.9;
}
</style>
<div class="cheap">transform/opacity skip layout reflow</div>
Minimizing Selector Complexity
Browsers read CSS selectors from right to left. A deeply nested selector path (like 'body div.container ul li a') forces the browser to scan your entire DOM tree multiple times to find matching elements, slowing down rendering speeds.
Example: Minimizing Selector Complexity
<style>
/* Slow: deep nested path, scanned right to left */
body div.container ul li a { color: blue; }
/* Fast: flat single class */
.nav-link { color: blue; }
</style>
<div class="container"><ul><li><a class="nav-link" href="#">Home</a></li><li><a class="nav-link" href="#">About</a></li></ul></div>
Hardware Acceleration with will-change
The will-change property warns the browser that an element is about to be animated, allowing it to prepare the GPU and run the animation smoothly.
- Only apply will-change to elements with complex, high-performance animations, and remove it once the animation finishes.
- Accepted values:
- will-change: transform — promotes to its own layer
- will-change: opacity — same for opacity
- will-change: scroll-position — scrolling is about to change
- will-change: contents — the element's contents will change
- will-change: auto — the default
- use sparingly — each layer costs memory
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Hardware Acceleration with will-change
<style>
.modal { width: 200px; padding: 20px; background: #cfe8ff; border: 2px solid #1e6fd9; }
.modal {
will-change: transform;
}
</style>
<div class="modal">Modal promoted to its own layer</div>
The critical CSS Path
Critical Path CSS is the technique of extracting and inlining the styles needed to render the top portion of your page (the above-the-fold content) directly inside your HTML head tag, allowing the page to display instantly while external stylesheets load in the background.
Example: The critical CSS Path
<head>
<style>
header { background: navy; color: white; padding: 20px; }
</style>
</head>
<header>Above-the-fold styles inlined here load before the external stylesheet</header>
Performance Isolation with contain
The contain property allows you to isolate specific elements from the rest of the page layout, telling the browser that changes inside this container will never affect the layout of surrounding elements, preventing expensive page-wide reflows.
- Use contain: content on heavy, dynamic widgets (like chat widgets or infinite scroll feeds) to optimize rendering speeds.
- Accepted values:
- content — shorthand for layout paint style
- strict — shorthand for size layout paint style
- layout — isolates internal layout from the outside
- paint — clips painting to the box
- size — box size independent of children
- inline-size — size containment on the inline axis only
- style — scopes counters to the subtree
- none — default, no containment
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Performance Isolation with contain
<style>
.widget { background: #cfe8ff; border: 2px solid #1e6fd9; padding: 16px; width: 220px; }
.widget {
contain: content;
}
</style>
<div class="widget">Widget isolated from the rest of the page layout</div>
- Using heavy, complex animations on structural properties like width and height, causing page lag.
- Writing deeply nested, inefficient CSS selector paths that require the browser to scan the entire DOM tree.
- Overusing the will-change property on too many elements, which consumes substantial GPU memory and slows down devices.
- Minimize expensive page layout reflows by using lightweight properties like transform and opacity for animations.
- Write flat, optimized class selectors (like .nav-link) to make matching fast and efficient for the browser.
- Use critical path CSS to render the top portion of your page instantly, and the contain property to isolate dynamic widgets cleanly.
Standard performance optimization properties like transform, opacity, and contain are supported natively by all modern web browsers.
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep