Critical CSS: Inline Above-the-Fold Styles
Inline the CSS for the first screen, load the rest without blocking, and handle breakpoints, the 14 KB first round trip and caching trade-offs.
Related chapters
- Core Web Vitals in Production: LCP, INP and CLS
- The Cost of JavaScript: Bundle Size, Parsing and Hydration
- Critical Rendering Path Explained: DOM, CSSOM, Paint
- CSS Performance: Selectors, @import and Layout Cost
- HTTP Caching Explained: Cache-Control, ETag, CDN
- Lazy Loading Images and Code: Best Practices
- List Virtualization (Windowing) in React Explained
- Intersection Observer API Explained, with Examples
- How Mobile Browsers Work: Camera, Permissions, Quirks
- Frontend Performance Case Studies: Netflix, Airbnb, Flipkart
Keep learning
Overview
Phone · 640px Laptop · 800px Tall · 1350px reset, body, type other routes = 0 && rule.top Chapter 69 · critical css Inline the CSS the first screen needs. Load the rest later. A stylesheet link costs one more round trip before any pixel. Critical CSS removes that trip for the part of the page the reader sees first.
6. The markup, today
Inline paints the first screen from the HTML alone. preload fetches the full file at high priority without blocking render. onload flips it to a stylesheet once it lands. noscript keeps the page styled when JS is off. Then and now 2016: inline a loadCSS polyfill because few browsers supported preload HTTP/2 server push as a cacheable “inline”: removed from Chrome in 2022 103 Early Hints starts the CSS fetch before the HTML is ready
8. What you decide in production
Generate it Build the HTML, render it headless, keep the rules that match above the fold, inline them. build HTML headless render rules above fold Never hand-maintain it. Beasties (the maintained Critters fork) inlines at build from the HTML. critical / Penthouse render each template in a headless browser and keep the rules that match above the fold. Per template, per viewport Critical rules per template and viewport. The union is what ships. The home page and the product page have different folds. Extract at a phone and a desktop size and take the union. HTML plus critical CSS above 14 KB spills into a second round trip. HTML plus critical CSS under about 14 KB compressed. Past that, the inlined bytes cost the round trip they were meant to save. The cache trade Round trips spent on CSS per visit. The link costs one on the first visit only. Inline costs bytes every visit. visit 1 visit 2 visit 3 +1 trip Inlined bytes ride on every HTML response and are never cached on their own. A returning visitor with a warm app.css already paid zero trips. Inlining wins on cold, high-latency first visits. CSS-in-JS on the server Render on the server, collect styles from the components that rendered, emit one style tag. collect used rules <style> in head Collecting styles while rendering (styled-components, Emotion, vanilla-extract at build) is critical CSS by construction: only components that rendered emit rules. Fonts and backgrounds A background image is found only after CSS. Preloaded, it downloads alongside the CSS. bg found found late starts early Put @font-face in the critical block and preload the file. A hero background-image is discovered only after CSS applies, so preload it too or use an <img>. Content Security Policy What a strict CSP allows onload="…" inline handler <style nonce> + an external swap script Inline from a file, automatically critical.css in the repo, read by the server or build, inlined into every HTML response for that template. build / server include <style> in HTML Spend it on high-value pages Prioritize templates by traffic: landing pages first. When to skip it When to skip critical CSS total CSS under ~10 KB: inline it all warm cache + hashed link: already free Total CSS under ~10 KB: inline all of it. A logged-in SPA with a warm cache: a hashed, immutable link is already free.
1. The fold decides what is critical
Rules for anything above the line ship inside the HTML. Everything else waits. Viewport height = 0 && rule.top not on this screen HTML CSS 14 KB · first round trip of the CSS blocks first paint 2 trips 1 trip for the first screen A tall fold pulled the cards in. HTML plus CSS no longer fits the first window, so the HTML itself needs a second round trip. The first screen fits in one round trip. connect + ask 2nd trip app.css · preload not blocking app.css · async Main thread Unstyled paint First paint All styles Warm cache: the link costs nothing. This is why critical CSS matters most on the first visit. The HTML arrived, then the page sat blank for a whole round trip while app.css downloaded. The first screen painted styled from the HTML. The rest of the CSS landed before anyone scrolled to it. Nothing blocked, so the browser painted raw HTML. When the CSS landed, everything moved: a flash of unstyled content and a layout shift.
3. Three ways to ship the same CSS
One <link> Inline critical + preload All async Repeat visit (app.css cached) Filmstrip of what the reader sees from cache first paint HTML on every visit layout shift Blank screen Unstyled HTML: default fonts, a plain list Header and hero styled, the rest not yet needed Fully styled page
7. Two copies of the same rule
The hero rule lives inline and in app.css. If they disagree, the file wins when it loads, and the first screen jumps. Copies disagree Copies match app.css arrives The hero shrank and the cards below jumped up. The hero and cards are in place. The hero shrank by 48px when app.css applied. Everything below moved. Generate both copies from the same build so they cannot drift. Inline and file agree, so the swap is invisible. Site header Content hubs Recipe title Recipe image Share buttons Banner ad Related list Side ad phone · 1 column desktop · 2 columns
2. Every breakpoint has its own fold
A recipe page in two layouts. Faint lines mark common screen heights (568–1280px). Move the fold and take inventory: on desktop, the sidebar jumps above the fold. Common screen heights Critical components by layout critical on phone critical on desktop components to inline (union) Slow 3G Fast 3G Cable / Wi-Fi
4. The payoff shrinks as the network gets faster
First paint with one blocking <link> versus inlined critical CSS, on four networks. Uses your fold from section 1. one <link> inline critical + preload On slow, high-latency connections the saved round trip is a large share of the wait. On cable it is a few milliseconds. The case study measured a 30–40% faster first paint on throttled 3G and about 20% on a real phone: optimise for the people on the worst networks.
5. Many templates: shared critical + one per template
Header, navigation and type are above the fold everywhere. The hero of the home page is not on the search page. Inline only what each template shows. Critical CSS files One file for every page global + per template Inlined critical CSS per template. other templates Each HTML response carries global plus its own template. Every page stays well inside the first round trip.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.