How do you optimize CSS for performance?
Answer
CSS optimization for performance: Critical CSS: extract above-the-fold CSS and inline it in <style> tags — eliminates render-blocking CSS for initial paint. Tools: Critical, Penthouse, Next.js does this automatically. Reduce CSS size: remove unused CSS with PurgeCSS or Tailwind CSS's JIT (only includes used utility classes). Avoid complex selectors: deeply nested selectors are slower to parse. BEM avoids deep nesting. Avoid layout-triggering properties in animations: animate only transform and opacity — they run on the GPU compositor without triggering layout or paint. Animating width, height, top, left triggers expensive layout. Contain: contain: layout or contain: strict limits the scope of layout recalculation to the contained element. will-change: will-change: transform promotes an element to its own compositor layer — use sparingly (memory cost). @layer: organize CSS with explicit cascade layers to avoid specificity battles and unnecessary overrides. CSS modules / scoped CSS: scoped styles prevent style leakage and enable unused CSS removal. Minimize specificity and nesting in production CSS.
Previous
What is requestIdleCallback and how is it used for performance?
Next
What is cumulative layout shift and how do you debug it?
More Web Performance Questions
View all →- Intermediate How do you optimize React applications for performance?
- Intermediate What is the browser performance API and how do you measure performance?
- Intermediate What is layout thrashing and how do you avoid it?
- Intermediate What is resource prioritization in browsers?
- Intermediate How does SSR (Server-Side Rendering) improve performance?