Published on April 23, 2025 Last Updated on July 1, 2025
Written by
Morgan Frank - Specialist in Page Speed
Cascading Style Sheets CSS are essential for styling your website, but like JavaScript, unoptimized CSS can negatively impact page speed. Large CSS files, unused styles, and inefficient selectors can all contribute to slower rendering and a poorer user experience.
This guide covers three key CSS optimization techniques: extracting critical CSS, removing unused CSS, and optimizing selectors.
Key Takeaways
Critical CSS: Inline minimal above-the-fold styles in <head> to enable immediate rendering without waiting for external CSS downloads. This dramatically improves initial page load metrics.
Remove Unused CSS: Eliminate dead code from stylesheets to reduce file sizes and improve maintainability. Use tools like PurgeCSS or Chrome DevTools Coverage tab.
Optimize Selectors: Write efficient CSS selectors by favoring ID/class selectors over complex descendant chains, avoiding universal selectors (*), and minimizing specificity.
Automate Everything: Integrate these optimizations into your build process using tools like Webpack, Gulp, or Parcel to ensure consistency and efficiency.
Test After Implementation: Verify that optimizations work correctly and monitor performance improvements.
Common Themes:
Performance-first approach: All techniques focus on faster rendering and smaller file sizes
Tooling and automation: Manual optimization is error-prone; automated solutions are preferred
Browser efficiency: Each technique reduces the browser’s workload during parsing and rendering
Build process integration: These optimizations work best when incorporated into development workflows rather than applied as afterthoughts
1. Critical CSS: Rendering the Above-the-Fold Content Faster
As we learned in the section on render-blocking resources, CSS is, by default, render blocking. The browser needs to download and process all of your CSS before it can render anything on the page. However, not all of your CSS is needed to render the initial view of the page (the “above-the-fold” content – what’s visible without scrolling).
Critical CSS is the minimal set of CSS rules required to style the above-the-fold content. By inlining this critical CSS directly into the <head> of your HTML, you allow the browser to render the initial view immediately, without waiting for external CSS files to download.
Benefits of Critical CSS:
Faster First Contentful Paint (FCP) and Largest Contentful Paint (LCP): The user sees content much sooner, improving perceived performance.
Improved User Experience: The page appears to load faster, reducing bounce rates and increasing engagement.
Reduced Render Blocking: By inlining the critical CSS, you eliminate the render blocking nature of that portion of your CSS.
How to Extract Critical CSS:
1.Manual Extraction: You can manually identify the above-the-fold content and copy the relevant CSS rules into a separate file (or directly into your HTML). This is feasible for small, simple websites, but it’s not scalable.
2.Automated Tools: Several tools can automatically extract critical CSS:
Critical (by Addy Osmani): A popular Node.js module that extracts critical CSS.
Penthouse: Another Node.js module for critical CSS extraction.
CriticalCSS (online tool): A web-based tool for generating critical CSS.
PurgeCSS: Removes Unused CSS
WordPress plugins:
Autoptimize: Includes critical CSS functionality.
WP Rocket: A premium caching plugin that also includes critical CSS generation.
LiteSpeed Cache: Another option.
How to Inline Critical CSS:
Once you’ve extracted the critical CSS, you inline it directly into the <head> of your HTML, within <style> tags:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
/* Your critical CSS here */
body {
font-family: sans-serif;
background-color: #f0f0f0;
}
h1
color: #333;
}
/* ... */
</style>
<!--Load the rest of your CSS asynchronously or deferred ⟶
<link rel="preload" href="non-critical.css" as="style"
onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="non-critical.css"></noscript>
</head>
<body>
<!--... ⟶
</body>
`</html>`
Important Considerations:
Keep it Small: The critical CSS should be as small as possible. Only include the styles that are absolutely necessary for the initial view.
Automate: Automate the process of extracting and inlining critical CSS as part of your build process. Don’t rely on manual extraction.
Test: Test your implementation thoroughly to ensure it doesn’t break your page layout or styling.
2. Removing Unused CSS: Decluttering Your Stylesheets
Over time, websites often accumulate unused CSS rules. These are styles that were defined but are no longer applied to any elements on the page. Unused CSS adds unnecessary weight to your CSS files, slowing down download times and making your code harder to maintain.
Benefits of Removing Unused CSS:
Smaller File Sizes: Reducing the amount of CSS directly reduces file size, leading to faster downloads.
Faster Parsing: The browser can parse and process smaller CSS files more quickly.
Improved Maintainability: Cleaner, more concise CSS code is easier to understand and maintain.
Tools for Removing Unused CSS:
PurgeCSS: A powerful tool that analyzes your HTML and CSS and removes unused styles. It can be integrated into your build process (Webpack, Gulp, etc.).
UnCSS: Another popular tool for removing unused CSS (also a Node.js module).
Chrome DevTools Coverage Tab): The Coverage tab in Chrome DevTools shows you which parts of your CSS (and JavaScript) files are actually being used on a page. This is great for manual analysis.
PurifyCSS: Another option.
Workflow for Removing Unused CSS:
Analyze: Use one of the tools above to analyze your website and identify unused CSS.
Remove: Carefully remove the unused CSS from your stylesheets.
Test: Thoroughly test your website to ensure that removing the CSS hasn’t broken any styling or layout. It’s crucial to test on different pages and different screen sizes.
Automate: Integrate the process of removing unused CSS into your build process to prevent unused styles from creeping back in.
The way you write your CSS selectors can also impact performance. Complex, inefficient selectors can force the browser to do more work to match styles to elements.
General Principles for Optimizing Selectors:
Avoid the Universal Selector (*):The universal selector (*) matches every element on the page, which is very inefficient.
Be Specific, but Not Too Specific: Use the most specific selector that’s necessary, but avoid overly long and complex selectors.
Avoid Descendant Selectors When Possible: Descendant selectors (e.g., ul, li, a) can be slower than more specific selectors. If possible, use a class on the a element instead (e.g., .navigation-link).
Don’t Qualify ID Selectors: ID selectors (#myElement) are already unique, so you don’t need to qualify them with tag names or parent elements (e.g., div#myElement is redundant).
Don’t Qualify Class Selectors with Tag Names (Usually): In most cases, you don’t need to qualify class selectors with tag names (e.g., div.myClass is usually unnecessary; .myClass is sufficient).
Example (Less Efficient):
ul#navigation li a {
color: blue;
}
Example (More Efficient):
#navigation a { /* If the 'a' elements are unique within #navigation */
color: blue;
}
/* Or, even better, use a class: */
.nav-link {
color: blue;
}
Keyhole Selectors:
Keyhole selectors are CSS selectors that unnecessarily traverse a large portion of the DOM tree before reaching the intended target element. For Example:
Use CSS analyzer: Use tools to understand the selector performance.
Conclusion
Optimizing your CSS is a crucial part of improving website performance. By extracting critical CSS, removing unused styles, and writing efficient selectors, you can significantly reduce the size and complexity of your CSS files, leading to faster rendering, a better user experience, and improved search engine rankings. These techniques work together to ensure that your CSS is as lean and efficient as possible.
Determined to change that, he built RapidLoad — a smart, AI-driven tool that empowers site owners to dramatically improve speed scores, enhance user experience, and meet Google’s Core Web Vitals without needing to touch a single line of code.