Skip to content

Critical CSS

Critical CSS Concepts

Critical CSS is the minimal subset of styles required to render the above-the-fold content of a webpage—the portion visible without scrolling. By isolating and inlining this critical CSS, developers reduce render-blocking styles, directly improving First Contentful Paint (FCP) and Largest Contentful Paint (LCP), two key metrics in Core Web Vitals. Render-blocking styles delay the browser's ability to render the page, leading to slower perceived performance and lower LCP scores.


Render-Blocking Styles and Their Impact

When a browser parses a CSS file, it must wait for the entire file to load before rendering the page. This creates a render-blocking delay, which can significantly impact FCP and LCP. For example: - A 1MB CSS file can add ~838ms of delay on a 10Mbps connection. - Non-critical styles (e.g., for below-the-fold content) exacerbate this issue.

Critical CSS addresses this by ensuring only the essential styles for the visible content are loaded first, allowing the browser to render the page as quickly as possible.


Identifying Critical CSS

To implement critical CSS, you must first determine which styles are required for the above-the-fold content. Techniques include: - Manual analysis: Inspect the DOM and identify elements that must be styled for the visible portion. - Automated tools: Use tools like Lighthouse, WebPageTest, or Critical to analyze and extract critical CSS.

Example:

# Generate critical CSS using the 'critical' tool
npx critical https://example.com --out critical.css


Implementation Techniques

  1. Inline Critical CSS
    Embed the critical CSS directly in the <head> of your HTML to avoid network requests:

    <style>
      /* Critical CSS for above-the-fold content */
      body { font-family: sans-serif; }
      .header { background: #fff; }
    </style>
    

  2. Load Non-Critical CSS Asynchronously
    Use async or defer attributes to load non-critical styles after the initial render:

    <link rel="stylesheet" href="non-critical.css" media="print" onload="this.media='all'">
    

  3. Use Media Queries to Hide Non-Critical Styles
    Load full stylesheets with media="print" and switch to media="all" once the page is rendered:

    <link rel="stylesheet" href="styles.css" media="print">
    


Trade-offs and Best Practices

  • Pros: Faster FCP/LCP, improved user experience, better Core Web Vitals.
  • Cons: Increased initial payload size, potential complexity in managing critical vs. non-critical styles.

Best practices: - Keep critical CSS under 25KB to avoid performance penalties. - Use server-side rendering (SSR) or static site generators (e.g., Next.js) to inject critical CSS dynamically. - Combine critical CSS with other optimizations like image compression and code splitting for holistic performance gains.


Key takeaways

  • Critical CSS is the minimal styles required to render above-the-fold content, reducing render-blocking delays.
  • Render-blocking styles directly impact FCP and LCP, making critical CSS a cornerstone of performance optimization.
  • Use tools like Lighthouse or Critical to identify and extract critical CSS, then inline it while loading non-critical styles asynchronously.
  • Balance critical CSS size (ideally <25KB) with other optimizations for maximum performance gains.