HTML Under the Hood: How HTML Really Works Behind the Scenes


Hyper Text Markup Language
π Hyper Text
This just means text with links.
When you click on a link to go to another pagethatβs hypertext.
So HTML lets you connect pages together with <a> tags (links).
π Markup
Markup means tags that tell the browser how to structure and display content.
Example:
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
Youβre not writing the content alone youβre marking it up to say what it is: a heading, a paragraph, a link, etc.
π Language
HTML is a computer language, but not like Python or JavaScript.
It doesnβt have logic or calculations just structure.
Think of it like this:
HTML is the skeleton of a webpageit defines whatβs on the page and in what order.
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Hello!</h1>
<p>This is my first webpage.</p>
<a href="https://google.com/">Go to Google</a>
</body>
</html>
π§ What Happens When a Browser Renders HTML?
Imagine a browser (like Chrome, Firefox, Safari) as a super-fast reader. When you open a web page:
Browser gets HTML code from a server.
It reads the HTML line by line (top to bottom).
It builds a visual structure called the DOM (Document Object Model).
Then it draws what you see on the screen text, images, buttons, etc.
π§ WHAT IS THE DOM REALLY?
The DOM is an in-memory tree-like data structure that the browser creates from HTML.
Itβs not HTML, but a representation of it that JavaScript and the browser can interact with.
Every HTML element becomes a node in the tree.
The structure of your HTML defines the parent-child relationships between those nodes.
π What This Tree Tells Us
The root of every DOM is
Document
.Inside the
<html>
element, we have two main children:<head>
with a<title>
that contains text.<body>
with three elements:<h1>
: heading text<p>
: a paragraph<a>
: a link with text and an attribute (href
)
Each element node (like <h1>
) can have text nodes or child elements inside.
π·οΈ How Tags Work
HTML is made up of tags, like <p>
or <h1>
. Most tags come in pairs:
<p>This is a paragraph.</p>
<p>
= opening tag</p>
= closing tagContent goes in between.
There are also self-closing tags, like:
<img src="cat.jpg" alt="Cute cat">
<img>
inserts an image. It doesnβt wrap anything, so itβs self-closing.
π STEP-BY-STEP: HOW BROWSERS BUILD THE DOM
β 1. HTML Download
When you enter a URL:
The browser sends an HTTP request to the server.
The server responds with an HTML file.
The browser starts reading it before it's fully downloaded (this is called streaming parsing).
β 2. Tokenization
The browser breaks the raw HTML text into tokens:
Example HTML:
<p>Hello <b>world</b></p>
Becomes tokens like:
Start tag
<p>
Text node
Hello
Start tag
<b>
Text node
world
End tag
</b>
End tag
</p>
β 3. Lexing & Tree Construction
The tokens are converted into nodes and attached to the DOM tree:
Document
βββ <html>
βββ <body>
βββ <p>
βββ "Hello"
βββ <b>
βββ "world"
Each node has:
A type (element, text, comment, etc.)
Attributes
Children
A reference to its parent
β 4. Dealing with Bad HTML (HTML5 parser)
Browsers are forgiving. Even if you write messy HTML, they try to fix it. For example:
<p>Hello
<b>world
Will be interpreted and closed properly in the DOM as if you had written the full:
<p>Hello <b>world</b></p>
Browsers have error recovery logic based on the HTML5 spec.
β 5. Script Execution May Pause DOM Building
When the parser encounters a <script>
tag without async
or defer
, it:
Pauses DOM construction.
Runs the script (because it might modify the DOM).
Resumes parsing after the script runs.
Thatβs why putting <script>
at the bottom of the page or using defer
is good for performance.
β 6. Final DOM Tree Built
Once parsing is complete, the browser has a full DOM Tree in memory. Example simplified tree:
Document
βββ html
βββ head
β βββ title β "My Page"
βββ body
βββ h1 β "Welcome"
βββ p β "Hello World"
This is what JavaScript talks to when you do things like:
document.querySelector("h1").textContent = "Changed!";
You're not modifying raw HTML youβre modifying the DOM structure in memory.
Thanks for reading π
π€ Let's Connect!
I'm Aman, a freelance web developer.
I love building clean, functional websites and apps.
I'm open to work, collaborations, or just a good tech chat.
π¬ Reach out or follow me:
Subscribe to my newsletter
Read articles from Aman Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aman Raj
Aman Raj
Iβm a full-stack developer and open-source contributor pursuing B.Tech in CSE at SKIT, Jaipur. I specialize in building scalable web apps and AI-driven tools. With internship experience and a strong portfolio, Iβm actively open to freelance projects and remote job opportunities. Letβs build something impactful!