avatarThon Ly

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

13664

Abstract

        <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%2FjOyRwMg%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DjOyRwMg&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fthonly%2Fpen%2FjOyRwMg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" allowfullscreen="" frameborder="0" height="600" width="800">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="3f19">Inline CSS</h2><p id="28b8">The <b>third</b> and final method to include CSS code in our document is called <b>inline</b>.</p><div id="ea5b"><pre><span class="hljs-keyword">Method</span> 3: <span class="hljs-keyword">Inline</span> CSS</pre></div><p id="5bbb">On the tag that we want to include CSS code, we can simply add an <b>attribute</b> called <code>style</code>:</p><div id="f561"><pre><span class="hljs-selector-tag">HTML</span> Window:</pre></div><div id="c6b1"><pre><span class="hljs-section">&lt;html&gt;</span>

<span class="hljs-section"><head></span><span class="hljs-section"></head></span> <span class="hljs-section"><body style ></span><span class="hljs-section"></body></span> <span class="hljs-section"></html></span></pre></div><p id="b858">Then, we set it equal to the CSS <b>declarations</b> we want to apply to the said <code><b>body</b></code> tag.</p><p id="d8df">For example:</p><div id="0786"><pre><span class="hljs-selector-tag">HTML</span> Window:</pre></div><div id="d996"><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">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">body</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"background: skyblue;"</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><blockquote id="2db9"><p>Don’t forget to surround the declarations with <b>quotes</b>!</p></blockquote><p id="7bfe">The CSS <code>body</code> <i>selector</i> is not necessary here because it’s already implied by its presence inside the <code><b>body</b></code> bag.</p><div id="33fe"><pre><<span class="hljs-keyword">body</span> style=<span class="hljs-string">"background: skyblue;"</span>></<span class="hljs-keyword">body</span>></pre></div><p id="f630">Notice that there are now <i>three</i> different locations where the CSS rulesets want to set the color of the background, yet in this case, priority is given to our inline CSS code.</p><div id="6e73"><pre>UI <span class="hljs-built_in">Window</span>:</pre></div><div id="dc77"><pre>[<span class="hljs-built_in">background</span> changed to sky <span class="hljs-built_in">blue</span>]</pre></div><p id="54c1" type="7">Why is that?</p><p id="3d07">The answer is called <b>CSS Specificity</b> which basically states that priority will be given to the CSS property that is <i>more specific</i>.</p><p id="d353">You may have guessed it by now: <i>inline</i> CSS is more specific than <i>internal</i> CSS which is more specific than <i>external</i> CSS.</p><div id="448a"><pre><span class="hljs-keyword">Inline</span> CSS > <span class="hljs-type">Internal</span> CSS > <span class="hljs-keyword">External</span> CSS</pre></div><p id="a96e">In other words, <i>inline</i> CSS is the most specific and has the <i>highest</i> priority.</p><div id="627d"><pre><span class="hljs-keyword">Inline</span> CSS > <span class="hljs-type">Internal</span> CSS > <span class="hljs-keyword">External</span> CSS</pre></div><p id="c772">While <i>external</i> CSS is the least specific and has the <i>lowest</i> priority.</p><div id="9f45"><pre><span class="hljs-keyword">Inline</span> CSS > <span class="hljs-type">Internal</span> CSS > <span class="hljs-keyword">External</span> CSS</pre></div><blockquote id="b068"><p><b>CSS Specificity</b> has many other nuances which we will study in detail in Unit 3.</p></blockquote><p id="6478">The finished <a href="https://codepen.io/thonly/pen/MWJRova">Codepen</a>:</p> <figure id="deb8"> <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%2FMWJRova%3Fdefault-tabs%3Dhtml%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DMWJRova&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fthonly%2Fpen%2FMWJRova%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="83aa">Summary</h1><p id="fdaf">There are <b>three</b> ways to include CSS code in our HTML document.</p><div id="f903"><pre><span class="hljs-keyword">Method</span> 1: <span class="hljs-keyword">External</span> CSS <span class="hljs-keyword">Method</span> 2: Internal CSS <span class="hljs-keyword">Method</span> 3: <span class="hljs-keyword">Inline</span> CSS</pre></div><h2 id="eeae">External CSS</h2><p id="0fcf">The <b>first</b> method is called <b>external</b> which uses the <code><b>link</b></code> tag inside the <code><b>head</b></code>.</p><figure id="5283"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*sYgVoeTe5mX3CcukP0KDaQ.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#external_stylesheet">https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#external_stylesheet</a></figcaption></figure><p id="24db"><code><b>link</b></code> tags do not have content so a closing tag is not necessary.</p><div id="40fb"><pre><span class="hljs-tag"><<span class="hljs-name">link</span>></span><span class="hljs-tag"></<span class="hljs-name">link</span>></span> => <span class="hljs-tag"><<span class="hljs-name">link</span>></span></pre></div><p id="4ea8">Formally, they are known as <b>empty</b> elements because they do not have any children.</p><figure id="925d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Tyd2n86wiODuJimNsNWlcA.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Glossary/Empty_element">https://developer.mozilla.org/en-US/docs/Glossary/Empty_element</a></figcaption></figure><p id="2317">In other words, we can think of empty elements like parents who cannot have children.</p><div id="75e8"><pre>Empty elements <span class="hljs-operator">=</span>> Cannot have children</pre></div><p id="62ea">Instead, <code><b>link</b></code> tags have <b>attributes</b> that we can specify.</p><figure id="7c69"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UmBDOPNzIrCbX7UWkxynKg.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Glossary/Attribute">https://developer.mozilla.org/en-US/docs/Glossary/Attribute</a></figcaption></figure><p id="b10e">Attributes always have the form:</p><div id="e71c"><pre><span class="hljs-attribute">name</span><span class="hljs-operator">=</span><span class="hljs-string">"value"</span></pre></div><p id="2753">And are always written inside of <i>opening</i> tags:</p><div id="2579"><pre><tag <span class="hljs-attribute">name</span>=<span class="hljs-string">"value"</span>></pre></div><p id="fdba">For example, to import an external CSS file, we specify a <b>relationship</b> <i>attribute</i> and a <b>hypertext reference</b> <i>attribute</i>:</p><div id="3f0b"><pre><link <span class="hljs-attribute">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attribute">href</span>=<span class="hljs-string">"styles.css"</span>></pre></div><h2 id="0d16">Internal CSS</h2><p id="76b3">The <b>second</b> method to include CSS code is called <b>internal</b> which uses the <code><b>style</b></code> tag inside the <code><b>head</b></code>.</p><figure id="6a55"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*TJrlX8QkN0MdRs2RfQMDQQ.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#internal_stylesheet">https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#internal_stylesheet</a></figcaption></figure><p id="e2d5">Unlike the <code><b>link</b></code> tag, the <code><b>style</b></code> tag has <i>content</i> and therefore must include a <i>closing</i> tag.</p><div id="7cc1"><pre><span class="hljs-tag"><<span class="hljs-name">style</span>></span><span class="hljs-tag"></<span class="hljs-name">style</span>></span></pre></div><p id="6ac7">The <i>content</i> is CSS code.</p><div id="2474"><pre><span class="hljs-tag"><<span class="hljs-name">style</span>></span><span class="language-css"> CSS <span class="hljs-selector-tag">code</span> goes here </span><span class="hljs-tag"></<span class="hljs-name">style</span>></span></pre></div><p id="6c04">In other words, we can think of <i>non-empty</i> elements like parents who can have children.</p><div id="c4e9"><pre>Non-<span class="hljs-keyword">empty</span> elements => Can have children</pre></div><h2 id="f0ba">Inline CSS</h2><p id="ead1">Finally, the <b>third</b> method to include CSS code is called <b>inline</b> which uses the <code>style</code> <i>attribute</i> on the tag that we want to style.</p><figure id="e677"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1NgUKQnKHCJ0H66zubaXug.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#inline_styles">https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#inline_styles</a></figcaption></figure><p id="f49d">The <b><i>value</i></b> of this attribute is CSS <b>declaration</b> <b>statements</b>.</p><div id="01d6"><pre><p <span class="hljs-built_in">style</span>=<span class="hljs-string">"color:red;"</span>>This <span class="hljs-built_in">is</span> my <span class="hljs-built_in">first</span> CSS <span class="hljs-built_in">example</span></p></pre></div><h2 id="d772">CSS Specificity</h2><p id="7146">When all three methods want to set the same CSS properties, priority will be given to the ruleset that is <i>more specific</i>.</p><div id="c15f"><pre>More <span class="hljs-keyword">Specific</span> Ruleset <span class="hljs-operator">></span> Less <span class="hljs-keyword">Specific</span> Ruleset</pre></div><p id="f2b5">For example, <i>inline</i> CSS is more specific than <i>internal</i> CSS.</p><div id="eda5"><pre><span class="hljs-keyword">Inline</span> CSS > <span class="hljs-type">Internal</span> CSS</pre></div><p id="1076">And <i>internal</i> CSS is more specific than <i>external</i> CSS.</p><div id="e9cc"><pre><span class="hljs-type">Internal</span> CSS > <span class="hljs-keyword">External</span> CSS</pre></div><p id="7969">This is called <b>CSS Specificity</b>.</p><figure id="d980"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*m7bzVTPvmMH-COROiQu-KQ.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity">https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity</a></figcaption></figure><blockquote id="9cc9"><p>Again, we will study <b>CSS Specificity</b> in great detail in Unit 3.</p></blockquote><p id="3209" type="7">Why would we use one method over another?</p><p id="5bef">The answer is that each method has both <b>pros</b> and <b>cons</b>.</p><p id="4a15"><b>External</b> CSS is great because we can contain all the CSS code in <b><i>one</i></b> <b>file</b>.</p><div id="2e8e"><pre>External CSS => <span class="hljs-keyword">Separate</span> <span class="hljs-keyword">File</span></pre></div><p id="1273">Which we can give to the Designers in our team.</p><div id="05ad"><pre>CSS <span class="hljs-keyword">File</span> => Designers</pre></div><p id="d9fa">Our Designers can simply focus on and edit this one CSS file while our Copywriters can focus on and edit the HTML file and nothing else.</p><div id="f5d4"><pre>Designers: CSS <span class="hljs-built_in">File</span> Copywriters: HTML <span class="hljs-built_in">File</span></pre></div><p id="f155">The <i>drawback</i> is that because it’s a separate file, it’s <i>slower</i> for the browser to go fetch the extra CSS file.</p><div id="066a"><pre><span class="hljs-attribute">Browser</span> <span class="hljs-operator">=</span>> fetching... <span class="hljs-operator">=</span>> CSS File</pre></div><p id="e0a6"><b>Inline</b> CSS is great because we can style each tag <i>individually</i> in our HTML document.</p><div id="11ec"><pre><span class="hljs-keyword">Inline</span> CSS => can style each HTML tag</pre></div><p id="40ce">However, the <i>drawback</i> is that our CSS code <i>commingles</i> with our HTML code which can be confusing for both our Designers and our Copywriters to edit.</p><div id="f359"><pre>Inline CSS: CSS <span class="hljs-keyword">code</span> commingles <span class="hljs-keyword">with</span> HTML <span class="hljs-keyword">code</span></pre></div><p id="61f4"><b>Internal</b> CSS is great because it’s similar

Options

to external CSS in that all the CSS code is located in <i>one</i> place.</p><div id="fd1a"><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">style</span>></span><span class="language-css"> CSS <span class="hljs-selector-tag">code</span> </span><span class="hljs-tag"></<span class="hljs-name">style</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">body</span>></span> <span class="hljs-tag"></<span class="hljs-name">html</span>></span></pre></div><p id="5fc6">Yet, it still exists within the same HTML file.</p><p id="ccd4">Therefore, it’s faster than external CSS.</p><div id="80f7"><pre>Speed: <span class="hljs-type">Internal</span> CSS > <span class="hljs-keyword">External</span> CSS</pre></div><p id="162e">However, similar to inline CSS, internal CSS is in the <i>same</i> file as our HTML code.</p><div id="ba99"><pre><span class="hljs-selector-tag">HTML</span> <span class="hljs-selector-tag">File</span>: <span class="hljs-selector-tag">HTML</span> <span class="hljs-selector-tag">Code</span> + <span class="hljs-selector-tag">CSS</span> <span class="hljs-selector-tag">Code</span></pre></div><p id="251f">Hence, our Designers and our Copywriters will have to edit the <i>same</i> file when making changes. This can cause confusions and bugs.</p><div id="d1b5"><pre>HTML <span class="hljs-built_in">File</span>: Copywriters + Designers</pre></div><p id="56dd">The best practice, therefore, is actually <i>external</i> CSS because it creates <b>“separation of concerns”</b> which minimizes team members from editing <i>the</i> <i>same files at the same time</i>.</p><figure id="7314"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wznRhVTCfIj_HJG1Z1QTiQ.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Glossary/MVC">https://developer.mozilla.org/en-US/docs/Glossary/MVC</a></figcaption></figure><h1 id="0980">Concept Quiz</h1><p id="d04c">Take my <a href="https://frontend.siliconwat.com/#learn-chapter4"><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="3927"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*oxvm0B3lKD_FP7YTgCoKMw.png"><figcaption><a href="https://quiz.siliconwat.com/?lang=en#frontend-chapter4">https://quiz.siliconwat.com/?lang=en#frontend-chapter4</a></figcaption></figure><p id="dfad"><b><i>Sample</i></b> Quiz Questions for <b>Lesson 4</b>:</p><h2 id="f55b">Question 1:</h2><p id="051c">Which is NOT a method to include CSS code in the HTML document?</p><ol><li>inline</li><li>online</li><li>internal</li><li>external</li></ol><h2 id="015e">Question 2:</h2><p id="1674">What tag is used to import an <i>external</i> CSS file?</p><ol><li><code><head></head></code></li><li><code><link></link></code></li><li><code><link/></code></li></ol><h2 id="60c7">Question 3:</h2><p id="8200">A <code><b>link</b></code> tag is always a child of:</p><ol><li><code><head></head></code></li><li><code><body></body></code></li></ol><h2 id="0ad7">Question 4:</h2><p id="fa09">A <code><b>link</b></code> tag can have content.</p><ol><li>True</li><li>False</li></ol><h2 id="8d9c">Question 5:</h2><p id="42da">A <code><b>link</b></code> tag can never have children tags.</p><ol><li>True</li><li>False</li></ol><h2 id="a768">Question 6:</h2><p id="b621">A <code><b>link</b></code> tag can have a <i>closing</i> tag.</p><ol><li>True</li><li>False</li></ol><h2 id="86cb">Question 7:</h2><p id="a165">Which tag is a self-closing tag?</p><ol><li><code><link></code></li><li><code><link/></code></li></ol><h2 id="b5f1">Question 8:</h2><p id="e762">A <code><b>link</b></code> tag must be a self-closing tag.</p><ol><li>True</li><li>False</li></ol><h2 id="ebd7">Question 9:</h2><p id="6fef">Which attribute is NOT required by a <code><b>link</b></code> tag to import an external CSS file?</p><ol><li><code>rel</code></li><li><code>href</code></li><li><code>style</code></li></ol><h2 id="a249">Question 10:</h2><p id="e54c">The <code>rel</code> attribute should equal to:</p><ol><li><code>“style”</code></li><li><code>“stylesheet”</code></li><li><code>“CSS”</code></li></ol><h2 id="53a6">Question 11:</h2><p id="e6dd">The <code>href</code> attribute should equal to:</p><ol><li><code>“CSS”</code></li><li><code>“stylesheet”</code></li><li><code>“[URL to an external CSS file]”</code></li></ol><h2 id="11d9">Question 12:</h2><p id="35e2">What tag is used to include an <i>internal</i> stylesheet?</p><ol><li><code><link></code></li><li><code><style></style></code></li><li><code><head></head></code></li></ol><h2 id="fa2d">Question 13:</h2><p id="a19d">The <code><b>style</b></code> tag is always a child of:</p><ol><li><code><head></head></code></li><li><code><body></body></code></li><li><code><link></code></li></ol><h2 id="1c86">Question 14:</h2><p id="3f1b">A <code><b>style</b></code> tag must have a <i>closing</i> tag.</p><ol><li>True</li><li>False</li></ol><h2 id="8557">Question 15:</h2><p id="9dab">What kind of content is written inside a <code><b>style</b></code> tag?</p><ol><li>any text</li><li>CSS code</li><li>children tags</li></ol><h2 id="ea0a">Question 16:</h2><p id="06c5">What <b>attribute</b> is used to add <i>inline</i> CSS to any tags?</p><ol><li><code>rel</code></li><li><code>href</code></li><li><code>style</code></li></ol><h2 id="2353">Question 17:</h2><p id="caad">The <code>style</code> attribute should equal to:</p><ol><li><code>"css"</code></li><li><code>“stylesheet”</code></li><li><code>“[CSS declaration statements]”</code></li></ol><h2 id="d611">Question 18:</h2><p id="bede">Which is an empty element?</p><ol><li><code><head></head></code></li><li><code><style></style></code></li><li><code><link></code></li></ol><h2 id="81db">Question 19:</h2><p id="cb13">Attributes always have the form:</p><ol><li><code>name=value</code></li><li><code>name=”value”</code></li></ol><h2 id="9603">Question 20:</h2><p id="edf9">What is CSS Specificity?</p><ol><li>Priority is given to the CSS property that is <i>least</i> specific.</li><li>Priority is given to the CSS property that is <i>most</i> specific.</li></ol><h2 id="5bf7">Question 21:</h2><p id="b42d">Which method has the <i>highest</i> CSS priority?</p><ol><li>external</li><li>internal</li><li>inline</li></ol><h2 id="1517">Question 22:</h2><p id="d731">Which method has the <i>lowest</i> CSS priority?</p><ol><li>external</li><li>internal</li><li>inline</li></ol><h2 id="2a88">Question 23:</h2><p id="1be5">Which is NOT a benefit of an <i>external</i> stylesheet?</p><ol><li>All the CSS code is located in one file.</li><li>It is slower for the browser to fetch the external file.</li></ol><h2 id="de1e">Question 24:</h2><p id="a60e">Which is NOT a benefit of an <i>internal</i> stylesheet?</p><ol><li>All the CSS code is located in one place.</li><li>Designers and copywriters will need to edit the same file.</li></ol><h2 id="368e">Question 25:</h2><p id="b06b">Which is NOT a benefit of <i>inline</i> CSS?</p><ol><li>We can style each HTML tag individually.</li><li>The CSS code commingles with the HTML code.</li></ol><h2 id="e2d2">Question 26:</h2><p id="5e54"><b>Separation of concerns</b> minimizes team members from editing the same files at the same time.</p><ol><li>True</li><li>False</li></ol><h2 id="9a60">Question 27:</h2><p id="8030">Which method achieves the <i>most</i> separation of concerns?</p><ol><li>inline</li><li>internal</li><li>external</li></ol><h2 id="0e5d">Question 28:</h2><p id="345b">Which method has the <i>least</i> separation of concerns?</p><ol><li>external</li><li>internal</li><li>inline</li></ol><h2 id="1406">Question 29:</h2><p id="1935">In practice, what is the best method for a development team?</p><ol><li>external</li><li>internal</li><li>inline</li></ol><figure id="65ab"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Twlkcbwl6ilLRaXucZbk1w.png"><figcaption><a href="https://quiz.siliconwat.com/?lang=en#frontend-chapter4">https://quiz.siliconwat.com/?lang=en#frontend-chapter4</a></figcaption></figure><p id="30c3"><a href="https://frontend.siliconwat.com/#learn-chapter4">Programming Concept Quiz for Chapter 4</a></p><h1 id="5a6f">Coding Exercises</h1><p id="28d1">Check out my <a href="https://frontend.siliconwat.com/#practice-chapter4"><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-chapter4">Interactive Coding Exercises for Chapter 4</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-chapter4"><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="56cb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*LM2bVcZ0grBo_cz_jfA1eQ.png"><figcaption><a href="https://flashcard.siliconwat.com/?lang=en#frontend-chapter4">https://flashcard.siliconwat.com/?lang=en#frontend-chapter4</a></figcaption></figure><p id="4d35">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="f5f1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*RnVoy-t5s6aCzKeD7cYebA.png"><figcaption><a href="https://flashcard.siliconwat.com/?lang=en#frontend-chapter4">https://flashcard.siliconwat.com/?lang=en#frontend-chapter4</a></figcaption></figure><p id="20ad"><a href="https://frontend.siliconwat.com/#review-chapter4">Syntax Flashcard Game for Chapter 4</a></p><h1 id="2ded">Next Steps</h1><p id="b323">Congrats on completing <b>Unit 1:</b> <b>Lesson 4 of 5</b>! 🎉</p><div id="9f05"><pre><span class="hljs-attribute">Unit</span> <span class="hljs-number">1</span>: <span class="hljs-number">80</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">4</span>% Completed</pre></div><p id="4da0"><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> JavaScript code via <b>three</b> different methods as well. To import <b>external</b> JavaScript code, we use the <code><b>script</b></code> tag and the <code>src</code> attribute. To include <b>internal</b> JavaScript code, we use the <i>content</i> of the <code><b>script</b></code> tag. To include <b>inline</b> JavaScript code, we can add <b>event listeners</b> as <i>attributes</i>. This lesson will go over these methods in detail!</p><p id="53ec"><b><a href="https://readmedium.com/chapter-5-importing-javascript-code-e53b86d2e8b6">Chapter 5: Importing JavaScript 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 4: Importing CSS Code

A Complete Frontend Developer Textbook for Beginners (2023 Edition)

This is the textbook version of Lesson 4 of 100 from the Udemy video course: A Complete Frontend Developer Course for Beginners

Chapter 3: Tree Structures in HTML, CSS and JavaScript

Table of Contents

Overview

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

HTML

  1. link
  2. style

CSS

  1. attribute
  2. style (attribute)

JS

None.

This lesson begins with a fresh Codepen. Open a new one and code along with me to increase retention!

Lecture

In this lesson, we explore three different ways to include CSS code in our HTML document.

External CSS

The first method is called external.

Method 1: External CSS

When we write our CSS code in this window on Codepen:

CSS Window (Codepen 1):
body {
   background: orange;
}
UI Window (Codepen 1):
[background is orange]

We are really writing our CSS code into a separate file.

https://codepen.io/thonly/pen/ExZJXKg

To demonstrate this, let’s create another Codepen file.

CSS Window (Codepen 2):
[blank]
UI Window (Codepen 2):
[background is black]

Now let’s move our CSS code over to this new file.

CSS in Codepen 1 => CSS in Codepen 2 (external file)

Select all then Cut.

CSS Window (Codepen 1):
[blank]

Notice the orange background is gone.

UI Window (Codepen 1):
[background is black]

Then in our external file (Codepen 2), we paste:

CSS Window (Codepen 2):
body {
   background: orange;
}
UI Window (Codepen 2):
[background is orange]

To import this external CSS file to Codepen 1:

  1. First we copy the URL in Codepen 2.
  2. Then we go to our Settings in Codepen 1.
  3. Under CSS, scroll down to Add External Stylesheets/Pens.
  4. Then paste in the URL link.
  5. Press Save & Close.
https://codepen.io/thonly/pen/OJWGgPo?editors=1111

Now notice the orange background is back!

UI Window (Codepen 1):
[background is orange]

What happened?

What did Codepen do under the hood to make this possible?

The answer is by using a special tag called a link tag.

<link>

Basically, to import any external CSS file, all we have to do is create a link tag inside the head tag.

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

A link tag will never have any content so a closing link tag is not necessary.

<link></link> => <link>

For readability, we can make the link tag a self-closing tag by adding a backlash like this:

<link />

This is also optional and is not required.

<link /> => <link>

To import our external CSS file, we must give our link tag two attributes like this:

<link rel="stylesheet">

rel=“stylesheet” indicates that its relationship to the document is a stylesheet.

And:

<link rel="stylesheet" href="paste link here">

href stands for Hypertext Reference.

Altogether, the HTML code looks like this:

<html>
 <head>
  <link rel="stylesheet" href="https://codepen.io/thonly/pen/ExZJXKg">
 </head>
 <body></body>
</html>

The finished Codepen:

Internal CSS

We have just learned that the first method to include CSS code in our document is to import it using a link tag inside the head.

HTML Window:
<html>
   <head>
      <link rel="stylesheet" href="https://codepen.io/thonly/pen/ExZJXKg">
   </head>
   <body></body>
</html>

We also learned that Codepen uses this method for the CSS code we write in this CSS window:

CSS Window:
[blank]

The second method to include CSS code in our document is called internal.

Method 2: Internal CSS

To do this, we use another special tag called a style tag inside the head.

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

Unlike the link tag, the style tag has content and therefore we must include a closing style tag.

<style></style>

What is its content?

CSS code.

For example, let’s change the background to yellow.

<style>
   body {
      background: yellow;
   }
</style>

There it is:

UI Window:
[background changed to yellow]

Notice that our external CSS code and our internal CSS code both want to set the color of the background, but priority is given to a background of yellow instead of orange.

Why is that?

We will answer this question in the next lecture.

The finished Codepen:

Inline CSS

The third and final method to include CSS code in our document is called inline.

Method 3: Inline CSS

On the tag that we want to include CSS code, we can simply add an attribute called style:

HTML Window:
<html>
   <head></head>
   <body style ></body>
</html>

Then, we set it equal to the CSS declarations we want to apply to the said body tag.

For example:

HTML Window:
<html>
   <head></head>
   <body style="background: skyblue;"></body>
</html>?

Don’t forget to surround the declarations with quotes!

The CSS body selector is not necessary here because it’s already implied by its presence inside the body bag.

<body style="background: skyblue;"></body>

Notice that there are now three different locations where the CSS rulesets want to set the color of the background, yet in this case, priority is given to our inline CSS code.

UI Window:
[background changed to sky blue]

Why is that?

The answer is called CSS Specificity which basically states that priority will be given to the CSS property that is more specific.

You may have guessed it by now: inline CSS is more specific than internal CSS which is more specific than external CSS.

Inline CSS > Internal CSS > External CSS

In other words, inline CSS is the most specific and has the highest priority.

Inline CSS > Internal CSS > External CSS

While external CSS is the least specific and has the lowest priority.

Inline CSS > Internal CSS > External CSS

CSS Specificity has many other nuances which we will study in detail in Unit 3.

The finished Codepen:

Summary

There are three ways to include CSS code in our HTML document.

Method 1: External CSS
Method 2: Internal CSS
Method 3: Inline CSS

External CSS

The first method is called external which uses the link tag inside the head.

https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#external_stylesheet

link tags do not have content so a closing tag is not necessary.

<link></link> => <link>

Formally, they are known as empty elements because they do not have any children.

https://developer.mozilla.org/en-US/docs/Glossary/Empty_element

In other words, we can think of empty elements like parents who cannot have children.

Empty elements => Cannot have children

Instead, link tags have attributes that we can specify.

https://developer.mozilla.org/en-US/docs/Glossary/Attribute

Attributes always have the form:

name="value"

And are always written inside of opening tags:

<tag name="value">

For example, to import an external CSS file, we specify a relationship attribute and a hypertext reference attribute:

<link rel="stylesheet" href="styles.css">

Internal CSS

The second method to include CSS code is called internal which uses the style tag inside the head.

https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#internal_stylesheet

Unlike the link tag, the style tag has content and therefore must include a closing tag.

<style></style>

The content is CSS code.

<style>
   CSS code goes here
</style>

In other words, we can think of non-empty elements like parents who can have children.

Non-empty elements => Can have children

Inline CSS

Finally, the third method to include CSS code is called inline which uses the style attribute on the tag that we want to style.

https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_is_structured#inline_styles

The value of this attribute is CSS declaration statements.

<p style="color:red;">This is my first CSS example</p>

CSS Specificity

When all three methods want to set the same CSS properties, priority will be given to the ruleset that is more specific.

More Specific Ruleset > Less Specific Ruleset

For example, inline CSS is more specific than internal CSS.

Inline CSS > Internal CSS

And internal CSS is more specific than external CSS.

Internal CSS > External CSS

This is called CSS Specificity.

https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

Again, we will study CSS Specificity in great detail in Unit 3.

Why would we use one method over another?

The answer is that each method has both pros and cons.

External CSS is great because we can contain all the CSS code in one file.

External CSS => Separate File

Which we can give to the Designers in our team.

CSS File => Designers

Our Designers can simply focus on and edit this one CSS file while our Copywriters can focus on and edit the HTML file and nothing else.

Designers: CSS File
Copywriters: HTML File

The drawback is that because it’s a separate file, it’s slower for the browser to go fetch the extra CSS file.

Browser => fetching... => CSS File

Inline CSS is great because we can style each tag individually in our HTML document.

Inline CSS => can style each HTML tag

However, the drawback is that our CSS code commingles with our HTML code which can be confusing for both our Designers and our Copywriters to edit.

Inline CSS: CSS code commingles with HTML code

Internal CSS is great because it’s similar to external CSS in that all the CSS code is located in one place.

<html>
   <head>
      <style>
         CSS code
      </style>
   </head>
   <body></body>
</html>

Yet, it still exists within the same HTML file.

Therefore, it’s faster than external CSS.

Speed: Internal CSS > External CSS

However, similar to inline CSS, internal CSS is in the same file as our HTML code.

HTML File: HTML Code + CSS Code

Hence, our Designers and our Copywriters will have to edit the same file when making changes. This can cause confusions and bugs.

HTML File: Copywriters + Designers

The best practice, therefore, is actually external CSS because it creates “separation of concerns” which minimizes team members from editing the same files at the same time.

https://developer.mozilla.org/en-US/docs/Glossary/MVC

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-chapter4

Sample Quiz Questions for Lesson 4:

Question 1:

Which is NOT a method to include CSS code in the HTML document?

  1. inline
  2. online
  3. internal
  4. external

Question 2:

What tag is used to import an external CSS file?

  1. <head></head>
  2. <link></link>
  3. <link/>

Question 3:

A link tag is always a child of:

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

Question 4:

A link tag can have content.

  1. True
  2. False

Question 5:

A link tag can never have children tags.

  1. True
  2. False

Question 6:

A link tag can have a closing tag.

  1. True
  2. False

Question 7:

Which tag is a self-closing tag?

  1. <link>
  2. <link/>

Question 8:

A link tag must be a self-closing tag.

  1. True
  2. False

Question 9:

Which attribute is NOT required by a link tag to import an external CSS file?

  1. rel
  2. href
  3. style

Question 10:

The rel attribute should equal to:

  1. “style”
  2. “stylesheet”
  3. “CSS”

Question 11:

The href attribute should equal to:

  1. “CSS”
  2. “stylesheet”
  3. “[URL to an external CSS file]”

Question 12:

What tag is used to include an internal stylesheet?

  1. <link>
  2. <style></style>
  3. <head></head>

Question 13:

The style tag is always a child of:

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

Question 14:

A style tag must have a closing tag.

  1. True
  2. False

Question 15:

What kind of content is written inside a style tag?

  1. any text
  2. CSS code
  3. children tags

Question 16:

What attribute is used to add inline CSS to any tags?

  1. rel
  2. href
  3. style

Question 17:

The style attribute should equal to:

  1. "css"
  2. “stylesheet”
  3. “[CSS declaration statements]”

Question 18:

Which is an empty element?

  1. <head></head>
  2. <style></style>
  3. <link>

Question 19:

Attributes always have the form:

  1. name=value
  2. name=”value”

Question 20:

What is CSS Specificity?

  1. Priority is given to the CSS property that is least specific.
  2. Priority is given to the CSS property that is most specific.

Question 21:

Which method has the highest CSS priority?

  1. external
  2. internal
  3. inline

Question 22:

Which method has the lowest CSS priority?

  1. external
  2. internal
  3. inline

Question 23:

Which is NOT a benefit of an external stylesheet?

  1. All the CSS code is located in one file.
  2. It is slower for the browser to fetch the external file.

Question 24:

Which is NOT a benefit of an internal stylesheet?

  1. All the CSS code is located in one place.
  2. Designers and copywriters will need to edit the same file.

Question 25:

Which is NOT a benefit of inline CSS?

  1. We can style each HTML tag individually.
  2. The CSS code commingles with the HTML code.

Question 26:

Separation of concerns minimizes team members from editing the same files at the same time.

  1. True
  2. False

Question 27:

Which method achieves the most separation of concerns?

  1. inline
  2. internal
  3. external

Question 28:

Which method has the least separation of concerns?

  1. external
  2. internal
  3. inline

Question 29:

In practice, what is the best method for a development team?

  1. external
  2. internal
  3. inline
https://quiz.siliconwat.com/?lang=en#frontend-chapter4

Programming Concept Quiz for Chapter 4

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 4

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-chapter4

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-chapter4

Syntax Flashcard Game for Chapter 4

Next Steps

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

Unit 1: 80% 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: 4% Completed

Join Remote Frontend Cohort Program

Next Lesson

In the next lesson, we will learn how to import JavaScript code via three different methods as well. To import external JavaScript code, we use the script tag and the src attribute. To include internal JavaScript code, we use the content of the script tag. To include inline JavaScript code, we can add event listeners as attributes. This lesson will go over these methods in detail!

Chapter 5: Importing JavaScript 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
Front End Development
Programming
Recommended from ReadMedium