avatarThon Ly

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

13678

Abstract

-built_in">background</span> <span class="hljs-built_in">is</span> still orange]</pre></div><p id="b0bf">The finished CSS code on <a href="https://codepen.io/thonly/pen/XWpEKEa?editors=1111">Codepen</a>:</p><figure id="8a50"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*XB6zNB_dE4FrHt-CPqm4YA.png"><figcaption><a href="https://codepen.io/thonly/pen/XWpEKEa?editors=1111">https://codepen.io/thonly/pen/XWpEKEa?editors=1111</a></figcaption></figure><h2 id="3d29">JavaScript Tree Structure</h2><p id="d72f">Finally, let’s look at JavaScript.</p><p id="ab95">Because we have arranged our HTML into a nice tree structure, JavaScript can follow this structure to build an <b><i>exact model</i></b> in the computer’s memory.</p><div id="3a9d"><pre><span class="hljs-built_in">Real</span> <span class="hljs-built_in">HTML</span> => Virtual <span class="hljs-built_in">HTML</span></pre></div><p id="3801">This way, it’s much <i>easier</i> and <i>faster</i> to make changes to the HTML Tree.</p><p id="52eb">In other words, an HTML document by itself is just a <b>static text file</b>:</p><div id="6ab9"><pre><span class="hljs-built_in">HTML</span> Document = Static <span class="hljs-built_in">Text</span></pre></div><p id="2068">By converting it into a <b><i>virtual</i> model</b> in the memory, the HTML Tree becomes more <b>dynamic</b>.</p><div id="e0e1"><pre><span class="hljs-keyword">Virtual</span> HTML => <span class="hljs-keyword">Dynamic</span></pre></div><p id="183e">As a matter of fact, this model is easily the <i>most important object</i> of all on the Frontend.</p><p id="3534">Thus, we give it a special name: the <b>Document Object Model</b> (or <b>DOM</b> for short).</p><div id="5711"><pre>Virtual HTML <span class="hljs-operator">=</span> DOM</pre></div><p id="5c09">In JavaScript, the DOM is represented by the command:</p><div id="9d95"><pre>JS <span class="hljs-built_in">Window</span>:</pre></div><div id="b58b"><pre><span class="hljs-built_in">document</span></pre></div><p id="f1d8">Inside of which is the entire HTML <i>virtual</i> tree.</p><div id="7647"><pre><span class="hljs-attribute">document</span> <span class="hljs-operator">=</span>> DOM</pre></div><p id="d940">Within the DOM, <b>tags</b> are referred to as <b><i>elements</i></b>.</p><div id="c93a"><pre><span class="hljs-attribute">HTML</span> <span class="hljs-operator">=</span>> tags <span class="hljs-attribute">DOM</span> <span class="hljs-operator">=</span>> elements</pre></div><p id="654f">For example, we know that the HTML Tree begins with the root <code><b>html</b></code> element.</p><p id="fdff">With JavaScript, we can get it with the command:</p><div id="5487"><pre><span class="hljs-built_in">document</span>.documentElement</pre></div><p id="ac9c">To verify this, we can <code>console.log()</code> it like this:</p><div id="ae90"><pre><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(document.documentElement)</pre></div><p id="2f71">It’s a little messy as you can see because it’s giving us the entire Codepen HTML:</p><div id="1503"><pre><span class="hljs-built_in">Console</span> Window:</pre></div><div id="c05a"><pre><span class="hljs-tag"><<span class="hljs-name">html</span>></span>...<span class="hljs-tag"></<span class="hljs-name">html</span>></span></pre></div><p id="82b6">So let’s just ask for the <code>tagName</code> only like this:</p><div id="e123"><pre><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(document.documentElement.tagName)</pre></div><p id="fd2f">There it is:</p><div id="dd20"><pre><span class="hljs-built_in">Console</span> Window:</pre></div><div id="5f21"><pre><span class="hljs-string">"HTML"</span></pre></div><p id="36c6">We now know that <code>document.documentElement</code> gives us the root <code><b>html</b></code> element.</p><p id="284b"><b>How would we get the <code>head</code> element?</b></p><p id="9287">Since the <code><b>head</b></code> element is its <b><i>first child</i></b>, we can just add <code>.firstChild</code>:</p><div id="60e2"><pre><span class="hljs-built_in">document</span>.documentElement.firstChild</pre></div><p id="6a94">To verify this, let’s <code>console.log()</code> it again:</p><div id="8b0d"><pre><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(document.documentElement.firstChild)</pre></div><p id="04d6">Again, it’s a little messy because it’s giving us Codepen’s entire <code><b>head</b></code> element.</p><div id="800c"><pre><span class="hljs-built_in">Console</span> Window:</pre></div><div id="a0e8"><pre><span class="hljs-tag"><<span class="hljs-name">head</span>></span>...<span class="hljs-tag"></<span class="hljs-name">head</span>></span></pre></div><p id="1c1d">So let’s just ask for the <code>.tagName</code> only like before:</p><div id="9afd"><pre>console<span class="hljs-selector-class">.log</span>(document<span class="hljs-selector-class">.documentElement</span><span class="hljs-selector-class">.firstChild</span>.tagName)</pre></div><p id="72c6">There it is:</p><div id="becc"><pre><span class="hljs-built_in">Console</span> Window:</pre></div><div id="8636"><pre><span class="hljs-string">"<span class="hljs-keyword">HEAD</span>"</span></pre></div><p id="e14e"><b>How about the <code>body</code> element? Can you guess how to get it?</b></p><p id="49d0">You got it. Just add <code>.<b>lastChild</b></code> to <code>document.documentElement</code> instead:</p><div id="e7cb"><pre><span class="hljs-built_in">document</span>.documentElement.lastChild</pre></div><p id="fea3">This will give us Codepen’s entire <code><b>body</b></code> element.</p><div id="f03d"><pre><span class="hljs-built_in">Console</span> Window:</pre></div><div id="8694"><pre><<span class="hljs-keyword">body</span>>...</<span class="hljs-keyword">body</span>></pre></div><p id="53a9">So let’s also add <code>.tagName</code> like before:</p><div id="3041"><pre>document<span class="hljs-selector-class">.documentElement</span><span class="hljs-selector-class">.lastChild</span>.tagName</pre></div><p id="eabc">To verify this works, let’s <code>console.log()</code> it once more:</p><div id="fe10"><pre>console<span class="hljs-selector-class">.log</span>(document<span class="hljs-selector-class">.documentElement</span><span class="hljs-selector-class">.lastChild</span>.tagName)</pre></div><p id="ce51">And there it is:</p><div id="6822"><pre><span class="hljs-built_in">Console</span> Window:</pre></div><div id="e81c"><pre><span class="hljs-string">"BODY"</span></pre></div><p id="418c">To review, inside <code><b>document</b></code> is the <i>entire virtual HTML Tree</i>. To access its children and its children’s children, and so on and so forth, we use a <b>period</b> (or <b>dot)</b>:</p><div id="cc60"><pre>.</pre></div><p id="d840">This is referred to as the <b>Dot Notation</b> in JavaScript.</p><p id="ccef">The finished JavaScript code on <a href="https://codepen.io/thonly/pen/XWpEKEa?editors=1111">Codepen</a>:</p><figure id="a615"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Rz0xk8QVVk0Q-eHM0TlYEA.png"><figcaption><a href="https://codepen.io/thonly/pen/XWpEKEa?editors=1111">https://codepen.io/thonly/pen/XWpEKEa?editors=1111</a></figcaption></figure><h2 id="4755">Codepen</h2><p id="7d1c">The complete finished <a href="https://codepen.io/thonly/pen/XWpEKEa?editors=1111">Codepen</a> for this lesson:</p> <figure id="bc0f"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fthonly%2Fembed%2Fpreview%2FXWpEKEa%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DXWpEKEa&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fthonly%2Fpen%2FXWpEKEa%3Feditors%3D1111&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" allowfullscreen="" frameborder="0" height="600" width="800"> </div> </div> </figure></iframe></div></div></figure><h1 id="e781">Summary</h1><p id="4a76">Let’s summarize what we have learned using MDN.</p><h2 id="6d01">HTML Tree Structure</h2><figure id="ec7c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-l6TjG-7LJ6KQ5sqkLectA.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Using_the_W3C_DOM_Level_1_Core">https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Using_the_W3C_DOM_Level_1_Core</a></figcaption></figure><p id="fa7d"><b>HTML documents</b> are organized into<b> tree structures</b> because they are <i>easier to read</i> and <i>faster to search through</i>.</p><div id="bde6"><pre><span class="hljs-tag"><<span class="hljs-name">html</span>></span> <span class="hljs-tag"><<span class="hljs-name">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>My Document<span class="hljs-tag"></<span class="hljs-name">title</span>></span> <span class="hljs-tag"></<span class="hljs-name">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">body</span>></span> <span class="hljs-tag"><<span class="hljs-name">h1</span>></span>Header<span class="hljs-tag"></<span class="hljs-name">h1</span>></span> <span class="hljs-tag"><<span class="hljs-name">p</span>></span>Paragraph<span class="hljs-tag"></<span class="hljs-name">p</span>></span> <span class="hljs-tag"></<span class="hljs-name">body</span>></span> <span class="hljs-tag"></<span class="hljs-name">html</span>></span></pre></div><figure id="8ce2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*y_fQ0X4FgDB0gFAZ.jpg"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Using_the_W3C_DOM_Level_1_Core/using_the_w3c_dom_level_1_core-doctree.jpg">https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Using_the_W3C_DOM_Level_1_Core/using_the_w3c_dom_level_1_core-doctree.jpg</a></figcaption></figure><p id="2aaa">A HTML tree structure always begins with the <b>root</b> <code><b>html</b></code> tag.</p><div id="936f"><pre>HTML Document <span class="hljs-operator">=</span>> html (root)</pre></div><p id="e811">Inside of which there are <b>only 2</b> possible children:</p><div id="5a72"><pre><span class="hljs-attr">html</span> => head + body</pre></div><p id="9d5a">The <code><b>head</b></code> tag followed by the <code><b>body</b></code> tag.</p><figure id="99f2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*57bYJnEBw_f5OHGYqhExZg.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head</a></figcaption></figure><p id="6ffb">The <code><b>head</b></code> tag is where we place <i>information about the document</i> such as the title, author, etc., which are also known as the <b>metadata</b>.</p><div id="0a59"><pre><span class="hljs-attribute">head</span> <span class="hljs-operator">=</span>> metadata</pre></div><figure id="479c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*hpJbEu-CTMmf0WSn9VydSw.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body</a></figcaption></figure><p id="59b1">The <code><b>body</b></code> tag is where we place the <i>visible content</i> of the document such as a header, a paragraph, etc.</p><div id="4e56"><pre><span class="hljs-attribute">body</span> <span class="hljs-operator">=</span>> content</pre></div><figure id="3aaa"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fesJecSI3yo5PJ4af5BmYw.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html</a></figcaption></figure><p id="4bf5">Again, the <code><b>html</b></code> tag is the <b>root</b> tag. Its content is just one <code><b>head</b></code> tag followed by one <code><b>body</b></code> tag.</p><div id="fb93"><pre><span class="hljs-section"><html></span> <span class="hljs-section"><head></span><span class="hljs-section"></head></span> <span class="hljs-section"><body></span><span class="hljs-section"></body></span> <span class="hljs-section"></html></span></pre></div><p id="1ed0">The <code><b>head</b></code> tag contains the <b>metadata</b> while the <code><b>body</b></code> contains the actual <b>content.</b></p><div id="bea4"><pre><span class="hljs-attribute">head</span> <span class="hljs-operator">=</span>> metadata <span class="hljs-attribute">body</span> <span class="hljs-operator">=</span>> content</pre></div><p id="7fd4" type="7">Please commit this very important tree to your memory!</p><h2 id="ca1a">DOM Tree</h2><figure id="3e22"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*7az3WrdKNc-zZOuMbFEEUQ.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_works">https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_works</a></figcaption></figure><p id="863f">Remember, a HTML document is really only a <b>file</b> with <b>plain static text</b>.</p><div id="4d99"><pre><span class="hljs-built_in">HTML</span> Document = Plain Static <span class="hljs-built_in">Text</span></pre></div><figure id="d475"><img src

Options

="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*3JX89x3-mCr4V_6N2eF3Ww.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_works/rendering.svg">https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_works/rendering.svg</a></figcaption></figure><p id="c40a">To make the document easier to update, the browser converts the HTML into a special object in JavaScript called the <b>DOM Tree</b> through a process called <b>parsing</b>.</p><div id="7c68"><pre>HTML Code <span class="hljs-operator">=</span>> Parsing <span class="hljs-operator">=</span>> JavaScript DOM Tree</pre></div><p id="44c7">In fact, CSS code is also just a <b>file</b> with <b>plain static text</b>.</p><div id="af37"><pre>CSS <span class="hljs-built_in">Code</span> = Plain Static <span class="hljs-built_in">Text</span></pre></div><p id="e736">So the browser also <b>parses</b> it and then <b>attaches</b> it to the DOM Tree.</p><div id="c8ff"><pre>CSS Code <span class="hljs-operator">=</span>> Parsing <span class="hljs-operator">=</span>> CSS DOM Tree</pre></div><p id="a5d0">Finally, the browser <b>paints</b> the DOM Tree onto the UI window.</p><div id="960e"><pre>DOM Tree <span class="hljs-operator">=</span>> Painting <span class="hljs-operator">=</span>> UI Window</pre></div><p id="285f">By converting the <b>static</b> HTML Tree into a <b>virtual</b> DOM Tree in memory, the document becomes more <b>dynamic</b> and therefore <i>easily changeable</i>.</p><div id="9017"><pre><span class="hljs-variable">HTML</span> <span class="hljs-built_in">Tree</span> <span class="hljs-operator">=></span> <span class="hljs-variable">DOM</span> <span class="hljs-built_in">Tree</span> <span class="hljs-operator">=></span> <span class="hljs-built_in">Dynamic</span></pre></div><p id="2946">Without this, for example, smooth animations would be extremely difficult, or impossible to achieve.</p><p id="3a25">In JavaScript, the DOM Tree resides inside a special object called the <code>document</code>.</p><div id="ccc1"><pre>DOM Tree => <span class="hljs-built_in">document</span></pre></div><p id="61c9">To access its children and any descendants, we use the <b>dot notation</b>:</p><div id="2cdc"><pre><span class="hljs-function"><span class="hljs-title">document</span></span>...</pre></div><p id="20bb">By far, this DOM Tree is the <i>most important structure</i> to know as a Frontend Engineer.</p><h1 id="1b13">Concept Quiz</h1><p id="8093">Take my <a href="https://frontend.siliconwat.com/#learn-chapter3"><b>Programming Concept Quiz</b></a> to check your understanding! For every correct choice, you will earn <b>SW Coins</b> which you can redeem for <b><i>coupons</i></b> towards the purchase of any of my <a href="https://siliconwat.com">Udemy courses</a>!</p><figure id="a601"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Xex5-axJQeihGE_y45khnw.png"><figcaption><a href="https://quiz.siliconwat.com/?lang=en#frontend-chapter3">https://quiz.siliconwat.com/?lang=en#frontend-chapter3</a></figcaption></figure><p id="8530"><b><i>Sample</i></b> Quiz Questions for <b>Lesson 3</b>:</p><h2 id="336f">Question 1:</h2><p id="7386">When we write HTML tags <b>inside of </b>HTML tags, what <b>data structure</b> does this produce?</p><ol><li>Linear structure</li><li>Tree structure</li></ol><h2 id="afa3">Question 2:</h2><p id="0312">A HTML tree structure begins with what <b>tag</b>?</p><ol><li><code><head></head></code></li><li><code><body></body></code></li><li><code><html></html></code></li></ol><h2 id="e0a5">Question 3:</h2><p id="bb46">Which HTML tag is the <b>root</b> tag?</p><ol><li><code><html></html></code></li><li><code><head></head></code></li></ol><h2 id="92a9">Question 4:</h2><p id="e329">How many <i>children</i> tags can the <b>root</b> tag have?</p><ol><li>1</li><li>2</li><li>Infinite</li></ol><h2 id="11c3">Question 5:</h2><p id="ae05">Which tag is NOT a <b>child</b> of the <b>root</b>?</p><ol><li><code><head></head></code></li><li><code><body></body></code></li><li><code><p></p></code></li></ol><h2 id="7284">Question 6:</h2><p id="4b1d">To make the HTML tree structure easier to read, the convention among coders is to <b>indent the children</b>.</p><ol><li>True</li><li>False</li></ol><h2 id="a2a0">Question 7:</h2><p id="0de6">Which tag is the <b>last child</b> of the <b>root</b>?</p><ol><li><code><head></head></code></li><li><code><body></body></code></li><li><code><html></html></code></li></ol><h2 id="2198">Question 8:</h2><p id="d43e">What tag is the <b>parent</b> of <code><b>head</b></code>?</p><ol><li><code><body></body></code></li><li><code><html></html></code></li></ol><h2 id="cbf6">Question 9:</h2><p id="2340">It is faster to search through a<b> linear structure</b> than a <b>tree structure</b>?</p><ol><li>True</li><li>False</li></ol><h2 id="7c40">Question 10:</h2><p id="a261">A <b>tree structure</b> is <i>faster</i> because we might not need to search through every element to find the one that we want.</p><ol><li>True</li><li>False</li></ol><h2 id="bd9a">Question 11:</h2><p id="beea">What CSS <b>selector</b> will select every element that is a <b>first child</b>?</p><ol><li><code>first-child</code></li><li><code>:first-child</code></li></ol><h2 id="5175">Question 12:</h2><p id="9d32">What CSS <b>selector</b> will select every element that is a <b>last child</b>?</p><ol><li><code>:lastChild</code></li><li><code>:last-child</code></li></ol><h2 id="e4a3">Question 13:</h2><p id="2b76">What information is placed inside the <code><b>head</b></code> tag?</p><ol><li>data</li><li>metadata</li></ol><h2 id="37ba">Question 14:</h2><p id="2370">Where should we place a <code><b>p</b></code> tag?</p><ol><li>Inside the <code><b>head</b></code> tag</li><li>Inside the <code><b>body</b></code> tag</li></ol><h2 id="4c40">Question 15:</h2><p id="4d26">It is possible to apply CSS styles to the <code><b>head</b></code> element.</p><ol><li>True</li><li>False</li></ol><h2 id="eed0">Question 16:</h2><p id="7ff1">What best describes a HTML document?</p><ol><li>Static</li><li>Dynamic</li></ol><h2 id="8d47">Question 17:</h2><p id="2d2f">What best describes a DOM?</p><ol><li>Static</li><li>Dynamic</li></ol><h2 id="a315">Question 18:</h2><p id="24f3">The Document Object Model is a <b>virtual copy</b> of the HTML document.</p><ol><li>True</li><li>False</li></ol><h2 id="290b">Question 19:</h2><p id="610a">It is faster to make changes to an HTML Tree than a DOM Tree.</p><ol><li>True</li><li>False</li></ol><h2 id="f7f3">Question 20:</h2><p id="bfe7">What JavaScript command gives us the DOM?</p><ol><li><code>console</code></li><li><code>document</code></li></ol><h2 id="6db9">Question 21:</h2><p id="802a">The entire DOM Tree resides inside <code>document</code>.</p><ol><li>True</li><li>False</li></ol><h2 id="3a8f">Question 22:</h2><p id="fff0">What <b>notation</b> do we use to access any descendants of <code>document</code>?</p><ol><li>Comma</li><li>Dot</li></ol><h2 id="3019">Question 23:</h2><p id="aeb4">What will this command give us: <code>document.documentElement</code>?</p><ol><li><b>root</b> element</li><li><code><b>head</b></code> element</li><li><code><b>body</b></code> element</li></ol><h2 id="6361">Question 24:</h2><p id="1f1d">What will this command give us: <code>document.documentElement.tagName</code>?</p><ol><li><code>"HTML"</code></li><li><code>"HEAD"</code></li><li><code>"BODY"</code></li></ol><h2 id="94ec">Question 25:</h2><p id="fb17"><code>document.documentElement.firstChild</code> will give us what?</p><ol><li><code><b>html</b></code> element</li><li><code><b>head</b></code> element</li><li><code><b>body</b></code> element</li></ol><h2 id="b83c">Question 26:</h2><p id="1da8"><code>document.documentElement.firstChild.tagName</code> will give us what?</p><ol><li><code>"HTML"</code></li><li><code>"HEAD"</code></li><li><code>"BODY"</code></li></ol><h2 id="07e4">Question 27:</h2><p id="380f">The browser parses the HTML document into the DOM Tree.</p><ol><li>True</li><li>False</li></ol><h2 id="ec09">Question 28:</h2><p id="7590">After parsing the CSS file, where does the browser attaches it to?</p><ol><li>HTML document</li><li>DOM Tree</li></ol><h2 id="9a39">Question 29:</h2><p id="71f8">What is the most important structure to know as a Frontend Engineer?</p><ol><li>HTML Tree</li><li>DOM Tree</li></ol><figure id="b6d8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*LFoGQg1kZwfRXAdauzGndw.png"><figcaption><a href="https://quiz.siliconwat.com/?lang=en#frontend-chapter3">https://quiz.siliconwat.com/?lang=en#frontend-chapter3</a></figcaption></figure><p id="4d3c"><a href="https://frontend.siliconwat.com/#learn-chapter3">Programming Concept Quiz for Chapter 3</a></p><h1 id="81a3">Coding Exercises</h1><p id="28d1">Check out my <a href="https://frontend.siliconwat.com/#practice-chapter3"><b>Interactive Coding Exercises</b></a> to put to practice what you have learned! There, you will also find <b><i>interactive hints</i></b> to help you understand<i> each line of code. </i>Likewise, for every correct solution, you will earn <b>SW Coins</b> which you can redeem for <b><i>coupons</i></b> towards the purchase of any of my <a href="https://siliconwat.com">Udemy courses</a>!</p><p id="b26c"><a href="https://frontend.siliconwat.com/#practice-chapter3">Interactive Coding Exercises for Chapter 3</a></p><h1 id="da70">Syntax Flashcards</h1><p id="ebac">Review what you have learned by playing my <a href="https://frontend.siliconwat.com/#review-chapter3"><b>Syntax Flashcard Game</b></a>! These flashcards are designed to help you <b><i>commit to memory</i></b> all the <b><i>new</i></b> <b>code</b> <b>syntaxes</b> you learned in this lesson.</p><figure id="1d2c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fhypa8VKeL8WA_swazWVfw.png"><figcaption><a href="https://flashcard.siliconwat.com/?lang=en#frontend-chapter3">https://flashcard.siliconwat.com/?lang=en#frontend-chapter3</a></figcaption></figure><p id="910c">Likewise, for every correct answer, you will earn <b>SW Coins</b> which you can redeem for <b><i>coupons</i></b> towards the purchase of any of my <a href="https://siliconwat.com">Udemy courses</a>!</p><figure id="3293"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*iMj61q1WoLG61HCLX9LPtA.png"><figcaption><a href="https://flashcard.siliconwat.com/?lang=en#frontend-chapter3">https://flashcard.siliconwat.com/?lang=en#frontend-chapter3</a></figcaption></figure><p id="20ad"><a href="https://frontend.siliconwat.com/#review-chapter3">Syntax Flashcard Game for Chapter 3</a></p><h1 id="d525">Next Steps</h1><p id="779e">Congrats on completing <b>Unit 1:</b> <b>Lesson 3 of 5</b>! 🎉</p><div id="9f05"><pre><span class="hljs-attribute">Unit</span> <span class="hljs-number">1</span>: <span class="hljs-number">60</span>% Completed <span class="hljs-attribute">Unit</span> <span class="hljs-number">2</span>: <span class="hljs-number">0</span>% Completed <span class="hljs-attribute">Unit</span> <span class="hljs-number">3</span>: <span class="hljs-number">0</span>% Completed <span class="hljs-attribute">Unit</span> <span class="hljs-number">4</span>: <span class="hljs-number">0</span>% Completed <span class="hljs-attribute">Unit</span> <span class="hljs-number">5</span>: <span class="hljs-number">0</span>% Completed <span class="hljs-attribute">Bonus</span> Unit <span class="hljs-number">6</span>: <span class="hljs-number">0</span>% Completed <span class="hljs-attribute">Bonus</span> Unit <span class="hljs-number">7</span>: <span class="hljs-number">0</span>% Completed</pre></div><div id="5403"><pre><span class="hljs-attribute">Overall</span> Progress: <span class="hljs-number">3</span>% Completed</pre></div><p id="370a"><a href="https://frontend.siliconwat.org">Join Remote Frontend Cohort Program</a></p><h2 id="b9c2">Next Lesson</h2><p id="6832">In the next lesson, we will learn how to <b>import</b> CSS code via <b>three</b> different methods. To import <b>external</b> CSS code, we use the <code>link</code> tag. To include <b>internal</b> CSS code, we use the <code>style</code> tag. To include <b>inline</b> CSS code, we use the <code>style</code> <i>attribute</i>. This lesson will go over these methods in detail!</p><p id="53ec"><b><a href="https://readmedium.com/chapter-4-importing-css-code-81264eb81d5">Chapter 4: Importing CSS Code</a></b></p><p id="f20a"><b><a href="https://frontend.siliconwat.com">Table of Contents</a></b></p><div id="f916" class="link-block"> <a href="https://medium.com/@thonly/membership"> <div> <div> <h2>Join Medium with my referral link — Thon Ly</h2> <div><h3>As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*gOmGdkmkAHn0xYBV)"></div> </div> </div> </a> </div><blockquote id="0a07"><p>When you use my <a href="https://medium.com/@thonly/membership">referral link</a> above 👆 to become a Medium member, <b>all proceeds</b> will be donated towards the construction of the <a href="https://siliconwat.org">Silicon Wat Campus</a> for children in <b>Ukraine</b> and <b>Cambodia</b> ❤️</p></blockquote></article></body>

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

Table of Contents

Overview

This lesson covers the following HTML tags, CSS topics, and JavaScript commands for the first time:

HTML

  1. html
  2. head
  3. body

CSS

  1. background
  2. :first-child
  3. :last-child

JS

  1. document (Document Object Model)
  2. document.documentElement
  3. document.documentElement.firstChild
  4. document.documentElement.lastChild
  5. 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 => Structure

HTML 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 structure

Like 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 Time

On 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 Time

In 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:

https://codepen.io/thonly/pen/XWpEKEa?editors=1111

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-child and :last-child selectors 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 => content

The head element is where we describe the entire document, such as its title, author, date, etc.

head => document

Because 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:

https://codepen.io/thonly/pen/XWpEKEa?editors=1111

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 HTML

This 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 Text

By converting it into a virtual model in the memory, the HTML Tree becomes more dynamic.

Virtual HTML => Dynamic

As 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 = DOM

In JavaScript, the DOM is represented by the command:

JS Window:
document

Inside of which is the entire HTML virtual tree.

document => DOM

Within the DOM, tags are referred to as elements.

HTML => tags
DOM => elements

For example, we know that the HTML Tree begins with the root html element.

With JavaScript, we can get it with the command:

document.documentElement

To 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.firstChild

To 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.lastChild

This will give us Codepen’s entire body element.

Console Window:
<body>...</body>

So let’s also add .tagName like before:

document.documentElement.lastChild.tagName

To 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:

https://codepen.io/thonly/pen/XWpEKEa?editors=1111

Codepen

The complete finished Codepen for this lesson:

Summary

Let’s summarize what we have learned using MDN.

HTML Tree Structure

https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Using_the_W3C_DOM_Level_1_Core

HTML documents are organized into tree structures because they are easier to read and faster to search through.

<html>
  <head>
    <title>My Document</title>
  </head>
  <body>
    <h1>Header</h1>
    <p>Paragraph</p>
  </body>
</html>
https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Using_the_W3C_DOM_Level_1_Core/using_the_w3c_dom_level_1_core-doctree.jpg

A HTML tree structure always begins with the root html tag.

HTML Document => html (root)

Inside of which there are only 2 possible children:

html => head + body

The head tag followed by the body tag.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head

The head tag is where we place information about the document such as the title, author, etc., which are also known as the metadata.

head => metadata
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body

The body tag is where we place the visible content of the document such as a header, a paragraph, etc.

body => content
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html

Again, the html tag is the root tag. Its content is just one head tag followed by one body tag.

<html>
  <head></head>
  <body></body>
</html>

The head tag contains the metadata while the body contains the actual content.

head => metadata
body => content

Please commit this very important tree to your memory!

DOM Tree

https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_works

Remember, a HTML document is really only a file with plain static text.

HTML Document = Plain Static Text
https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_works/rendering.svg

To make the document easier to update, the browser converts the HTML into a special object in JavaScript called the DOM Tree through a process called parsing.

HTML Code => Parsing => JavaScript DOM Tree

In fact, CSS code is also just a file with plain static text.

CSS Code = Plain Static Text

So the browser also parses it and then attaches it to the DOM Tree.

CSS Code => Parsing => CSS DOM Tree

Finally, the browser paints the DOM Tree onto the UI window.

DOM Tree => Painting => UI Window

By converting the static HTML Tree into a virtual DOM Tree in memory, the document becomes more dynamic and therefore easily changeable.

HTML Tree => DOM Tree => Dynamic

Without this, for example, smooth animations would be extremely difficult, or impossible to achieve.

In JavaScript, the DOM Tree resides inside a special object called the document.

DOM Tree => document

To access its children and any descendants, we use the dot notation:

document...

By far, this DOM Tree is the most important structure to know as a Frontend Engineer.

Concept Quiz

Take my Programming Concept Quiz to check your understanding! For every correct choice, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!

https://quiz.siliconwat.com/?lang=en#frontend-chapter3

Sample Quiz Questions for Lesson 3:

Question 1:

When we write HTML tags inside of HTML tags, what data structure does this produce?

  1. Linear structure
  2. Tree structure

Question 2:

A HTML tree structure begins with what tag?

  1. <head></head>
  2. <body></body>
  3. <html></html>

Question 3:

Which HTML tag is the root tag?

  1. <html></html>
  2. <head></head>

Question 4:

How many children tags can the root tag have?

  1. 1
  2. 2
  3. Infinite

Question 5:

Which tag is NOT a child of the root?

  1. <head></head>
  2. <body></body>
  3. <p></p>

Question 6:

To make the HTML tree structure easier to read, the convention among coders is to indent the children.

  1. True
  2. False

Question 7:

Which tag is the last child of the root?

  1. <head></head>
  2. <body></body>
  3. <html></html>

Question 8:

What tag is the parent of head?

  1. <body></body>
  2. <html></html>

Question 9:

It is faster to search through a linear structure than a tree structure?

  1. True
  2. False

Question 10:

A tree structure is faster because we might not need to search through every element to find the one that we want.

  1. True
  2. False

Question 11:

What CSS selector will select every element that is a first child?

  1. first-child
  2. :first-child

Question 12:

What CSS selector will select every element that is a last child?

  1. :lastChild
  2. :last-child

Question 13:

What information is placed inside the head tag?

  1. data
  2. metadata

Question 14:

Where should we place a p tag?

  1. Inside the head tag
  2. Inside the body tag

Question 15:

It is possible to apply CSS styles to the head element.

  1. True
  2. False

Question 16:

What best describes a HTML document?

  1. Static
  2. Dynamic

Question 17:

What best describes a DOM?

  1. Static
  2. Dynamic

Question 18:

The Document Object Model is a virtual copy of the HTML document.

  1. True
  2. False

Question 19:

It is faster to make changes to an HTML Tree than a DOM Tree.

  1. True
  2. False

Question 20:

What JavaScript command gives us the DOM?

  1. console
  2. document

Question 21:

The entire DOM Tree resides inside document.

  1. True
  2. False

Question 22:

What notation do we use to access any descendants of document?

  1. Comma
  2. Dot

Question 23:

What will this command give us: document.documentElement?

  1. root element
  2. head element
  3. body element

Question 24:

What will this command give us: document.documentElement.tagName?

  1. "HTML"
  2. "HEAD"
  3. "BODY"

Question 25:

document.documentElement.firstChild will give us what?

  1. html element
  2. head element
  3. body element

Question 26:

document.documentElement.firstChild.tagName will give us what?

  1. "HTML"
  2. "HEAD"
  3. "BODY"

Question 27:

The browser parses the HTML document into the DOM Tree.

  1. True
  2. False

Question 28:

After parsing the CSS file, where does the browser attaches it to?

  1. HTML document
  2. DOM Tree

Question 29:

What is the most important structure to know as a Frontend Engineer?

  1. HTML Tree
  2. DOM Tree
https://quiz.siliconwat.com/?lang=en#frontend-chapter3

Programming Concept Quiz for Chapter 3

Coding Exercises

Check out my Interactive Coding Exercises to put to practice what you have learned! There, you will also find interactive hints to help you understand each line of code. Likewise, for every correct solution, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!

Interactive Coding Exercises for Chapter 3

Syntax Flashcards

Review what you have learned by playing my Syntax Flashcard Game! These flashcards are designed to help you commit to memory all the new code syntaxes you learned in this lesson.

https://flashcard.siliconwat.com/?lang=en#frontend-chapter3

Likewise, for every correct answer, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!

https://flashcard.siliconwat.com/?lang=en#frontend-chapter3

Syntax Flashcard Game for Chapter 3

Next Steps

Congrats on completing Unit 1: Lesson 3 of 5! 🎉

Unit 1: 60% Completed
Unit 2: 0% Completed
Unit 3: 0% Completed
Unit 4: 0% Completed
Unit 5: 0% Completed
Bonus Unit 6: 0% Completed
Bonus Unit 7: 0% Completed
Overall Progress: 3% Completed

Join Remote Frontend Cohort Program

Next Lesson

In the next lesson, we will learn how to import CSS code via three different methods. To import external CSS code, we use the link tag. To include internal CSS code, we use the style tag. To include inline CSS code, we use the style attribute. This lesson will go over these methods in detail!

Chapter 4: Importing CSS Code

Table of Contents

When you use my referral link above 👆 to become a Medium member, all proceeds will be donated towards the construction of the Silicon Wat Campus for children in Ukraine and Cambodia ❤️

HTML
CSS
JavaScript
Programming
Front End Development
Recommended from ReadMedium