# Render-Blocking Resources

> How CSS and synchronous JavaScript pause the Critical Rendering Path, delaying the first paint on the screen.

Subject: Internet | Lesson 31 of 35 | 5 min read
Interactive lesson: https://codewithanimation.in/internet/render-blocking-resources
Previous: [The Compositing Phase](https://codewithanimation.in/internet/compositing-phase.md) | Next: [Script Loading Strategies](https://codewithanimation.in/internet/script-loading-strategies.md)

## What it is

Render-Blocking Resources are files-primarily CSS and synchronous JavaScript-that the browser must completely download, parse, and execute before it can render any content to the screen.

They act as roadblocks in the Critical Rendering Path, preventing the browser from showing the user a finished page.

## Why it matters

Render-blocking resources directly impact First Contentful Paint (FCP). When a browser encounters these resources, rendering is paused, leaving the user staring at a blank screen (the infamous 'white screen of death').

High FCP times lead to severe user bounce rates, poor user experience, and negative SEO rankings.

## How it works

When the browser parses HTML top-to-bottom and encounters a `<link rel="stylesheet">`, it pauses building the Render Tree because CSS fundamentally alters how elements look. It must wait for the full CSS Object Model (CSSOM) to be ready.

When it encounters a `<script>` tag, HTML parsing completely stops. The browser conservatively assumes the script might manipulate the DOM structure (e.g., using `document.write`), so it halts everything until the script is downloaded and executed.

## Common mistake: The 'Everything in the Head' Mistake

A massive common mistake is putting huge unoptimized JavaScript bundles or monolithic application-wide CSS stylesheets in the `<head>` without using `defer` or `async`.

This forces users to download and process the entire application logic and all styling rules before they are allowed to see a single pixel of the UI.

## Tip: Pro tip

**Inline critical CSS, defer the rest.**

Identify the CSS strictly necessary for the 'above-the-fold' content (what the user sees immediately without scrolling) and inline it directly in a `<style>` tag in the `<head>`. Load the remaining CSS asynchronously.

Always add the `defer` attribute to your `<script>` tags to ensure they download in the background without blocking DOM construction.

## Try it: Try it

Experience the 'tollbooth'. Watch how a large synchronous script or stylesheet halts the HTML cars from reaching the screen, and see what happens when you 'defer' the script.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/render-blocking-resources

## Check yourself

### Question 1

Why does synchronous JavaScript in the `<head>` block DOM construction?

- A. JavaScript files are usually larger than HTML files.
- B. The CSSOM cannot be built while JavaScript is downloading.
- C. The script might manipulate the DOM, so the browser must safely halt parsing until the script finishes executing.
- D. Browsers can only download one file at a time.

Correct answer: C

Browsers must assume any synchronous script might fundamentally alter the DOM tree being built. Therefore, it pauses HTML parsing completely until the script is downloaded and executed.

- Why not A: File size affects download time, but it's the synchronous execution that blocks parsing.
- Why not B: CSS downloads can happen in parallel; JS execution blocks the DOM.
- Why not D: Modern browsers download multiple files in parallel.

### Question 2

Which attribute should you add to a `<script>` tag so it downloads in the background and executes after the document has been fully parsed?

- A. async
- B. defer
- C. lazy
- D. non-blocking

Correct answer: B

The 'defer' attribute tells the browser to download the script in parallel but wait to execute it until HTML parsing is 100% complete, preserving DOM construction speed.

- Why not A: async executes immediately when ready, interrupting HTML parsing.
- Why not C: lazy is used for loading='lazy' on media like images, not scripts.
- Why not D: This is a concept, not a valid HTML attribute.

### Question 3

What is the recommended strategy for handling CSS to achieve the fastest First Contentful Paint (FCP)?

- A. Put all your CSS at the bottom of the `<body>`.
- B. Combine all CSS into one massive file to reduce HTTP requests.
- C. Use JavaScript to dynamically inject all styles.
- D. Inline the critical CSS in the `<head>` and load the rest asynchronously.

Correct answer: D

By inlining only the CSS needed for the initial viewport (critical CSS), you allow the browser to immediately paint the visible part of the page without waiting for external network requests.

- Why not A: This causes a Flash of Unstyled Content (FOUC).
- Why not B: A massive file takes longer to download and parse, blocking rendering longer.
- Why not C: This makes CSS dependent on JS downloading, heavily delaying the render.

## Remember this

- CSS blocks rendering. JS blocks parsing AND rendering.
- The browser must wait for synchronous scripts to execute before building the DOM.
- Use 'defer' on scripts to move them out of the critical rendering path.
