or the <b><i>hexadecimal</i></b> <b>system</b>.</p><div id="03e9"><pre><span class="hljs-attribute">Base</span> <span class="hljs-number">16</span>: <span class="hljs-number">0</span> -> <span class="hljs-number">9</span> and a -> f = <span class="hljs-number">16</span> possibilities</pre></div><p id="8fb5">Using this system, we only need <b>2 digits</b> to account for all 256 possibilities:</p><div id="4105"><pre><span class="hljs-symbol">16 </span>x <span class="hljs-number">16</span> = <span class="hljs-number">256</span> possibilities </pre></div><p id="71e8">This means we only need <b>6 digits</b> total to represent any color:</p><div id="84fa"><pre>Hexadecimal <span class="hljs-keyword">System</span>:</pre></div><div id="a526"><pre>R: <span class="hljs-number">2</span> <span class="hljs-keyword">digits</span>
G: <span class="hljs-number">2</span> <span class="hljs-keyword">digits</span>
B: <span class="hljs-number">2</span> <span class="hljs-keyword">digits</span></pre></div><p id="2aeb" type="7">Let’s do some examples!</p><p id="8271">To get a <i>perfect</i> <b>red</b>, we write:</p><ul><li><code>ff</code> for <b>red</b> (which is 255 in the decimal system)</li><li><code>00</code> for <b>green</b> (which is 0 in the decimal system)</li><li><code>00</code> for <b>blue</b> (which is 0 in the decimal system)</li></ul><div id="fd18"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: ff0000;
}</pre></div><p id="c56c">To let CSS know that we’re using the <b><i>hexadecimal</i></b> <b>system</b>, we <b><i>prefix</i></b> it with the <b>pound</b> <b>symbol</b>:</p><div id="18df"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#ff0000</span>;
}</pre></div><p id="938b">To get a <i>perfect</i> <b>blue</b>, we write <code>#</code>:</p><ul><li><code>00</code> for <b>red</b></li><li><code>00</code> for <b>green</b></li><li><code>ff</code> for <b>blue</b></li></ul><div id="dea5"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0000ff</span>;
}</pre></div><p id="b2a4">To get <i>pure</i> <b>black</b>, we write <code>#</code> and <code>00</code> for everything:</p><div id="4a62"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#000000</span>;
}</pre></div><p id="22fd">To get <i>pure</i> <b>white</b>, we write <code>#</code> and <code>ff</code> for everything:</p><div id="a654"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#ffffff</span>;
}</pre></div><p id="af7f">We can also <i>add</i> <b>transparency</b>.</p><p id="dd8e">To make a color <b><i>fully</i></b> <b>transparent</b>, add <code>00</code> at the end:</p><div id="fbac"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#ffffff00</span>;
}</pre></div><p id="4802">To make it <b><i>50%</i></b> <b>transparent</b>, add <code>80</code> at the end:</p><div id="4075"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#ffffff80</span>;
}</pre></div><p id="ec13">To make it have <b><i>no</i></b> <b>transparency</b>, add <code>ff</code> at the end:</p><div id="b64e"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#ffffffff</span>;
}</pre></div><p id="6425">Finally, there’s even a shorter <b>shorthand</b> we can use!</p><p id="bbdd">If <b>both digits</b> are <i>identical</i> <i>for each value</i>, we can just <b><i>drop</i></b><i> one of them</i>:</p><div id="158c"><pre>R: <span class="hljs-function"><span class="hljs-params">ff</span> =></span> f
G: <span class="hljs-function"><span class="hljs-params">ff</span> =></span> f
B: <span class="hljs-function"><span class="hljs-params">ff</span> =></span> f
A: <span class="hljs-function"><span class="hljs-params">ff</span> =></span> f</pre></div><p id="3ec2"><code><b>A</b></code> represents the <b>Alpha</b> <b>channel</b> which is the formal name for <b><i>transparency</i></b>.</p><div id="bb6e"><pre><span class="hljs-attribute">A</span> <span class="hljs-operator">=</span> Alpha channel <span class="hljs-operator">=</span> transparency</pre></div><p id="7bdf">In practice, we may be more <i>intuitive</i> to think of the <b>alpha channel</b> as the <b><i>opacity</i></b> instead.</p><div id="dd03"><pre><span class="hljs-attribute">A</span> <span class="hljs-operator">=</span> opacity</pre></div><p id="7056"><b>Transparency</b> and <b>opacity</b> are <i>opposite</i> terms that describe the ability to see through an object. Greater <b><i>transparency</i></b> means it is <i>easier</i> to see through something. Greater <b><i>opacity</i></b> means it is <i>harder</i> to see through it.</p><div id="c492"><pre>Low opacity (A <span class="hljs-operator">=</span> <span class="hljs-number">0</span>%) <span class="hljs-operator">=</span>> High transparency
High opacity (A <span class="hljs-operator">=</span> <span class="hljs-number">100</span>%) <span class="hljs-operator">=</span>> Low transparency</pre></div><blockquote id="6dcf"><p>In other words, when something is <b>opaque</b>, light cannot pass through it easily. Hence, it is <b>not transparent</b>.</p></blockquote><p id="3d94">The <b>shorthand</b>:</p><div id="0328"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#ffff</span>;
}</pre></div><p id="5ca6">The finished <a href="https://codepen.io/thonly/pen/BaRjYBq?editors=1100">Codepen</a>:</p>
<figure id="b514">
<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%2FBaRjYBq%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DBaRjYBq&display_name=CodePen&url=https%3A%2F%2Fcodepen.io%2Fthonly%2Fpen%2FBaRjYBq%3Feditors%3D1100&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=codepen" allowfullscreen="" frameborder="0" height="600" width="800">
</div>
</div>
</figure></iframe></div></div></figure><h2 id="4f40">HSL Color System</h2><p id="f8a8">Using the <b>RGB</b> color system, to change the <b>color</b> of a pixel on a computer screen, we simply <b><i>vary</i></b> <b><i>the amount</i></b> of the <b>3 primary colors of light</b>.</p><div id="2ef8"><pre><span class="hljs-built_in">Red</span> <span class="hljs-operator">+</span> <span class="hljs-built_in">Green</span> <span class="hljs-operator">+</span> <span class="hljs-built_in">Blue</span> <span class="hljs-operator">=</span> <span class="hljs-variable">Any</span> <span class="hljs-variable">Color</span></pre></div><p id="96be">On a <b>color wheel</b>:</p><figure id="f66e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*aqq1qwtW4HY35j02.png"><figcaption><a href="https://tsh.io/wp-content/uploads/2019/12/hsl-color-wheel.png">https://tsh.io/wp-content/uploads/2019/12/hsl-color-wheel.png</a></figcaption></figure><p id="b0ac">They are located at:</p><ul><li><b>Red</b> = 0 degrees</li><li><b>Green</b> = 120 degrees</li><li><b>Blue</b> = 240 degrees</li></ul><p id="d14b">For <b>coders</b> and <b>engineers</b>, this color system is <i>simple</i> and <i>intuitive</i>.</p><div id="b9e8"><pre>Engineers: RGB <span class="hljs-keyword">Color</span> <span class="hljs-keyword">System</span></pre></div><p id="6c0d"><b>Designers</b> and <b>painters</b>, on the other hand, prefer another system that uses a <i>different</i> <i>set</i> <i>of <b>primary colors</b></i>. For them, <b>colors</b> are produced by <b><i>mixing</i></b>:</p><div id="f89a"><pre><span class="hljs-variable">Designers</span><span class="hljs-operator">:</span> <span class="hljs-built_in">Yellow</span> <span class="hljs-operator">+</span> <span class="hljs-built_in">Cyan</span> <span class="hljs-operator">+</span> <span class="hljs-built_in">Magenta</span></pre></div><p id="97e2">On the <b>color wheel</b>, they are the <b><i>complementary</i></b> <b>colors of light</b> located at:</p><ul><li><b>Yellow</b> = 60 degrees</li><li><b>Cyan</b> = 180 degrees</li><li><b>Magenta</b> = 300 degrees</li></ul><p id="6279">Using this system, to create <i>different</i> <b><i>shades</i></b> of the same <b>Hue</b>, painters can simply <b><i>vary</i></b><i> the amount</i> of <b>white</b> <b>paint</b> being added. This <b><i>amount</i></b> of <b>white</b> <b>paint</b> is referred to as the <b>Lightness</b> factor.</p><div id="74fb"><pre><span class="hljs-attribute">Lightness</span> <span class="hljs-operator">=</span> White</pre></div><p id="5d5f"><b>Computer printers</b> also use this system of colors. However, instead of using <b><i>white</i></b> to create different shades, printers use <b>black</b> instead. The <b><i>amount</i></b> of <b>black</b> <b>ink</b> being added is referred to as the <b>Saturation</b> factor.</p><div id="2498"><pre><span class="hljs-attribute">Saturation</span> <span class="hljs-operator">=</span> Black</pre></div><p id="48d4" type="7">Let’s do some examples!</p><p id="ea29">To tell CSS that we are using the <b>Hue-Saturation-Lightness</b> system of colors, we write:</p><div id="14a5"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">hsl</span>();
}</pre></div><p id="5cd1">Then, to get <i>perfect</i> <b>yellow</b> for example, we write:</p><ul><li><code>60</code> degrees for the <b><i>yellow</i></b> <b>Hue</b> on the color wheel</li><li><code>100%</code> for the <b>Saturation</b> which means <i>do</i> <i>not</i> add any <i>black</i> “ink”</li><li><code>50%</code> for the <b>Lightness</b></li></ul><div id="6822"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">hsl</span>(<span class="hljs-number">60deg</span>, <span class="hljs-number">100%</span>, <span class="hljs-number">50%</span>);
}</pre></div><p id="c293" type="7">Why 50%?</p><p id="43ae"><b>Below</b> 50%:</p><div id="a317"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">hsl</span>(<span class="hljs-number">60deg</span>, <span class="hljs-number">100%</span>, <span class="hljs-number">40%</span>);
}</pre></div><p id="2080">Means <b>white </b>“paint” will be <b><i>removed</i></b>.</p><p id="ed0d"><b>Above</b> 50%:</p><div id="abbf"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">hsl</span>(<span class="hljs-number">60deg</span>, <span class="hljs-number">100%</span>, <span class="hljs-number">60%</span>);
}</pre></div><p id="0065">Means <b>white</b> “paint” will be <b><i>added</i></b>.</p><p id="8150"><b>At</b> 50%:</p><div id="9dc7"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">hsl</span>(<span class="hljs-number">60deg</span>, <span class="hljs-number">100%</span>, <span class="hljs-number">50%</span>);
}</pre></div><p id="550b">Means to do <b><i>neither</i></b>.</p><div id="9c12"><pre><span class="hljs-symbol">Lightness:</span> </pre></div><div id="a263"><pre><span class="hljs-number">0</span>% -> <span class="hljs-number">50</span>% -> <span class="hljs-number">100</span>%
<span class="hljs-comment">remove white no white add white</span></pre></div><p id="f591">If we <i>add</i> <b>black</b> “ink” to the <b><i>maximum</i></b> by setting the <b>Saturation</b> to <code>0%</code>:</p><div id="3b01"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">hsl</span>(<span class="hljs-number">60deg</span>, <span class="hljs-number">0%</span>, <span class="hljs-number">50%</span>);
}</pre></div><p id="7ce5">The <b>color</b> will turn <b><i>gray</i></b>.</p><div id="f2ce"><pre><span class="hljs-symbol">Saturation:</span></pre></div><div id="f4ff"><pre><span class="hljs-number">0</span><span class="hljs-meta">%</span> -> <span class="hljs-number">100</span><span class="hljs-meta">%</span>
full <span class="hljs-symbol">none</span></pre></div><p id="9585">Like RGB, we can also add an <b>Alpha</b> channel (<b>transparency</b> factor).</p><p id="a46e">To make a color <b><i>50%</i></b> <b>transparent</b>, we simply add <code>50%</code> at the end like usual:</p><div id="7671"><pre><span class="hljs-selector-tag">main</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">hsl</span>(<span class="hljs-number">60deg</span>, <span class="hljs-number">0%</span>, <span class="hljs-number">50%</span>, <span class="hljs-number">50%</span>);
}</pre></div><blockquote id="e739"><p>It is more precise to use <code>hsla()</code> to include the <b>alpha</b> channel.</p></blockquote><p id="7fcd">The finished <a href="https://codepen.io/thonly/pen/KKmzzyK?editors=1100">Codepen</a>:</p>
<figure id="ccd4">
<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%2FKKmzzyK%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DKKmzzyK&display_name=CodePen&url=https%3A%2F%2Fcodepen.io%2Fthonly%2Fpen%2FKKmzzyK%3Feditors%3D1100&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=codepen" allowfullscreen="" frameborder="0" height="600" width="800">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="6726">Summary</h1><p id="192c">In this lesson, we studied the <b>background-color</b> in more detail.</p><figure id="1827"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rQQJRJB90hbhAW1fmNSr-g.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-color">https://developer.mozilla.org/en-US/docs/Web/CSS/background-color</a></figcaption></figure><p id="d59d">In particular, we explored <b><i>three</i></b> <b>different ways</b> to <i>specify</i> <b>colors</b>.</p><p id="79e6">If we want to use <b>colors</b> that are <b><i>common</i></b>, we can specify <b>keywords</b> such as <code>brown</code>:</p><div id="5524"><pre><span class="hljs-attribute">background-color</span>: brown;</pre></div><p id="281e">For the <b>complete list</b> of keywords, check out this MDN article:</p><figure id="fb52"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*yQP3UQJetxCadEGlrRP3OA.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/color_value">https://developer.mozilla.org/en-US/docs/Web/CSS/color_value</a></figcaption></figure><p id="5bd8">However, if we want to use <b>colors</b> that are <b><i>not common</i></b>, there are <b><i>two</i></b> <b>systems</b> <b>of colors</b> we can utilize:</p><ol><li><b>RGB Color System</b></li><li><b>HSL Color System</b></li></ol><figure id="291c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*aqq1qwtW4HY35j02.png"><figcaption><a href="https://tsh.io/wp-content/uploads/2019/12/hsl-color-wheel.png">https://tsh.io/wp-content/uploads/2019/12/hsl-color-wheel.png</a></figcaption></figure><h2 id="2732">RGB Color System</h2><p id
Options
="b8a1">The <b>RGB</b> color system mirrors the way <b>nature</b> produces colors by <b><i>varying</i></b> the amount of the <b><i>three</i> <i>primary colors</i> </b>of<b> light</b>:</p><ol><li><b>Red</b></li><li><b>Green</b></li><li><b>Blue</b></li></ol><p id="8ee8">For example, a <b><i>pixel</i></b> on a <b>computer screen</b> uses this system to produce color.</p><div id="0ad5"><pre><span class="hljs-variable">Pixel</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">Red</span> <span class="hljs-operator">+</span> <span class="hljs-built_in">Green</span> <span class="hljs-operator">+</span> <span class="hljs-built_in">Blue</span></pre></div><p id="249d">In fact, the <b><i>cone cells</i></b> in<i> </i><b>our eyes</b> also use this system to enable us to see colors.</p><div id="cd95"><pre><span class="hljs-built_in">Cone</span> <span class="hljs-built_in">Cell</span> <span class="hljs-operator">=</span> <span class="hljs-built_in">Red</span> <span class="hljs-operator">+</span> <span class="hljs-built_in">Green</span> <span class="hljs-operator">+</span> <span class="hljs-built_in">Blue</span></pre></div><h2 id="2507">HSL Color System</h2><p id="c236">On the other hand, the <b>HSL</b> color system uses the <b><i>three</i></b><i> <b>complementary</b> <b>colors</b></i> of <b>light</b>:</p><ol><li><b>Yellow</b></li><li><b>Cyan</b></li><li><b>Magenta</b></li></ol><p id="2a0d">With this system, <b>shades</b> can easily be created by <i>adding</i> <b><i>white</i></b> or <b><i>black</i></b> to the<b> Hue</b>.</p><p id="11d7">For example, <b>painters</b> use this system to easily create <i>shades</i> by adding <b><i>white</i></b> or the <b><i>Lightness</i></b> factor.</p><div id="360e"><pre>Hue + <span class="hljs-function"><span class="hljs-params">White</span> =></span> Shades</pre></div><p id="f01b">Computer <b>printers</b> also use this system to easily create <i>shades</i> by adding <b><i>black</i></b> or the <b><i>Saturation</i></b> factor.</p><div id="4778"><pre>Hue + <span class="hljs-function"><span class="hljs-params">Black</span> =></span> Shades</pre></div><blockquote id="724e"><p><b>Painters</b> don’t use <b>black</b> because it’s difficult to mix. <b>Printers</b> don’t use <b>white</b> because the paper is already white.</p></blockquote><h2 id="6ae3">RGB Syntax</h2><p id="e388">To use the <b>RGB</b> color system, we can write it as a <b>function</b>:</p><div id="bcf2"><pre><span class="hljs-function"><span class="hljs-title">rgb</span><span class="hljs-params">(R, G, B, A)</span></span></pre></div><p id="f6aa">Or, as a <b>hexadecimal</b>:</p><div id="ce35"><pre><span class="hljs-meta">#RRGGBBAA</span></pre></div><p id="3899">In the<b> functional notation</b>, <b><i>Red</i></b>, <b><i>Green</i></b>, and <b><i>Blue</i></b> can be a <b>number</b> from <code>0</code> to <code>255</code>, or a <b>percentage</b>.</p><div id="44b4"><pre><span class="hljs-attribute">R</span>: <span class="hljs-number">0</span> -> <span class="hljs-number">255</span> or %
<span class="hljs-attribute">G</span>: <span class="hljs-number">0</span> -> <span class="hljs-number">255</span> or %
<span class="hljs-attribute">B</span>: <span class="hljs-number">0</span> -< <span class="hljs-number">255</span> or %</pre></div><p id="b6f9">The <b>transparency</b> or <b><i>Alpha</i></b> can be a <b>number</b> between <code>0</code> and <code>1</code>, or a <b>percentage</b>.</p><div id="daa9"><pre><span class="hljs-attribute">A</span>: <span class="hljs-number">0</span> -> <span class="hljs-number">1</span> or %</pre></div><p id="43a3">We can <b><i>separate</i></b> these values with a <b>common</b>:</p><div id="018f"><pre><span class="hljs-function"><span class="hljs-title">rgb</span><span class="hljs-params">(R, G, B, A)</span></span></pre></div><p id="374c">Or, just a <b>space</b>:</p><div id="6ed4"><pre><span class="hljs-function"><span class="hljs-title">rgb</span><span class="hljs-params">(R G B A)</span></span></pre></div><p id="a0db">In the <b>hexadecimal</b> <b>notation</b>, we use <b><i>hexadecimal</i></b> <i>characters</i>:</p><ul><li><code>0</code> through <code>9</code></li><li><code>a</code> through <code>f</code></li></ul><p id="bc8e">If <b>all four values</b> and their <b>two digits</b> are the <b><i>same</i></b>, we can <i>drop one</i>:</p><div id="b827"><pre>#RRGGBBAA => #RGBA</pre></div><blockquote id="6600"><p>Again, it is more precise to use <code><i>rgba()</i></code> to include the <b>alpha</b> channel.</p></blockquote><h2 id="7e72">HSL Syntax</h2><p id="1a43">To use the <b>HSL</b> color system, we specify:</p><ul><li><b>Hue</b></li><li><b>Saturation</b></li><li><b>Lightness</b></li><li><b>Alpha</b></li></ul><p id="7681">We can <b><i>separate</i></b> them using a <b>comma</b>:</p><div id="4663"><pre><span class="hljs-function"><span class="hljs-title">hsl</span><span class="hljs-params">(H, S, L, A)</span></span></pre></div><p id="7793">Or, just a <b>space</b>:</p><div id="7808"><pre><span class="hljs-function"><span class="hljs-title">hsl</span><span class="hljs-params">(H S L A)</span></span></pre></div><p id="4d9a">The <b>Hue</b> is expressed as an <b><i>angle</i></b>, the <b>Saturation</b> and <b>Lightness</b> are expressed as <b><i>percentages</i></b>. The <b>Alpha</b> can be expressed as a <b><i>number</i></b><i> </i>between <code>0</code> to <code>1</code>, or a <b><i>percentage</i></b>.</p><div id="26d8"><pre><span class="hljs-symbol">H:</span> angle
<span class="hljs-symbol">S:</span> %
<span class="hljs-symbol">L:</span> %
<span class="hljs-symbol">A:</span> <span class="hljs-number">0</span> -> <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> %</pre></div><blockquote id="66bb"><p>In both color systems, the <b>Alpha</b> channel is <b>optional</b> and the <b>default</b> is <code><i>1</i></code> which is <b>no</b> transparency.</p></blockquote><blockquote id="f3b8"><p>Again, it is more precise to use <code><i>hsla()</i></code> to include the <b>alpha</b> channel.</p></blockquote><h1 id="cdb9">Concept Quiz</h1><p id="c1ed">Take my <a href="https://frontend.siliconwat.com/#learn-chapter11"><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="2c5d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*BsVP2owcb4fCQFbmvwSu0Q.png"><figcaption><a href="https://quiz.siliconwat.com/?lang=en#frontend-chapter11">https://quiz.siliconwat.com/?lang=en#frontend-chapter11</a></figcaption></figure><p id="bdc3"><b><i>Sample</i></b> Quiz Questions for <b>Lesson 11</b>:</p><h2 id="673d">Question 1:</h2><p id="acff">Which CSS property CANNOT set the <b>background-color</b>?</p><ol><li>background</li><li>color</li><li>background-color</li></ol><h2 id="48e9">Question 2:</h2><p id="c57b">Setting a <b>background-color</b> on the <code><b>body</b></code> element will always cover the <i>entire</i> viewport.</p><ol><li>True</li><li>False</li></ol><h2 id="6ab4">Question 3:</h2><p id="6c17">What is NOT a <b>primary color</b>?</p><ol><li>red</li><li>green</li><li>yellow</li><li>blue</li></ol><h2 id="dc18">Question 4:</h2><p id="ecbf">Which will produce <i>pure</i> <b>red</b>?</p><ol><li>rgb(0, 255, 0)</li><li>rgb(255, 0, 0)</li><li>rgb(0, 0, 255)</li></ol><h2 id="e2a1">Question 5:</h2><p id="1a69">Which will produce <i>pure</i> <b>green</b>?</p><ol><li>rgb(100%, 0%, 0%)</li><li>rgb(0%, 100%, 0%)</li><li>rgb(0%, 0%, 100%)</li></ol><h2 id="0abf">Question 6:</h2><p id="8fe5">Which will produce <i>pure</i> <b>blue</b>?</p><ol><li>rgb(0 0 255)</li><li>rgb(255 0 0)</li><li>rgb(0 255 0)</li></ol><h2 id="fa9f">Question 7:</h2><p id="7989">Which will produce <i>pure</i> <b>white</b>?</p><ol><li>rgb(100% 100% 100%)</li><li>rgb(0 0 0)</li></ol><h2 id="d286">Question 8:</h2><p id="8b96">Which will produce <i>pure</i> <b>black</b>?</p><ol><li>rgb(255 255 255)</li><li>rgb(0 0 0)</li></ol><h2 id="5268">Question 9:</h2><p id="8c40">Which will produce <i>perfect</i> <b>transparency</b>?</p><ol><li>rgb(0 0 0 100%)</li><li>rgb(0 0 0 0)</li></ol><h2 id="b642">Question 10:</h2><p id="b907"><b>Hexadecimal</b> <b>notation</b> requires a <code>#</code> prefix.</p><ol><li>True</li><li>False</li></ol><h2 id="01ad">Question 11:</h2><p id="43b1">Which is NOT a <b><i>hexadecimal</i></b> <b>character</b>?</p><ol><li>0</li><li>9</li><li>a</li><li>d</li><li>f</li><li>g</li><li>5</li></ol><h2 id="d4bf">Question 12:</h2><p id="8a75">Which will NOT produce <i>pure</i> <b>red</b>?</p><ol><li>#ff0000</li><li>#f00</li><li>#ff0</li></ol><h2 id="7ba0">Question 13:</h2><p id="0d2d">Which will NOT produce <i>pure</i> <b>green</b>?</p><ol><li>#0f0</li><li>#0ff</li><li>#00ff00</li></ol><h2 id="08bc">Question 14:</h2><p id="c506">Which will NOT produce <i>pure</i> <b>blue</b>?</p><ol><li>#f0f</li><li>#0000ff</li><li>#00f</li></ol><h2 id="40fb">Question 15:</h2><p id="b166">Which will NOT produce <i>pure</i> <b>white</b>?</p><ol><li>#ffffff</li><li>#fff</li><li>#000</li></ol><h2 id="c5e8">Question 16:</h2><p id="edfa">Which will NOT produce <i>pure</i> <b>black</b>?</p><ol><li>#fff</li><li>#000</li><li>#000000</li></ol><h2 id="6d9c">Question 17:</h2><p id="b7a9">Which will NOT produce <i>perfect</i> <b>transparency</b>?</p><ol><li>#0000</li><li>#0000000</li><li>#000f</li></ol><h2 id="ae2a">Question 18:</h2><p id="b146">Which is NOT a <b>complementary</b> <b>color</b>?</p><ol><li>yellow</li><li>green</li><li>cyan</li><li>magenta</li></ol><h2 id="dc19">Question 19:</h2><p id="ead7"><b>Yellow</b> is located at what degree?</p><ol><li>60</li><li>180</li><li>300</li></ol><h2 id="aa1f">Question 20:</h2><p id="8616"><b>Cyan</b> is located at what degree?</p><ol><li>60</li><li>180</li><li>300</li></ol><h2 id="26c9">Question 21:</h2><p id="b982"><b>Magenta</b> is located at what degree?</p><ol><li>60</li><li>180</li><li>300</li></ol><h2 id="085e">Question 22:</h2><p id="764d">What <i>“paint”</i> color does the <b>Lightness</b> factor add or remove?</p><ol><li>white</li><li>black</li></ol><h2 id="9382">Question 23:</h2><p id="70b7">What <i>“ink”</i> color does the <b>Saturation</b> factor add?</p><ol><li>white</li><li>black</li></ol><h2 id="5512">Question 24:</h2><p id="636e">What <b>Lightness</b> value will NOT add any white?</p><ol><li>0%</li><li>50%</li><li>100%</li></ol><h2 id="a4dd">Question 25:</h2><p id="3104">What <b>Saturation</b> value will NOT add any black?</p><ol><li>0%</li><li>50%</li><li>100%</li></ol><h2 id="adaf">Question 26:</h2><p id="b497">Which is NOT a <b>complementary color</b>?</p><ol><li>hsl(60deg 100% 50%)</li><li>hsl(180deg 100% 50%)</li><li>hsl(300deg 100% 100%)</li><li>hsl(300deg 100% 50%)</li></ol><h2 id="d4ce">Question 27:</h2><p id="301f">Which color system uses <b>primary colors</b>?</p><ol><li>RGB</li><li>HSL</li></ol><h2 id="06b9">Question 28:</h2><p id="3b92">Which color system uses <b>complementary colors</b>?</p><ol><li>RGB</li><li>HSL</li></ol><h2 id="4519">Question 29:</h2><p id="0678">Which <b>Alpha</b> value will NOT produce <i>perfect</i> <b>opacity</b> (<i>no</i> <b>transparency</b>)?</p><ol><li>1</li><li>100%</li><li>0</li></ol><figure id="72f4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*QQSZpEs7f1kA4hYvIIFKfw.png"><figcaption><a href="https://quiz.siliconwat.com/?lang=en#frontend-chapter11">https://quiz.siliconwat.com/?lang=en#frontend-chapter11</a></figcaption></figure><p id="ae3d">→ <a href="https://frontend.siliconwat.com/#learn-chapter11">Programming Concept Quiz for Chapter 11</a></p><h1 id="077f">Coding Exercises</h1><p id="28d1">Check out my <a href="https://frontend.siliconwat.com/#practice-chapter11"><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-chapter11">Interactive Coding Exercises for Chapter 11</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-chapter11"><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. 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><p id="20ad">→ <a href="https://frontend.siliconwat.com/#review-chapter11">Syntax Flashcard Game for Chapter 11</a></p><h1 id="eb18">Next Steps</h1><p id="3177">Congrats on completing <b>Unit 2:</b> <b>Lesson 6 of 13</b>! 🎉</p><div id="9f05"><pre><span class="hljs-attribute">Unit</span> <span class="hljs-number">1</span>: <span class="hljs-number">100</span>% Completed
<span class="hljs-attribute">Unit</span> <span class="hljs-number">2</span>: <span class="hljs-number">46</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">11</span>% Completed</pre></div><p id="4e18">→ <a href="https://frontend.siliconwat.org">Join Remote Frontend Cohort Program</a></p><h2 id="c823">Next Lesson</h2><p id="e317">In this lesson, we formally introduced the CSS <b>background</b> property and focused on the <code><b>background-color</b></code>. In the next lesson, we will continue our discussion of the <b>background</b> property by focusing on the <code><b>background-image</b></code>. This will <b><i>complete</i></b> our understanding of the <b>background</b> property as well as <b><i>enable</i></b> us to <i>create</i> some really fancy <b><i>gradient</i> effects </b>in Lesson 13!</p><p id="53ec"><b>→ <a href="https://readmedium.com/chapter-12-css-background-271ae741f2bc">Chapter 12: CSS Background</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 11: CSS Colors in RGB and HSL
A Complete Frontend Developer Textbook for Beginners (2023 Edition)
To be more precise, the background property is actually a shorthand that combinesmany related properties together (which we will cover in depth in the next lesson).
Basically, there are two types of backgrounds:
background-color
background-image
First, let’s look at background-color in greater detail (background-image will be covered in the next lesson).
To set only the background color, we can just write:
Unlike any other HTML elements, the body element is special because even though we set the width and height to be only 75percent of the viewport (the redborder), the greenbackground-color covers all of the viewport:
To make RGB shorter so we can typeless, it is possible to count with base 16 instead of base 10.
Base10:
R: 0 -> 255G: 0 -> 255B: 0 -> 255
Including 0, each of the three primary colors has 256possibilities.
0 -> 255 = 256 possibilities
When we count from from 0 to 9, we get base 10 or the decimalsystem:
Base10: 0 -> 9 = 10 possibilities
Using this system, we need at least 3 digits for each primary color:
DecimalSystem:
R: 3digits
G: 3digits
B: 3digits
Therefore in total, we need at least 9 digits to represent any color:
Total: 3 + 3 + 3 = 9 digits
For example:
rgb(255, 255, 255) => 9 digits
If we instead count from 0 to 9, then a to f, we get base 16 or the hexadecimalsystem.
Base16: 0 -> 9 and a -> f = 16 possibilities
Using this system, we only need 2 digits to account for all 256 possibilities:
16 x 16 = 256 possibilities
This means we only need 6 digits total to represent any color:
Hexadecimal System:
R: 2digits
G: 2digits
B: 2digits
Let’s do some examples!
To get a perfectred, we write:
ff for red (which is 255 in the decimal system)
00 for green (which is 0 in the decimal system)
00 for blue (which is 0 in the decimal system)
main {
background-color: ff0000;
}
To let CSS know that we’re using the hexadecimalsystem, we prefix it with the poundsymbol:
main {
background-color: #ff0000;
}
To get a perfectblue, we write #:
00 for red
00 for green
ff for blue
main {
background-color: #0000ff;
}
To get pureblack, we write # and 00 for everything:
main {
background-color: #000000;
}
To get purewhite, we write # and ff for everything:
main {
background-color: #ffffff;
}
We can also addtransparency.
To make a color fullytransparent, add 00 at the end:
main {
background-color: #ffffff00;
}
To make it 50%transparent, add 80 at the end:
main {
background-color: #ffffff80;
}
To make it have notransparency, add ff at the end:
main {
background-color: #ffffffff;
}
Finally, there’s even a shorter shorthand we can use!
If both digits are identicalfor each value, we can just drop one of them:
R: ff => f
G: ff => f
B: ff => f
A: ff => f
A represents the Alphachannel which is the formal name for transparency.
A= Alpha channel = transparency
In practice, we may be more intuitive to think of the alpha channel as the opacity instead.
A= opacity
Transparency and opacity are opposite terms that describe the ability to see through an object. Greater transparency means it is easier to see through something. Greater opacity means it is harder to see through it.
Low opacity (A =0%) => High transparency
High opacity (A =100%) => Low transparency
In other words, when something is opaque, light cannot pass through it easily. Hence, it is not transparent.
For coders and engineers, this color system is simple and intuitive.
Engineers: RGB ColorSystem
Designers and painters, on the other hand, prefer another system that uses a differentsetof primary colors. For them, colors are produced by mixing:
Designers:Yellow+Cyan+Magenta
On the color wheel, they are the complementarycolors of light located at:
Yellow = 60 degrees
Cyan = 180 degrees
Magenta = 300 degrees
Using this system, to create differentshades of the same Hue, painters can simply vary the amount of whitepaint being added. This amount of whitepaint is referred to as the Lightness factor.
Lightness= White
Computer printers also use this system of colors. However, instead of using white to create different shades, printers use black instead. The amount of blackink being added is referred to as the Saturation factor.
Saturation= Black
Let’s do some examples!
To tell CSS that we are using the Hue-Saturation-Lightness system of colors, we write:
main {
background-color: hsl();
}
Then, to get perfectyellow for example, we write:
60 degrees for the yellowHue on the color wheel
100% for the Saturation which means donot add any black “ink”
50% for the Lightness
main {
background-color: hsl(60deg, 100%, 50%);
}
Why 50%?
Below 50%:
main {
background-color: hsl(60deg, 100%, 40%);
}
Means white “paint” will be removed.
Above 50%:
main {
background-color: hsl(60deg, 100%, 60%);
}
Means white “paint” will be added.
At 50%:
main {
background-color: hsl(60deg, 100%, 50%);
}
Means to do neither.
Lightness:
0% -> 50% -> 100%
remove white no white add white
If we addblack “ink” to the maximum by setting the Saturation to 0%:
main {
background-color: hsl(60deg, 0%, 50%);
}
The color will turn gray.
Saturation:
0% -> 100%
full none
Like RGB, we can also add an Alpha channel (transparency factor).
To make a color 50%transparent, we simply add 50% at the end like usual:
main {
background-color: hsl(60deg, 0%, 50%, 50%);
}
It is more precise to use hsla() to include the alpha channel.
The RGB color system mirrors the way nature produces colors by varying the amount of the threeprimary colorsof light:
Red
Green
Blue
For example, a pixel on a computer screen uses this system to produce color.
Pixel=Red+Green+Blue
In fact, the cone cells inour eyes also use this system to enable us to see colors.
ConeCell=Red+Green+Blue
HSL Color System
On the other hand, the HSL color system uses the threecomplementarycolors of light:
Yellow
Cyan
Magenta
With this system, shades can easily be created by addingwhite or black to the Hue.
For example, painters use this system to easily create shades by adding white or the Lightness factor.
Hue + White => Shades
Computer printers also use this system to easily create shades by adding black or the Saturation factor.
Hue + Black => Shades
Painters don’t use black because it’s difficult to mix. Printers don’t use white because the paper is already white.
RGB Syntax
To use the RGB color system, we can write it as a function:
rgb(R, G, B, A)
Or, as a hexadecimal:
#RRGGBBAA
In the functional notation, Red, Green, and Blue can be a number from 0 to 255, or a percentage.
R: 0 -> 255 or %
G: 0 -> 255 or %
B: 0 -< 255 or %
The transparency or Alpha can be a number between 0 and 1, or a percentage.
A: 0 -> 1 or %
We can separate these values with a common:
rgb(R, G, B, A)
Or, just a space:
rgb(R G B A)
In the hexadecimalnotation, we use hexadecimalcharacters:
0 through 9
a through f
If all four values and their two digits are the same, we can drop one:
#RRGGBBAA => #RGBA
Again, it is more precise to use rgba() to include the alpha channel.
HSL Syntax
To use the HSL color system, we specify:
Hue
Saturation
Lightness
Alpha
We can separate them using a comma:
hsl(H, S, L, A)
Or, just a space:
hsl(H S L A)
The Hue is expressed as an angle, the Saturation and Lightness are expressed as percentages. The Alpha can be expressed as a numberbetween 0 to 1, or a percentage.
H: angle
S: %
L: %
A:0 -> 1or %
In both color systems, the Alpha channel is optional and the default is 1 which is no transparency.
Again, it is more precise to use hsla() to include the alpha channel.
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!
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!
Review what you have learned by playing my Syntax Flashcard Game! These flashcards are designed to help you commit to memory all the newcodesyntaxes you learned in this lesson. 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!
In this lesson, we formally introduced the CSS background property and focused on the background-color. In the next lesson, we will continue our discussion of the background property by focusing on the background-image. This will complete our understanding of the background property as well as enable us to create some really fancy gradient effects in Lesson 13!
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 ❤️