CSS Performance
Optimize CSS for faster loading, better user experience, and improved Core Web Vitals.
How do I make CSS load and render faster?
Ship less CSS (remove unused rules and minify), avoid @import chains that load files one after another, and keep render-blocking CSS small. Animate only transform and opacity, use content-visibility: auto for long below-the-fold sections, and preload or self-host fonts with font-display: swap.
.long-section { content-visibility: auto; contain-intrinsic-size: 1px 800px; }
.card:hover { transform: translateY(-2px); } /* not top/margin */Performance-Optimized CSS
Code Editor
<section class="long">Below-the-fold section 1</section>
<section class="long">Below-the-fold section 2</section>
<section class="long">Below-the-fold section 3</section>Preview
Critical CSS Strategy
Above-the-Fold Optimization
<!-- Inline critical CSS in head -->
<style>
/* Critical styles for immediate render */
body { margin: 0; font-family: system-ui; }
.header { background: #1e40af; padding: 1rem; }
.hero { padding: 2rem; text-align: center; }
</style>
<!-- Async load non-critical CSS -->
<link rel="preload" href="main.css" as="style"
onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="main.css"></noscript>
<!-- DNS prefetch for external resources -->
<link rel="dns-prefetch" href="//fonts.googleapis.com">
<link rel="dns-prefetch" href="//cdnjs.cloudflare.com">Inline critical CSS to eliminate render-blocking and load non-critical styles asynchronously.
Resource Optimization
/* Minimize HTTP requests */ @import url('fonts.css');
@import url('components.css');
/* Better: Combine files */ /* fonts.css + components.css = main.css */
/* Use font-display for web fonts */ @font-face {
font-family: 'MyFont';
src: url('font.woff2') format('woff2');
font-display: swap; /* Show fallback immediately */
}
/* Preload important assets */ <link rel="preload" href="hero-image.webp" as="image">
<link rel="preconnect" href="https://fonts.googleapis.com">Reduce requests, optimize fonts, and preload critical assets
CSS Delivery
/* CSS Loading Strategy */
/* 1. Critical CSS (inline) */
/* Header, hero, above-fold content */
/* 2. High priority (preload) */
/* Layout, typography, key components */
/* 3. Medium priority (async) */
/* Below-fold content, interactions */
/* 4. Low priority (defer) */
/* Print styles, non-essential */
/* Conditional loading */ <link rel="stylesheet" href="mobile.css"
media="(max-width: 768px)">
<link rel="stylesheet" href="print.css"
media="print">Strategic loading based on content priority and user needs
Selector & Layout Performance
Efficient Selectors
/* ✅ FAST SELECTORS */
.button { } /* Class selector - fastest */
#header { } /* ID selector - very fast */
.nav-item { } /* Single class - efficient */
.card__title { } /* BEM naming - predictable */
/* ✅ GOOD COMBINATIONS */
.sidebar .nav-item { } /* Scoped, specific */
.button:hover { } /* Pseudo-class - fine */
.modal.is-open { } /* State class - good */
/* ❌ SLOW SELECTORS */
* { } /* Universal - avoid */
div > div > div { } /* Deep nesting - slow */
[data-attribute*="value"] { } /* Attribute contains - expensive */
.parent .child .grandchild { } /* Over-qualified - slow */
div.content p:nth-child(3n+1) { } /* Complex - very slow */
/* ⚡ PERFORMANCE TIPS */
/* Right-to-left matching: .nav .item matches all .item first */
/* Keep selectors short and specific */
/* Use classes instead of complex selectors */CSS selectors are matched right-to-left. Keep them simple and specific.
Layout Performance
/* ⚡ USE TRANSFORM INSTEAD OF POSITION */ /* ❌ Causes layout recalculation */ .element {
left: 100px;
top: 100px;
}
/* ✅ Uses compositor layer */ .element {
transform: translate(100px, 100px);
}
/* ⚡ FORCE HARDWARE ACCELERATION */ .animated-element {
transform: translateZ(0);
will-change: transform;
}
/* ⚡ USE CONTAIN PROPERTY */ .component {
contain: layout style paint;
}
/* ⚡ EFFICIENT ANIMATIONS */ @keyframes slideIn {
from { transform: translateX(-100%); }
to { transform: translateX(0); }
}Use transform and opacity for animations to avoid reflows
Rendering Optimization
/* AVOID EXPENSIVE PROPERTIES */ /* These trigger layout (reflow) */
.avoid {
width: 100px; /* Layout */
height: 100px; /* Layout */
padding: 10px; /* Layout */
margin: 10px; /* Layout */
border-width: 2px; /* Layout */
}
/* These trigger paint */
.paint-heavy {
background: linear-gradient(...);
box-shadow: 0 0 10px rgba(...);
border-radius: 50%;
color: red;
}
/* COMPOSITOR-ONLY PROPERTIES */ /* These are cheap to animate */
.efficient {
transform: translateX(100px);
opacity: 0.5;
filter: blur(5px);
}
/* BATCH DOM OPERATIONS */
.container {
/* Change multiple properties together */
transform: scale(1.1) rotate(45deg);
}Stick to transform and opacity for smooth animations
Optimization Strategies
Measurement Tools
- • Chrome DevTools Performance tab
- • Lighthouse performance audit
- • WebPageTest for real-world metrics
- • Core Web Vitals monitoring
- • CSS coverage analysis
- • Bundle size analyzers
- • Real User Monitoring (RUM)
- • Performance budgets
Build Optimization
- • Minify and compress CSS files
- • Remove unused CSS (PurgeCSS)
- • Enable gzip/brotli compression
- • Use CSS modules for component isolation
- • Implement critical CSS extraction
- • Tree-shake unused code
- • Optimize vendor prefixes
- • Use HTTP/2 push for critical resources
Performance Checklist
Loading
- □ Critical CSS inlined (< 14KB)
- □ Non-critical CSS loaded async
- □ External fonts optimized
- □ DNS prefetch for external domains
- □ Resource hints implemented
Runtime
- □ Efficient selectors used
- □ Animations use transform/opacity
- □ Layout thrashing avoided
- □ Paint complexity minimized
- □ Accessibility preferences respected
CSS Performance — step by step
Minimize CSS file size
Remove unused rules, whitespace, and comments before production. Use minification tools to reduce file size and improve load speed.
🎯 Quick Check
Which CSS property is generally cheapest for animations?
CSS Performance: summary
Key points
- CSS in the
<head>blocks rendering: the page doesn't paint until it has downloaded. @importinside CSS creates a sequential request waterfall; bundle files instead.- Layout thrashing comes from animating geometry (
width,height,top);transformdoesn't trigger layout. will-changeis a hint for elements that are about to animate. Don't apply it to everything.
Frequently asked questions
›Does CSS affect Core Web Vitals?
Yes. Large render-blocking CSS delays Largest Contentful Paint, images and embeds without set dimensions cause Cumulative Layout Shift, and heavy animations can hurt Interaction to Next Paint.