Chapter 3: Tree Structures in HTML, CSS and JavaScript
A Complete Frontend Developer Textbook for Beginners (2023 Edition)

This is the textbook version of Lesson 3 of 100 from the Udemy video course: A Complete Frontend Developer Course for Beginners
← Chapter 2: JavaScript Console
Overview
This lesson covers the following HTML tags, CSS topics, and JavaScript commands for the first time:
HTML
CSS
JS
- document (Document Object Model)
- document.documentElement
- document.documentElement.firstChild
- document.documentElement.lastChild
- document.documentElement.tagName
Again, to get the most from my lecture, please create a new Codepen and code along with me!
Don’t just watch me code! 👀
Lecture
So far we have seen how tags can give our plain text more structure, and how useful this is for selection and manipulation with CSS and JavaScript.
Tags => StructureHTML Tree Structure
In this lesson, we explore how HTML can allow any kind and as many tags as we want.
To make selecting any one of them fast and easy, the creators of HTML came up with a very elegant way to organize them.
The brilliant idea is to allow tags inside of tags.
When we do this, we produce a data structure called a tree structure.
HTML: data structure = tree structureLike a real tree, a tree structure in HTML begins from the root tag which is appropriately named html:
<html></html>Then, inside this root tag, we have 2 and only 2 special tags:
The head tag:
<html><head></head></html>And the body tag:
<html><head></head><body></body></html>Written like this, it’s quite difficult to tell that the head and body tags are inside the html tag.
<html><head></head><body></body></html>So a best practice developed among coders. The convention is to indent them like this:
<html>
<head></head>
<body></body>
</html>Right away, we can clearly and easily tell that the head and body tags are inside the html tag:
<html>
<head></head>
<body></body>
</html>Just like a family tree, we can refer to the head and body tags as the children of the html tag:
<html>
<head></head>
<body></body>
</html>And the html tag as the parent of the head tag and body tag:
<html>
<head></head>
<body></body>
</html>We could even be more specific and refer to the head tag as the first child of the html tag:
<html>
<head></head>
<body></body>
</html>And the body tag as the last child of the html tag:
<html>
<head></head>
<body></body>
</html>Why is a tree structure better?
To answer this question, let’s rewrite our tags as a linear structure:
<html></html>
<head></head>
<body></body>Let’s say we want to select the body tag:
<html></html>
<head></head>
<body></body>CSS and JavaScript would need to search through each one by one until they find the body tag.
In Computer Science, we’d say this method takes linear time.
Linear Structure => Linear TimeOn the other hand, if these tags are written as a tree structure, we can just look for a tag that is a last child of the html tag:
<html>
<head></head>
<body></body>
</html>This means we can skip searching the head tag entirely:
<html>
<head></head>
<body></body>
</html>In Computer Science, we would say this method takes logarithmic time, which is much faster than linear time.
Tree Structure => Logarithmic TimeIn other words, by organizing our tags into a tree structure, we can avoid searching through every single one to find the one that we want.
The finished HTML code on Codepen:

CSS Tree Structure
A HTML tree structure is great because CSS code can understand it.
For example, we have learned that to select the body element, we can simply write:
body {}
To make the background green, we can write:
body {
background: green;
}There is another way we can use to select the body element. Since the body element is the last child of html, we can use this special selector:
:last-child {}
To prove that this works, let’s change the background to orange:
:last-child {
background: orange;
}There it is:
UI Window:[has orange background]Let’s say we want to select the head element. Again, we can do this by simply writing:
head {}
Or, you guessed it:
:first-child {}
The
:first-childand:last-childselectors are formally known as pseudo-classes which we will cover in detail in Unit 3.
What’s the difference between a head element and a body element?
The body element is where we show our content.
body => contentThe head element is where we describe the entire document, such as its title, author, date, etc.
head => documentBecause the head element has no content to show, CSS code here does not make sense and will not appear.
For example:
head {
background: blue;
}See, no changes:
UI Window:[background is still orange]The finished CSS code on Codepen:

JavaScript Tree Structure
Finally, let’s look at JavaScript.
Because we have arranged our HTML into a nice tree structure, JavaScript can follow this structure to build an exact model in the computer’s memory.
Real HTML => Virtual HTMLThis way, it’s much easier and faster to make changes to the HTML Tree.
In other words, an HTML document by itself is just a static text file:
HTML Document = Static TextBy converting it into a virtual model in the memory, the HTML Tree becomes more dynamic.
Virtual HTML => DynamicAs a matter of fact, this model is easily the most important object of all on the Frontend.
Thus, we give it a special name: the Document Object Model (or DOM for short).
Virtual HTML = DOMIn JavaScript, the DOM is represented by the command:
JS Window:documentInside of which is the entire HTML virtual tree.
document => DOMWithin the DOM, tags are referred to as elements.
HTML => tags
DOM => elementsFor example, we know that the HTML Tree begins with the root html element.
With JavaScript, we can get it with the command:
document.documentElementTo verify this, we can console.log() it like this:
console.log(document.documentElement)It’s a little messy as you can see because it’s giving us the entire Codepen HTML:
Console Window:<html>...</html>So let’s just ask for the tagName only like this:
console.log(document.documentElement.tagName)There it is:
Console Window:"HTML"We now know that document.documentElement gives us the root html element.
How would we get the head element?
Since the head element is its first child, we can just add .firstChild:
document.documentElement.firstChildTo verify this, let’s console.log() it again:
console.log(document.documentElement.firstChild)Again, it’s a little messy because it’s giving us Codepen’s entire head element.
Console Window:<head>...</head>So let’s just ask for the .tagName only like before:
console.log(document.documentElement.firstChild.tagName)There it is:
Console Window:"HEAD"How about the body element? Can you guess how to get it?
You got it. Just add .lastChild to document.documentElement instead:
document.documentElement.lastChildThis will give us Codepen’s entire body element.
Console Window:<body>...</body>So let’s also add .tagName like before:
document.documentElement.lastChild.tagNameTo verify this works, let’s console.log() it once more:
console.log(document.documentElement.lastChild.tagName)And there it is:
Console Window:"BODY"To review, inside document is the entire virtual HTML Tree. To access its children and its children’s children, and so on and so forth, we use a period (or dot):
.
This is referred to as the Dot Notation in JavaScript.
The finished JavaScript code on Codepen:

Codepen
The complete finished Codepen for this lesson:

















