Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Performance – Critical C | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 38 of 50

·css

CSS Performance – Critical CSS & Unused CSS

9 mins

Course progress76%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `rel="preload"` + `onload` trick loads the full stylesheet **asynchronously**.
  • Tools like **PurgeCSS** strip unused selectors (especially useful with frameworks).
  • Keep the **selector chain short**; deep nesting hurts performance.

Performance is the difference between a good and a great website. Two common bottlenecks are large, unused CSS and render‑blocking CSS.

Critical CSS

  • The smallest set of CSS required to paint above‑the‑fold content.
  • Inline it directly in the <head> so the browser can render immediately.
  • Load the rest of the stylesheet asynchronously.

Critical‑CSS Inline + Preload Pattern

<head>
  <style>
    /* Critical rules – hero, navigation, primary typography */
    .hero{background:#222;color:#fff;padding:4rem;}
    .nav{display:flex;gap:1rem;}
  </style>
  <link rel="preload" href="/css/main.css" as="style" onload="this.rel='stylesheet'">
  <noscript><link rel="stylesheet" href="/css/main.css"></noscript>
</head>

Removing Unused CSS

  • Static analysis tools: PurgeCSS, UnCSS, Unused‑CSS.
  • For frameworks (Tailwind, Bootstrap) configure purge in postcss.config.js to strip classes you never use.

Example – Tailwind Purge

module.exports = {
  content: ['./src/**/*.html', './src/**/*.js'],
  safelist: ['bg-red-500','text-center'], // keep if generated dynamically
};

Runtime CSS Optimisation

  • Set font-display:swap on @font-face to avoid FOIT (Flash of invisible text).
  • Use contain to limit re‑paint scope (see Lesson 48).

Measuring Success

  • Lighthouse > 90 ms TTI (Time to Interactive) is a good target.
  • WebPageTest – look at First Contentful Paint and Speed Index.
  • Chrome DevTools > Network – verify that the CSS file is cached and has a small payload.

Pro Tip – Split Vendor CSS

If you use a library (e.g., Bootstrap), keep a custom build that only includes the components you need. This dramatically reduces file size.

Accessibility Angle

Avoid delaying essential styles (like focus outlines) – they must be present before the user can interact.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS for EmailContinue to lesson 39CSS Architecture – BEM, OOCSS, SMACSS, ITCSSBack to CSS Basics to Advanced curriculum