Intro
When you open a website, the page appears on the screen in a fraction of a second. It can feel almost instant, but the browser performs a surprisingly large amount of work before you see the final result.
It must request files from a server, read HTML, understand CSS, execute JavaScript, calculate the position and size of every visible element, and finally draw pixels onto the screen.
Understanding this process makes it much easier to understand how modern websites work and why some changes to HTML, CSS, or JavaScript can affect performance.
In this article, we will follow the browser rendering process step by step.
1. It Starts With a URL
Imagine that you enter this address into your browser:
https://example.com
Before the browser can display anything, it needs to find the server that hosts the website.
At a high level, several things happen:
- The browser reads the URL.
- DNS resolves the domain name to an IP address.
- The browser connects to the server.
- An HTTP or HTTPS request is sent.
- The server returns a response.
The response often contains an HTML document.
A simplified request might look like this:
GET / HTTP/1.1
Host: example.com
The server may respond with something like:
HTTP/1.1 200 OK
Content-Type: text/html
After the response headers comes the actual HTML document.
At this point, the browser still does not have a visible web page. It only has text that it needs to interpret.
2. The Browser Receives HTML
Suppose the server returns this HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Browser Rendering</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<main>
<h1>Hello Browser</h1>
<p>This page was created from HTML, CSS and JavaScript.</p>
<button id="button">Change text</button>
</main>
<script src="script.js"></script>
</body>
</html>
To us, this is a document made of tags.
To the browser, it is a stream of characters that must be parsed and converted into a structured representation.
That structure is called the DOM.
3. HTML Becomes the DOM
DOM stands for Document Object Model.
The browser reads the HTML from top to bottom and creates objects representing the elements it finds.
From the HTML above, the browser creates a tree that can be simplified like this:
Document
└── html
├── head
│ ├── meta
│ ├── title
│ └── link
└── body
├── main
│ ├── h1
│ ├── p
│ └── button
└── script
This is called a tree because elements can contain other elements.
For example:
<main>
<h1>Hello Browser</h1>
<p>This page was created from HTML, CSS and JavaScript.</p>
</main>
The <main> element is the parent, while <h1> and <p> are its children.
The browser represents those relationships in the DOM.
The important idea is:
HTML is the source code. The DOM is the browser’s structured representation of that HTML.
JavaScript does not normally work directly with the original HTML file. It interacts with the DOM created by the browser.
For example:
const heading = document.querySelector('h1');
console.log(heading.textContent);
Here, document.querySelector() searches the DOM and returns the first <h1> element.
The JavaScript code can then read or modify that object.
4. The Browser Discovers Other Resources
While parsing HTML, the browser may find references to additional files.
For example:
<link rel="stylesheet" href="style.css">
This tells the browser that it needs a CSS stylesheet.
The browser may also find:
<script src="script.js"></script>
This tells it to request a JavaScript file.
Other resources can include:
<img src="hero.jpg" alt="Hero image">
or:
background-image: url("background.jpg");
A real website may therefore require many separate requests.
The browser does not simply download the entire website as one file. It discovers resources while processing the documents it has already received.
5. CSS Becomes the CSSOM
Now imagine that style.css contains:
body {
font-family: Arial, sans-serif;
background: #f5f5f5;
}
main {
max-width: 760px;
margin: 80px auto;
}
h1 {
font-size: 48px;
}
button {
padding: 12px 20px;
}
Just as HTML must be parsed, CSS must also be parsed.
The browser converts CSS into another tree-like structure called the CSSOM.
CSSOM stands for:
CSS Object Model
The CSSOM contains the CSS rules that the browser needs in order to determine how elements should look.
For example, when the browser encounters:
h1 {
font-size: 48px;
}
it understands that this rule applies to matching <h1> elements in the DOM.
Similarly:
main {
max-width: 760px;
margin: 80px auto;
}
affects the <main> element.
The browser must combine all relevant CSS rules, including inheritance, specificity, default browser styles, and the cascade, to determine the final computed style of each element.
6. DOM + CSSOM = Render Tree
At this point the browser has two important structures:
HTML → DOM
CSS → CSSOM
The browser combines information from both to create the render tree.
The render tree contains the elements that must actually be rendered on the page, together with their calculated styles.
Conceptually:
DOM + CSSOM → Render Tree
This distinction is important because not every DOM element necessarily appears in the render tree.
Consider this HTML:
<p class="message">Hello!</p>
and this CSS:
.message {
display: none;
}
The paragraph exists in the DOM.
JavaScript can still find it:
const message = document.querySelector('.message');
console.log(message);
However, because display: none removes the element from visual layout, it does not generate a visible box in the render tree.
This is one reason why the DOM and the rendered page are not exactly the same thing.
7. The Browser Calculates Layout
Once the render tree exists, the browser knows what should appear and which styles should apply.
But it still does not know the exact position of everything on the screen.
That is the job of the layout stage.
During layout, the browser calculates information such as:
- width
- height
- position
- margins
- padding
- relationships between elements
Consider this CSS:
main {
max-width: 760px;
margin: 80px auto;
padding: 24px;
}
The browser must determine the actual width of <main> based on the viewport.
If the browser window is 1400 pixels wide, the element may be 760 pixels wide because of:
max-width: 760px;
The horizontal value:
margin: 80px auto;
tells the browser to automatically calculate the left and right margins, which centers the element.
The browser must perform these calculations for every element that participates in the page layout.
8. Responsive CSS Can Change the Layout
Layout is not fixed forever.
The browser may calculate a different layout when the viewport changes.
For example:
main {
max-width: 760px;
margin: 80px auto;
padding: 24px;
}
@media (max-width: 600px) {
main {
margin: 32px auto;
padding: 16px;
}
}
On a large screen, the first set of values is used.
On a viewport that is 600 pixels wide or smaller, the media query becomes active.
The browser then recalculates the relevant styles and layout.
This is one of the core mechanisms behind responsive web design.
9. Paint Turns Layout Into Pixels
After layout, the browser knows where the visible elements belong.
The next stage is paint.
During painting, the browser draws the visual parts of the page.
This can include:
- text
- colors
- borders
- backgrounds
- shadows
- images
- icons
For example:
button {
color: white;
background: black;
border: 0;
border-radius: 8px;
padding: 12px 20px;
}
The browser already knows the button’s position and dimensions from layout.
During paint, it draws the black background, white text, rounded corners, and other visual details.
A useful simplified sequence is:
HTML
↓
DOM
CSS
↓
CSSOM
DOM + CSSOM
↓
Render Tree
↓
Layout
↓
Paint
This is often called part of the browser’s rendering pipeline.
10. Modern Browsers Also Use Compositing
Modern pages can be visually complex.
Instead of painting the entire page as a single flat image, browsers can place parts of the page into separate layers.
Those layers can then be combined during a stage called compositing.
A simplified pipeline can therefore be written as:
DOM
+
CSSOM
↓
Render Tree
↓
Layout
↓
Paint
↓
Composite
Compositing is especially useful for things such as animations, transforms, fixed elements, and other visual effects.
For example:
.card {
transform: translateX(20px);
}
In many situations, transforms can be handled efficiently because the browser can move an already-painted layer instead of recalculating the entire document layout.
The exact implementation is complex and differs between browser engines, but the important idea is that browsers try to minimize expensive rendering work whenever possible.
11. Where Does JavaScript Fit Into the Process?
JavaScript can interact with the page after the DOM has been created.
Consider this file:
const button = document.querySelector('#button');
const heading = document.querySelector('h1');
button.addEventListener('click', function () {
heading.textContent = 'The DOM has changed!';
});
First, JavaScript finds two elements in the DOM:
const button = document.querySelector('#button');
const heading = document.querySelector('h1');
Then it listens for a click:
button.addEventListener('click', function () {
heading.textContent = 'The DOM has changed!';
});
When the user clicks the button, JavaScript changes the text stored in the <h1> DOM node.
The browser then needs to update what appears on the screen.
In this example, the browser may need to repaint the heading. If the new text changes the size of the element, it may also need to perform layout again.
So JavaScript can cause new work in the rendering pipeline.
12. JavaScript Can Change CSS Too
JavaScript can also change styles.
For example:
const box = document.querySelector('.box');
box.style.width = '400px';
Changing the width of an element may affect the position of other elements.
Because of that, the browser may need to perform layout again.
A change such as:
box.style.backgroundColor = 'tomato';
usually does not change the dimensions of the element.
The browser may therefore only need to repaint it.
This leads to an important performance concept:
Not every visual change has the same cost.
13. Reflow and Repaint
You may encounter the terms reflow and repaint when learning browser performance.
A layout recalculation is often called reflow.
For example, changing:
element.style.width = '500px';
can affect the dimensions and position of elements.
The browser may need to recalculate layout.
Changing only a visual property:
element.style.backgroundColor = 'blue';
typically does not change geometry.
The browser may only need to paint the affected area again.
A simplified comparison is:
| Change | Possible browser work |
|---|---|
width | Layout + Paint |
height | Layout + Paint |
margin | Layout + Paint |
font-size | Layout + Paint |
background-color | Paint |
color | Paint |
transform | Often Composite |
opacity | Often Composite |
The exact behavior can vary, but this model is useful when thinking about performance.
14. JavaScript Can Block HTML Parsing
Script placement also matters.
Consider this:
<head>
<script src="script.js"></script>
</head>
When the browser parser encounters a traditional script like this, it may need to pause HTML parsing while the JavaScript file is downloaded and executed.
Why?
Because JavaScript can modify the document.
For example, a script could contain:
document.write('<p>Added by JavaScript</p>');
The browser must account for the possibility that the script will change the document being parsed.
This is one reason why scripts were traditionally placed near the end of <body>:
<body>
<main>
<h1>Hello Browser</h1>
</main>
<script src="script.js"></script>
</body>
By the time the browser reaches the script, most of the HTML has already been parsed.
15. The defer Attribute
Modern HTML gives us a better option for many external scripts.
We can write:
<script src="script.js" defer></script>
With defer, the browser can download the JavaScript file while continuing to parse HTML.
The deferred script executes after HTML parsing has completed.
This makes it useful for scripts that depend on DOM elements.
For example:
<head>
<script src="script.js" defer></script>
</head>
can work safely with code like:
const button = document.querySelector('#button');
because the script will execute after the HTML document has been parsed.
For many regular website scripts, defer is a good default choice.
16. What About async?
There is another attribute:
<script src="analytics.js" async></script>
The browser downloads an async script without waiting for HTML parsing to finish.
However, once the file is ready, it executes as soon as possible.
That means the execution order is not guaranteed relative to other async scripts.
async is useful when a script is independent of the rest of the page.
Typical examples include some analytics or third-party scripts.
A simplified comparison is:
| Script | Download | Execution |
|---|---|---|
| Normal | Parsing may pause | Immediately when encountered |
defer | Parallel with HTML parsing | After HTML parsing |
async | Parallel with HTML parsing | As soon as ready |
Understanding this helps explain why script loading strategy can affect page performance.
17. CSS Can Also Delay Rendering
CSS plays a special role in rendering.
The browser needs style information before it can correctly render the page.
Suppose the HTML contains:
<h1>Hello Browser</h1>
but the external stylesheet contains:
h1 {
font-size: 64px;
color: darkblue;
}
If the browser rendered the heading before loading the CSS, users might briefly see an incorrectly styled version and then see it change.
To avoid that, external CSS is generally considered render-blocking.
The browser typically waits until it has enough CSS information to produce the correct initial render.
This is one reason why excessively large stylesheets can affect page loading performance.
18. The Critical Rendering Path
The steps the browser must complete before displaying useful content are often described as the critical rendering path.
A simplified version is:
Request HTML
↓
Parse HTML
↓
Create DOM
↓
Request CSS
↓
Create CSSOM
↓
Create Render Tree
↓
Layout
↓
Paint
↓
Display Page
JavaScript can interact with several parts of this process and may delay or trigger additional rendering work.
When developers optimize website performance, they often try to reduce unnecessary work along this path.
Examples include:
- reducing unnecessary CSS
- loading scripts efficiently
- compressing assets
- reducing network requests
- optimizing images
- avoiding unnecessary layout changes
- loading important resources first
19. A Small Complete Example
Consider this HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rendering Example</title>
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
</head>
<body>
<main class="card">
<h1>Browser Rendering</h1>
<p>
HTML creates the structure, CSS creates the presentation,
and JavaScript adds behavior.
</p>
<button id="button">Change heading</button>
</main>
</body>
</html>
When the browser receives this file, it begins building the DOM.
It also discovers:
<link rel="stylesheet" href="style.css">
and requests the CSS file.
It discovers:
<script src="script.js" defer></script>
and can download the JavaScript in parallel while continuing to parse the document.
Now imagine the stylesheet contains:
body {
margin: 0;
font-family: Arial, sans-serif;
background: #f4f4f4;
}
.card {
max-width: 760px;
margin: 80px auto;
padding: 32px;
background: white;
border-radius: 16px;
}
h1 {
margin-top: 0;
}
The browser parses these rules and constructs the CSSOM.
The DOM and CSSOM are then used to create the render tree.
During layout, the browser determines the size and position of .card.
During paint, it draws the background, text, card, and rounded corners.
Finally, this JavaScript runs:
const button = document.querySelector('#button');
const heading = document.querySelector('h1');
button.addEventListener('click', function () {
heading.textContent = 'JavaScript Updated the DOM';
});
When the button is clicked, JavaScript modifies the DOM.
The browser detects that the visible content has changed and updates the rendered result.
This small example already demonstrates the relationship between all three core web technologies:
HTML → Structure
CSS → Presentation
JavaScript → Behavior
But the browser is the system that connects them.
20. The Browser Is Constantly Updating the Page
It is easy to imagine rendering as something that happens once when a page loads.
In reality, modern websites can trigger rendering work continuously.
For example:
window.addEventListener('resize', function () {
console.log(window.innerWidth);
});
When the browser window changes size, responsive CSS may change.
The browser may need to calculate layout again.
Animations can also trigger updates many times per second.
For example:
function animate() {
requestAnimationFrame(animate);
// Update something on the page.
}
requestAnimationFrame(animate);
requestAnimationFrame() allows JavaScript to schedule visual updates in coordination with the browser’s rendering cycle.
This is generally better than blindly updating visual properties as quickly as possible because the browser can coordinate the work with screen refreshes.
21. Why This Matters for Developers
You do not need to understand every internal detail of a browser engine to build websites.
But understanding the rendering pipeline explains many everyday web development problems.
For example, it helps explain:
Why JavaScript sometimes cannot find an element
If JavaScript runs before the element has been parsed into the DOM:
const button = document.querySelector('#button');
may return null.
Using defer can solve this in many situations.
Why large CSS files can slow initial rendering
The browser needs CSS information before it can correctly construct the visual representation of the page.
Why changing layout properties can be expensive
Changing width, height, margins, or font sizes may force the browser to calculate layout again.
Why transform is popular for animations
Transforms can often be handled later in the rendering pipeline and may avoid expensive layout calculations.
Why the DOM is so important
The DOM is the bridge between your HTML document and JavaScript.
Without understanding the DOM, JavaScript manipulation can feel mysterious.
22. A Useful Mental Model
You can remember the entire process with one simplified mental model.
HTML creates structure
<h1>Hello</h1>
The browser converts it into the DOM.
CSS describes presentation
h1 {
color: blue;
}
The browser converts CSS into the CSSOM.
The browser combines them
DOM + CSSOM → Render Tree
The browser calculates geometry
Render Tree → Layout
The browser draws the result
Layout → Paint → Composite
JavaScript can change the process
document.querySelector('h1').textContent = 'Updated';
JavaScript modifies the DOM or styles, which can cause the browser to perform some rendering stages again.
Final Rendering Pipeline
The complete simplified process looks like this:
URL
↓
HTTP Request
↓
HTML
↓
DOM
CSS
↓
CSSOM
↓
DOM + CSSOM
↓
Render Tree
↓
Layout
↓
Paint
↓
Composite
↓
Pixels on the Screen
JavaScript can interact with the DOM and CSS throughout the life of the page:
JavaScript
↓
DOM / Styles
↓
Possible Layout
↓
Possible Paint
↓
Possible Composite
Conclusion
A browser does much more than simply open an HTML file.
It downloads resources, parses HTML into the DOM, parses CSS into the CSSOM, combines those structures into a render tree, calculates layout, paints visual elements, and composites layers into the final image you see on the screen.
JavaScript can then modify the DOM and styles, causing parts of that rendering process to happen again.
Understanding this pipeline gives you a much clearer picture of how HTML, CSS, and JavaScript work together.
Instead of seeing them as three separate technologies, you can see them as inputs into one larger system: the browser rendering engine.
Once you understand that system, concepts such as DOM manipulation, CSS performance, responsive layouts, script loading, animations, and page-speed optimization become much easier to reason about.