CSS Performance
๐ Covered in this chapter:
File Size ยท Unused CSS ยท Critical CSS ยท Loading ยท Expensive Selectors ยท Reflow ยท Repaint ยท Compositing ยท Transform vs Position ยท Animation Performance ยท Image Optimization ยท Auditing ยท Caching
Welcome to Chapter 47: CSS Performance โ part of the CSS Complete Roadmap. This lesson covers all subtopics with clear explanations, code examples, and best practices used in professional web development.
๐ What You Will Learn
- CSS file size optimization and unused CSS removal
- Critical CSS inlining for faster First Contentful Paint
- Reflow vs repaint vs compositing โ browser rendering pipeline
- Animation performance โ transform/opacity vs width/height
- Performance auditing with Lighthouse and DevTools
๐ก Why This Matters
CSS directly impacts Core Web Vitals. Large CSS files block rendering. Animating width/height causes layout thrashing. Understanding the browser rendering pipeline helps you write CSS that runs at 60fps and loads in milliseconds.
1CSS Loading & File Size
HTML โ Efficient CSS Loadingโถ Try in Editor
- PurgeCSS / Tailwind JIT: Remove unused classes from production build
- Split CSS: critical.css (inline) + main.css (deferred)
- Minify: cssnano, clean-css โ remove whitespace and comments
- Cache:
Cache-Control: max-age=31536000with hashed filenames - Avoid @import in CSS: blocks parallel downloads โ use HTML link tags
2Rendering Pipeline โ Reflow, Repaint, Compositing
JavaScript โ Style โ Layout โ Paint โ Composite
โ โ โ โ
cheap expensive expensive cheap
Layout (Reflow): width, height, margin, padding, display
Paint: color, background, box-shadow, border-radius
Composite only: transform, opacity (GPU accelerated โ
)
CSS โ Performance Comparisonโถ Try in Editor
/* โ SLOW โ triggers Layout every frame */
.bad-animation {
animation: move 1s infinite;
}
@keyframes move {
from { left: 0; }
to { left: 200px; }
}
/* โ
FAST โ compositor only, GPU accelerated */
.good-animation {
animation: move 1s infinite;
}
@keyframes move {
from { transform: translateX(0); }
to { transform: translateX(200px); }
}
/* โ
Also fast */
.fade { transition: opacity 200ms; }
.card:hover { opacity: 0.9; }
3Selector Performance & Auditing
- Right-most selector matters most โ browser matches right to left
- Avoid:
*, deep nesting,[attr*=""]universal attribute - Prefer: Single class selectors
.nav-link - will-change:
will-change: transform;hint browser to optimize (use sparingly) - contain:
contain: layout style;isolates rendering scope
Audit tools: Lighthouse (Chrome DevTools), WebPageTest, CSS Stats, Coverage tab in DevTools (shows unused CSS %).
โ ๏ธ Common Mistakes
- Animating width, height, top, left โ causes layout reflow every frame
- will-change on everything โ wastes GPU memory
- @import chains in CSS โ serial loading
- 500KB CSS file with 80% unused styles
- Not using font-display: swap โ invisible text during font load
โ Mini Quiz
Q1 transform vs left animation โ eppudu transform fast?
transform and opacity composite layer lo run avuthayi โ GPU accelerated, no layout reflow. left/top trigger layout every frame.
Q2 Critical CSS enti?
Above-the-fold content ki required minimum CSS โ inline in head for faster First Contentful Paint. Rest deferred.
โ
Quick Recap
- Minify, purge unused CSS, cache with hashed filenames
- Animate transform and opacity only โ avoid width/height/top/left
- Critical CSS inline, defer non-critical stylesheets
- Lighthouse and Coverage tab for auditing
- contain and will-change for targeted optimization