Critical CSS

When a browser loads a webpage, it must download and process CSS before rendering content.

Large CSS files can block rendering, causing delays in displaying visible content.

Critical CSS is a technique that extracts and prioritizes only the CSS needed for above-the-fold content.

This allows the browser to render the visible portion of the page much faster.

What Is Critical CSS?

Critical CSS refers to the minimal set of styles required to render the visible part of a webpage.

It is inlined directly into the HTML to eliminate render-blocking requests.

Remaining CSS is loaded asynchronously after the initial render.

Above-the-Fold Content

Above-the-fold content is the portion of a webpage visible without scrolling.

Optimizing this area ensures users see content quickly, improving perceived performance.

How Critical CSS Works

The browser first loads inline critical CSS and renders visible content immediately.

Non-critical CSS is loaded later without blocking rendering.

This reduces render-blocking resources and improves First Contentful Paint (FCP).

Implementation Example

HTML
Inline critical CSS
<style>
  body { margin: 0; font-family: Arial; }
  header { background: #333; color: #fff; padding: 10px; }
</style>
HTML
Load remaining CSS asynchronously
<link rel="preload" href="styles.css" as="style" onload="this.rel='stylesheet'">

Tools to Generate Critical CSS

  • Critical (npm package)
  • PurgeCSS
  • Penthouse
  • Chrome DevTools Coverage

Benefits

  • Faster First Contentful Paint (FCP)
  • Improved Largest Contentful Paint (LCP)
  • Reduced render-blocking resources
  • Better user experience
  • Improved Core Web Vitals

Best Practices

  • Inline only essential CSS
  • Keep critical CSS small
  • Defer non-critical styles
  • Avoid duplication of styles
  • Test across different screen sizes

Common Mistakes

  • Inlining too much CSS
  • Ignoring mobile layouts
  • Not updating critical CSS after design changes
  • Blocking async CSS loading

Real-World Example

An e-commerce homepage can inline styles for header, hero section, and navigation.

Product grids and footer styles can be loaded later asynchronously.

Tools for Testing Critical CSS

  • Lighthouse
  • PageSpeed Insights
  • WebPageTest
  • Chrome DevTools

Conclusion

Critical CSS is a powerful technique for improving initial rendering performance.

By prioritizing above-the-fold styles, developers can significantly enhance perceived load speed.

When combined with other optimizations, it leads to faster and more efficient web applications.