# Loading CSS, JS & Resource Hints

> How you load stylesheets and scripts determines whether the browser can paint the page quickly. `defer`, `async`, `type="module"`, and resource hints let you take control of the loading timeline without changing a line of application logic.

Subject: HTML | Lesson 39 of 42 | 8 min read
Interactive lesson: https://codewithanimation.in/html/loading-css-js-and-hints
Previous: [Social Previews & Structured Data](https://codewithanimation.in/html/social-and-structured-data.md) | Next: [details & summary](https://codewithanimation.in/html/details-and-summary.md)

## What happens when the browser encounters CSS and JS

When the browser parses an HTML document and encounters `<link rel="stylesheet">`, it stops rendering and downloads the stylesheet. It must build the CSS Object Model (CSSOM) before it can calculate styles and paint anything. This is called render-blocking. A stylesheet in `<head>` blocks the entire first paint until it downloads and parses.

A plain `<script src="...">` is even more disruptive. It is both render-blocking and parser-blocking: the browser stops parsing the HTML entirely, downloads the script, executes it, and only then continues. This is because scripts may call `document.write()` which would add content to the document. The traditional workaround was to put all scripts just before `</body>` so the HTML was mostly parsed before the script blocked. Modern solutions are the `defer` and `async` attributes.

## Why the loading order affects Core Web Vitals

Largest Contentful Paint (LCP) measures how long it takes for the largest visible content element to appear. A render-blocking script in `<head>` delays LCP directly. An unpreloaded hero image delays LCP because the browser does not discover it until it parses the HTML. A Google Font loaded with a `<link>` in `<head>` blocks the render while the DNS resolves, the connection establishes, and the font file downloads.

The `defer` attribute on a script tag tells the browser: download this file in parallel while parsing continues, then execute it after the HTML is fully parsed, in document order. This is safe for almost every script that touches the DOM. The `async` attribute says: download in parallel, execute immediately when done -- in no guaranteed order. Use `async` only for independent scripts like analytics pixels that do not depend on each other or on the DOM being ready. `type="module"` scripts are automatically deferred.

## How to use defer, async, and resource hints

Place `defer` on every `<script>` that touches the DOM and has no dependency on execution order with other scripts. Place `async` on fully independent third-party scripts (analytics, ad tags) that should not delay the page even if the network is slow. Use `type="module"` for ES module scripts; they defer automatically and support `import` statements.

Use `<link rel="preload" href="critical.woff2" as="font" type="font/woff2" crossorigin>` to fetch fonts at top priority before the CSS even requests them. Always set the `as` attribute to match the resource type or the browser will ignore the hint. Use `<link rel="preconnect" href="https://fonts.googleapis.com">` to establish the connection to third-party origins early. Add `<link rel="dns-prefetch" href="https://fonts.googleapis.com">` as a fallback for browsers that do not support preconnect.

For Google Fonts specifically, the recommended pattern is: `<link rel="preconnect" href="https://fonts.googleapis.com">` then `<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>` before the `<link>` to the font CSS. This eliminates the DNS and TCP round-trips from the critical path.

## Common mistake: Using async on scripts that depend on each other

`async` scripts execute as soon as they download, in no guaranteed order. If script B depends on a library loaded by script A, but B downloads faster than A, B will execute first and throw a reference error. Only use `async` on genuinely independent scripts. If two scripts have any dependency relationship, use `defer` on both -- it preserves document order while still downloading in parallel.

## Tip: Preload the LCP image

The browser does not discover `<img>` tags (and therefore cannot start fetching images) until it parses that part of the HTML. For a hero image that is the LCP element, add `<link rel="preload" href="/hero.webp" as="image">` in `<head>`. This starts the fetch at the highest priority before the HTML body is parsed, which can cut LCP by hundreds of milliseconds on slow connections.

## Try it: Try it

Watch the load timeline for three script-loading strategies side by side.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/loading-css-js-and-hints

## Check yourself

### Question 1

Script A uses defer and Script B uses async. Both start downloading at the same time. Script B finishes downloading first. In what order do they execute?

- A. B executes immediately when downloaded. A executes after HTML parsing completes.
- B. Both execute in the order they appear in the HTML
- C. A executes first because defer has higher priority than async

Correct answer: A

`async` executes out-of-order, as soon as downloaded. `defer` waits until HTML parsing is complete and executes in document order. Only use `async` for scripts with no dependencies.

- Why not B: Only defer preserves document order. async executes as soon as the script finishes downloading, regardless of HTML position or other scripts.
- Why not C: defer does not have higher priority than async. defer delays execution until after HTML parsing. async executes immediately on download, often before defer scripts run.

### Question 2

Why must <link rel='preload'> always include the as attribute?

- A. Without it, the browser ignores the hint or applies the wrong priority and CORS mode
- B. as is optional; it only affects how the resource is displayed
- C. as tells the browser which element will consume the resource

Correct answer: A

The `as` attribute on `<link rel="preload">` is mandatory. It must match the type of resource being preloaded. Without it, the browser either ignores the hint or sends incorrect request headers.

- Why not B: The `as` attribute is required for preload to work correctly. It tells the browser the resource type so it can apply the right request headers, CORS mode, content security policy, and priority. Without it, the hint is treated as an error in most browsers.
- Why not C: The `as` attribute specifies the resource type (script, style, font, image, fetch) so the browser can apply the correct request settings. It does not reference a specific element.

### Question 3

A script with type='module' is automatically what?

- A. Deferred
- B. Async
- C. Render-blocking like a regular script

Correct answer: A

`type="module"` scripts are automatically deferred: they download in parallel and execute after HTML parsing, in document order. They also run in strict mode and are fetched with CORS.

- Why not B: Module scripts are deferred, not async. They wait until HTML parsing completes and execute in document order, just like `defer`.
- Why not C: Regular scripts without defer or async are render-blocking. Module scripts have the deferred behaviour built in and do not block rendering.

## Remember this

- Plain `<script>` is parser-blocking. Place scripts before `</body>`, or use `defer` or `async`.
- `defer`: parallel download, executes in order after HTML parsed. Use for most scripts.
- `async`: parallel download, executes immediately when done (out of order). Use only for independent scripts.
- `<link rel="preload" as="...">`: high-priority early fetch. The `as` attribute is required.
