Critical CSS Generator
What is Critical CSS?
Critical CSS is the minimum set of CSS needed to render the above-the-fold content (the part of the webpage visible without scrolling). By inlining critical CSS and deferring non-critical styles, you can significantly improve page load performance and Core Web Vitals scores.
Why Critical CSS Matters
- Faster First Paint: Reduces time to first meaningful content render
- Better Core Web Vitals: Improves Largest Contentful Paint (LCP) score
- Eliminates Render Blocking: Prevents CSS from blocking page rendering
- Improved Perceived Performance: Users see content faster
- Better Mobile Experience: Especially important on slower connections
How to Implement Critical CSS
1. Extract Critical CSS
Use this tool or other tools like:
- Critical (Node.js tool)
- Sitelocity Critical CSS Generator
- Chrome DevTools Coverage tab
2. Inline Critical CSS
<head>
<style>
/* Critical CSS inlined here */
body { margin: 0; font-family: sans-serif; }
.header { background: #333; color: white; padding: 20px; }
/* Only include styles needed for above-the-fold content */
</style>
<!-- Defer non-critical CSS -->
<link rel="preload" href="/styles/main.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/styles/main.css"></noscript>
</head>
3. Using JavaScript to Load CSS Asynchronously
<script>
// Load CSS asynchronously
function loadCSS(href) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = href;
document.head.appendChild(link);
}
// Load non-critical CSS after page load
window.addEventListener('load', function() {
loadCSS('/styles/non-critical.css');
});
</script>
Best Practices
- Keep it small: Critical CSS should be under 14KB (compressed)
- Test on different viewports: Critical CSS differs for mobile vs desktop
- Automate extraction: Use build tools to extract critical CSS automatically
- Update regularly: Regenerate when you make layout changes
- Don't duplicate: Remove critical CSS from main stylesheet to avoid duplication
- Test thoroughly: Ensure deferred styles load correctly
Alternative Approaches
1. HTTP/2 Server Push
Push critical CSS to the browser before it's requested:
# Server configuration
Link: </styles/critical.css>; rel=preload; as=style
2. Modern CSS Loading
<!-- Modern browsers -->
<link rel="stylesheet" href="/styles/main.css" media="print" onload="this.media='all'">
<!-- Fallback for browsers without JS -->
<noscript>
<link rel="stylesheet" href="/styles/main.css">
</noscript>
3. CSS-in-JS Solutions
Frameworks like styled-components or Emotion automatically handle critical CSS extraction in React applications.
Measuring Impact
Use these tools to measure the impact of critical CSS optimization:
- Google Lighthouse: Check performance score and LCP
- WebPageTest: View filmstrip and start render time
- Chrome DevTools: Performance tab and Coverage tool
- PageSpeed Insights: Real-world Core Web Vitals data
Common Pitfalls
- Too much critical CSS: Keep it focused on above-the-fold content only
- Flash of unstyled content (FOUC): Test that deferred styles load properly
- Duplicate CSS: Remove critical styles from main stylesheet
- Outdated critical CSS: Regenerate after design changes
- Not testing mobile: Mobile critical CSS is often different from desktop
Example: Before and After
Before (Render Blocking):
<head>
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/css/components.css">
</head>
Page waits for all CSS to download before rendering
After (Optimized):
<head>
<style>
/* Critical CSS inlined */
</style>
<link rel="preload" href="/css/main.css" as="style" onload="this.rel='stylesheet'">
</head>
Page renders immediately with critical styles
Related Tools
- CSS Minifier - Compress CSS files
- Web Font Loader - Optimize font loading
- Page Speed Analyzer - Test performance
- HTML Minifier - Optimize HTML
- CSS Vendor Prefixer - Add prefixes