6"><pre><span class="hljs-selector-tag">img</span> {
<span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">background</span>: red;
<span class="hljs-attribute">position</span>: fixed;
<span class="hljs-attribute">bottom</span>: <span class="hljs-number">10px</span>;
<span class="hljs-attribute">right</span>: <span class="hljs-number">10px</span>;
}</pre></div><p id="a5c5">If we want to know <i>exactly how far</i> the <code><b>img</b></code> is from the <b>top</b> of the <b>viewport</b> in pixels, we can write:</p><div id="83ac"><pre>JS <span class="hljs-built_in">Window</span>:</pre></div><div id="d761"><pre><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(document.querySelector(“img”).offsetTop)</pre></div><p id="5db3">Hence:</p><div id="3bf6"><pre><span class="hljs-built_in">Console</span> Window:</pre></div><div id="cca5"><pre>345</pre></div><p id="bfe3">For <i>exactly how far</i> from the <b>left</b> of the <b>viewport</b> in pixels, we can write:</p><div id="451e"><pre>JS <span class="hljs-built_in">Window</span>:</pre></div><div id="1653"><pre><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(document.querySelector(“img”).offsetLeft)</pre></div><p id="430d">Hence:</p><div id="e967"><pre><span class="hljs-built_in">Console</span> Window:</pre></div><div id="2b60"><pre>641</pre></div><p id="f896">Per CSS, the <b>origin</b> of the <b>viewport</b> is located at the <b><i>top</i>-<i>left</i> corner</b>. In this vein, the <b><i>offsetLeft</i></b> and <b><i>offsetTop</i></b> form the <b>(x, y)</b> <b>coordinates</b> of an <b><i>element’s</i></b> <b><i>origin</i></b> (which is also at its <i>top-left corner</i>).</p><div id="656a"><pre><span class="hljs-symbol">x:</span> offsetLeft
<span class="hljs-symbol">y:</span> offsetTop</pre></div><p id="b5dc">For this reason, <b><i>offsetRight</i></b> and <b><i>offsetBottom</i></b> are <i>not</i> necessary (and do not exist) because they can be easily calculated indirectly:</p><div id="ba4c"><pre><span class="hljs-attr">offsetRight</span> = innerWidth - <span class="hljs-literal">off</span>setLeft
<span class="hljs-attr">offsetBottom</span> = innerHeight - <span class="hljs-literal">off</span>setTop</pre></div><p id="76be">The finished <a href="https://codepen.io/thonly/pen/eYvxVpv?editors=1111">Codepen</a>:</p>
<figure id="dfb8">
<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%2FeYvxVpv%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DeYvxVpv&display_name=CodePen&url=https%3A%2F%2Fcodepen.io%2Fthonly%2Fpen%2FeYvxVpv%3Feditors%3D1111&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=codepen" allowfullscreen="" frameborder="0" height="600" width="800">
</div>
</div>
</figure></iframe></div></div></figure><blockquote id="2f20"><p>When an element’s <b><i>position</i></b> is <b>not</b> <code>static</code>, it can <b>overlap</b> other elements. This <b>stacking order</b> is controlled by the <b><i>z-index</i></b> which we will study in detail in Lesson 15.</p></blockquote><h2 id="e6e0">Position: Shorthand</h2><p id="3823">We have just seen that when the <b>position</b> of an element is <b><i>not</i></b> <code>static</code>, the <b>top</b>, <b>left</b>, <b>bottom</b>, and <b>right</b> properties take effect:</p><div id="35ce"><pre><span class="hljs-selector-tag">img</span> {
<span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">background</span>: red;
<span class="hljs-attribute">position</span>: absolute;
<span class="hljs-attribute">bottom</span>: <span class="hljs-number">10px</span>;
<span class="hljs-attribute">right</span>: <span class="hljs-number">10px</span>;
}</pre></div><p id="0b17">In our example, the <b>top</b> and <b>left</b> have the <i>default</i> <i>value</i> of <code>auto</code>:</p><div id="c07e"><pre><span class="hljs-selector-tag">img</span> {
<span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">background</span>: red;
<span class="hljs-attribute">position</span>: absolute;</pre></div><div id="d5df"><pre> <span class="hljs-attribute">top</span>: auto;
<span class="hljs-attribute">right</span>: <span class="hljs-number">10px</span>;
<span class="hljs-attribute">bottom</span>: <span class="hljs-number">10px</span>;
<span class="hljs-attribute">left</span>: auto;
}</pre></div><p id="8c96">If we can remember <i>this clockwise order</i>, we can rewrite it using the <code><b>inset</b></code> <b>shorthand</b>:</p><div id="6f45"><pre><span class="hljs-selector-tag">img</span> {
<span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">background</span>: red;
<span class="hljs-attribute">position</span>: absolute;</pre></div><div id="d84d"><pre> inset: <span class="hljs-built_in">auto</span> <span class="hljs-number">10</span>px <span class="hljs-number">10</span>px <span class="hljs-built_in">auto</span>;
}</pre></div><p id="6654">In fact, this shorthand is exactly like the <i>shorthand</i> for <b>padding</b>, <b>margin</b>, and <b>border-width</b>!</p><p id="26fe">Likewise, this means:</p><ul><li>if <b>1 value</b> is specified: that value is applied to <b>all sides</b></li><li>if <b>2 values</b> are specified: the <b><i>first</i></b> value applies to the <b>top</b> and <b>bottom</b>, and the <b><i>second</i></b> value applies to the <b>right</b> and <b>left</b></li><li>if <b>3 values </b>are specified: the <b><i>first</i></b> value applies to the <b>top</b>, the <b><i>second</i></b> value applies to the <b>left</b> and <b>right</b>, and the <b><i>third</i></b> value applies to the <b>bottom</b></li><li>if <b>4 values</b> are specified: those values apply to the <b>top</b>, <b>right</b>, <b>bottom</b>, and <b>left</b>, <i>respectively</i></li></ul><blockquote id="94da"><p>If we want <b>a group of elements</b> to belong together, we should make them all <b>relative to the same corner</b> of the ancestor container or viewport (e.g., <b>top</b> and <b>left</b>). Otherwise, <b>resizing</b> <b>the viewport</b> will cause them to move apart in unintended ways.</p></blockquote><h2 id="7c69">Scrolled Distance</h2><p id="4b84">In our example, the <code><b>nav</b></code> element <b><i>overflows</i></b> within the <code><b>header</b></code>:</p><div id="4ec8"><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"><header></span>
<span class="hljs-section"><nav></span>
<span class="hljs-section"><img></span>
<span class="hljs-section"></nav></span>
<span class="hljs-section"></header></span>
<span class="hljs-section"><main></span><span class="hljs-section"></main></span>
<span class="hljs-section"><footer></span><span class="hljs-section"></footer></span>
<span class="hljs-section"></body></span>
<span class="hljs-section"></html></span></pre></div><p id="bea3">As such, it is also possible to determine <b><i>how far</i> <i>we have scrolled</i></b> within the <code><b>header</b></code>.</p><p id="d68c">To determine <b><i>how far</i></b> from the <b>top</b> (<i>this distance here</i>) in pixels, we can write:</p><div id="3574"><pre><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(document.querySelector(“header”).scrollTop)</pre></div><blockquote id="2a33"><p>The Console is showing 0 because Codepen always refreshes when we write new code.</p></blockquote><p id="9a29">To determine <b><i>how far</i></b> from the <b>left</b> (<i>this distance here</i>) in pixels, we can write:</p><div id="0f29"><pre><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(document.querySelector(“header”).scrollLeft)</pre></div><p id="2eb0">Similarly, <b><i>scrollRight</i></b> and <b><i>scrollBottom</i></b> do not exist because they can also be easily calculated indirectly:</p><div id="96a0"><pre><span class="hljs-keyword">scrollRight </span>= <span class="hljs-keyword">scrollWidth </span>- <span class="hljs-keyword">scrollLeft
</span><span class="hljs-keyword">scrollBottom </span>= <span class="hljs-keyword">scrollHeight </span>- <span class="hljs-keyword">scrollTop</span></pre></div><p id="f137">The finished <a href="https://codepen.io/thonly/pen/RwLBGEq?editors=0101">Codepen</a>:</p>
<figure id="9505">
<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%2FRwLBGEq%3Fdefault-tabs%3Dcss%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DRwLBGEq&display_name=CodePen&url=https%3A%2F%2Fcodepen.io%2Fthonly%2Fpen%2FRwLBGEq%3Feditors%3D0101&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=codepen" allowfullscreen="" frameborder="0" height="600" width="800">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="f4c3">Summary</h1><p id="f927">In this lesson, we learn <b><i>five</i></b> different ways to <b>position</b> <i>any</i> elements.</p><figure id="678e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*JkRLjgIPjpVGpNh5DVaMtg.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/position">https://developer.mozilla.org/en-US/docs/Web/CSS/position</a></figcaption></figure><h2 id="7044">Position Primer</h2><p id="7c13">By default, the <b>position</b> is <code>static</code>:</p><div id="85b1"><pre>position: <span class="hljs-type">static</span>;</pre></div><p id="1d87">This means it is <b><i>positioned</i></b> according to its <b><i>placement</i></b> in the <b>HTML Tree</b>.</p><p id="7c06">If we want, we can choose to make its <b>position</b>:</p><div id="21b3"><pre><span class="hljs-attribute">position</span>: relative;</pre></div><p id="17bd">Which then allows us to decide<b> <i>how far</i></b> from the<i> </i><b>top</b>:</p><div id="2c82"><pre><span class="hljs-attribute">position</span>: relative;
<span class="hljs-attribute">top</span>: <span class="hljs-number">40px</span>;</pre></div><p id="d58e">And <b><i>how far</i></b> from the <b>left</b>:</p><div id="aa4f"><pre><span class="hljs-attribute">position</span>: relative;
<span class="hljs-attribute">top</span>: <span class="hljs-number">40px</span>;
<span class="hljs-attribute">left</span>: <span class="hljs-number">40px</span>;</pre></div><p id="0525">of its <b><i>default</i></b> <code>static</code> <b>position</b>.</p><p id="59b2">We can also choose to make its <code>position</code>:</p><div id="ba17"><pre><span class="hljs-attribute">position</span>: sticky;</pre></div><p id="1ef1">Such that when we scroll, its <b>position</b> remains <b><i>constant</i></b>.</p><p id="3edf">We can also choose to take its position <b><i>out of</i></b> the <b>HTML Tree</b> entirely, and make it <b><i>relative</i></b><i> </i>to its <b>nearest <i>positioned</i> ancestor </b>instead by using:</p><div id="47ef"><pre><span class="hljs-attribute">position</span>: absolute;</pre></div><p id="9fb2">Finally, we can also choose to take its position <b><i>out of</i></b> the <b>HTML Tree</b>, and make it <b><i>relative</i></b><i> </i>to the <b>viewport</b> instead by using:</p><div id="cf85"><pre><span class="hljs-attribute">position</span>: fixed;</pre></div><blockquote id="92d7"><p>Again, when an element’s <b><i>position</i></b> is <b>not</b> <code><i>static</i></code>, it can <b>overlap</b> other elements. This <b>stacking
Options
order</b> is controlled by the <b><i>z-index</i></b> which we will study in detail in Lesson 15.</p></blockquote><blockquote id="9618"><p>And if positions are still confusing, many more examples will be shown in Unit 3.</p></blockquote><p id="3c9e">To get its <b><i>exact</i></b> <b>distance</b> from the <b>top</b> of the <b>viewport</b>, we can use <b><i>offsetTop</i></b> on the element:</p><figure id="9c1e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*9yL1Nga0DRwUn7M468PYdw.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop">https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop</a></figcaption></figure><p id="d16b">To get its <b><i>exact</i> distance</b> from the <b>left</b> of the <b>viewport</b>, we can use <b><i>offsetLeft</i></b> on the element:</p><figure id="0624"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*tDRnAj-E1jIUmCD4MWDTQw.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetLeft">https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetLeft</a></figcaption></figure><blockquote id="3b97"><p>Again, <b>offsetBottom</b> and <b>offsetRight</b> do not exist because they are redundant.</p></blockquote><p id="e55f">Basically, when the <b>position</b> of an element is <b><i>not</i></b> <code>static</code>, the <b>top</b>, <b>right</b>, <b>bottom</b>, and <b>left</b> properties take effect.</p><div id="709b"><pre>fixed, absolute, relative, sticky => <span class="hljs-built_in">top</span>, <span class="hljs-built_in">right</span>, <span class="hljs-built_in">bottom</span>, <span class="hljs-built_in">left</span></pre></div><p id="22fc">If not specified, their <i>default value</i> is <code>auto</code>.</p><figure id="5fc3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*VutkQP6O_LwSDAT8jneicQ.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/inset">https://developer.mozilla.org/en-US/docs/Web/CSS/inset</a></figcaption></figure><p id="f787">If we can remember <b><i>this clockwise order</i></b>, we can use the <code>inset</code> <b>shorthand</b> which works exactly like <i>padding</i>, <i>margin</i>, and <i>border-width</i>.</p><div id="fd0c"><pre>inset: <span class="hljs-built_in">top</span> <span class="hljs-built_in">right</span> <span class="hljs-built_in">bottom</span> <span class="hljs-built_in">left</span></pre></div><p id="c21d">Thus:</p><ul><li>if <b>1 value</b> is specified: that value is applied to <b>all sides</b></li><li>if <b>2 values</b> are specified: the <b><i>first</i></b> value applies to the <b>top</b> and <b>bottom</b>, and the <b><i>second</i></b> value applies to the <b>right</b> and <b>left</b></li><li>if <b>3 values </b>are specified: the <b><i>first</i></b> value applies to the <b>top</b>, the <b><i>second</i></b> value applies to the <b>left</b> and <b>right</b>, and the <b><i>third</i></b> value applies to the <b>bottom</b></li><li>if <b>4 values</b> are specified: those values apply to the <b>top</b>, <b>right</b>, <b>bottom</b>, and <b>left</b>, <i>respectively</i></li></ul><blockquote id="2033"><p>Again, if we want <b>a group of elements</b> to move together even after <b>resizing</b> <b>the</b> <b>viewport</b>, we should make them all <b>relative to the same corner</b> of the ancestor container or viewport.</p></blockquote><h2 id="a7e1">Scrolled Distance</h2><p id="4d2b">Lastly, we can also determine<b><i> how much</i></b> <i>has been</i> <i>scrolled</i>.</p><p id="b710">To determine <b><i>how far</i></b> from the <b>top</b>, we can use <b><i>scrollTop</i></b> on the <b><i>parent</i></b> element:</p><figure id="15ed"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jzxaPnD3OeA2-mvJwPFVtg.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop">https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop</a></figcaption></figure><p id="f0ea">Likewise, to determine <b><i>how far</i></b> from the <b>left</b>, we can use <b><i>scrollLeft</i></b> on the <b><i>parent</i></b> element:</p><figure id="14e4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*9cQTkhbSA1MlGnMb27ZDkw.png"><figcaption><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft">https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft</a></figcaption></figure><blockquote id="bb50"><p>Similarly, <b>scrollBottom</b> and <b>scrollRight</b> do not exist because they are redundant, too.</p></blockquote><h1 id="90f2">Concept Quiz</h1><p id="c1ed">Take my <a href="https://frontend.siliconwat.com/#learn-chapter10"><b>Programming Concept Quiz</b></a> to check your understanding! For every correct cboice, 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="0f86"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rUF-lEcz-S-TPM8WEJhVBw.png"><figcaption><a href="https://quiz.siliconwat.com/?lang=en#frontend-chapter10">https://quiz.siliconwat.com/?lang=en#frontend-chapter10</a></figcaption></figure><p id="8235"><b><i>Sample</i></b> Quiz Questions for <b>Lesson 10</b>:</p><h2 id="fd51">Question 1:</h2><p id="295f">By <i>default</i>, the <b>position</b> is:</p><ol><li>static</li><li>relative</li><li>sticky</li><li>absolute</li><li>fixed</li></ol><h2 id="bcf8">Question 2:</h2><p id="d39f">Which is positioned according to its placement in the HTML Tree?</p><ol><li>relative</li><li>absolute</li><li>fixed</li></ol><h2 id="0220">Question 3:</h2><p id="1540">Which is NOT positioned according to its placement in the HTML Tree?</p><ol><li>static</li><li>relative</li><li>sticky</li><li>absolute</li></ol><h2 id="457d">Question 4:</h2><p id="f1c5">A <b>relative</b> position is <i>relative</i> to:</p><ol><li>static position</li><li>nearest ancestor</li><li>viewport</li></ol><h2 id="6be2">Question 5:</h2><p id="e685">An <b>absolute</b> position is <i>relative</i> to:</p><ol><li>static position</li><li>nearest ancestor</li><li>viewport</li></ol><h2 id="0cad">Question 6:</h2><p id="10b8">An <b>fixed</b> position is <i>relative</i> to:</p><ol><li>static position</li><li>nearest ancestor</li><li>viewport</li></ol><h2 id="e177">Question 7:</h2><p id="1c76">To make an element <b><i>stick</i></b> to its <i>current position</i> while scrolling, we use:</p><ol><li>static</li><li>relative</li><li>sticky</li><li>absolute</li><li>fixed</li></ol><h2 id="8157">Question 8:</h2><p id="429a">To move an element from its <i>default</i> <b>static</b> position, we DO NOT use:</p><ol><li>top</li><li>bottom</li><li>left</li><li>right</li><li>center</li></ol><h2 id="24cd">Question 9:</h2><p id="427f">The <b>inset</b> shorthand follows what order:</p><ol><li>top left bottom right</li><li>top bottom left right</li><li>left right top bottom</li><li>top right bottom left</li></ol><h2 id="3da8">Question 10:</h2><p id="4d2f">To determine <i>how far</i> an element is from the <b>top</b> of the <b>viewport</b>, we use:</p><ol><li>scrollTop</li><li>offsetTop</li></ol><h2 id="a073">Question 11:</h2><p id="cbd8">To determine <i>how far</i> an element is from the <b>left</b> of the <b>viewport</b>, we use:</p><ol><li>scrollLeft</li><li>offsetLeft</li></ol><h2 id="d671">Question 12:</h2><p id="b667">To determine <i>how far</i> we have <b>scrolled</b> from the <b>top</b>, we use:</p><ol><li>scrollTop</li><li>offsetTop</li></ol><h2 id="6c0b">Question 13:</h2><p id="041a">To determine <i>how far</i> we have <b>scrolled</b> from the <b>left</b>, we use:</p><ol><li>scrollLeft</li><li>offsetLeft</li></ol><h2 id="708e">Question 14:</h2><p id="e7a9">We use <b>scrollTop</b> and <b>scrollLeft</b> on the:</p><ol><li>parent</li><li>child</li></ol><figure id="0081"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FREyLfNMjL5-hrj_WdP_YQ.png"><figcaption><a href="https://quiz.siliconwat.com/?lang=en#frontend-chapter10">https://quiz.siliconwat.com/?lang=en#frontend-chapter10</a></figcaption></figure><p id="b592">→ <a href="https://frontend.siliconwat.com/#learn-chapter10">Programming Concept Quiz for Chapter 10</a></p><h1 id="9d2a">Coding Exercises</h1><p id="28d1">Check out my <a href="https://frontend.siliconwat.com/#practice-chapter10"><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-chapter10">Interactive Coding Exercises for Chapter 10</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-chapter10"><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-chapter10">Syntax Flashcard Game for Chapter 10</a></p><h1 id="41c4">Next Steps</h1><p id="fc14">Congrats on completing <b>Unit 2:</b> <b>Lesson 5 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">38</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">10</span>% Completed</pre></div><p id="1cd9">→ <a href="https://frontend.siliconwat.org">Join Remote Frontend Cohort Program</a></p><h2 id="c823">Next Lesson</h2><p id="1f9c">In the next lesson, we will learn how to <i>generate</i> <b><i>any</i></b> <b>color</b> we can imagine! There are <b>two</b> major <b>color systems</b>: the <b>RGB</b> system and the <b>HSL</b> system. We will cover both as well as the <b>HEX</b> shorthand. Moreover, we will also learn how to add <b>transparency</b> to our colors which will help us make some really cool <b><i>layering </i>effects</b> in Lesson 13!</p><p id="53ec"><b>→ <a href="https://readmedium.com/chapter-11-css-colors-78ae715fb028">Chapter 11: CSS Colors in RGB and HSL</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 10: Positioning Elements
A Complete Frontend Developer Textbook for Beginners (2023 Edition)
If we want, we can choose to take the img element out of its placement in the HTML Tree entirely, we can make it relative to its nearestpositionedancestor.
Per CSS, the origin of the viewport is located at the top-left corner. In this vein, the offsetLeft and offsetTop form the (x, y)coordinates of an element’sorigin (which is also at its top-left corner).
x: offsetLeft
y: offsetTop
For this reason, offsetRight and offsetBottom are not necessary (and do not exist) because they can be easily calculated indirectly:
When an element’s position is notstatic, it can overlap other elements. This stacking order is controlled by the z-index which we will study in detail in Lesson 15.
Position: Shorthand
We have just seen that when the position of an element is notstatic, the top, left, bottom, and right properties take effect:
In fact, this shorthand is exactly like the shorthand for padding, margin, and border-width!
Likewise, this means:
if 1 value is specified: that value is applied to all sides
if 2 values are specified: the first value applies to the top and bottom, and the second value applies to the right and left
if 3 values are specified: the first value applies to the top, the second value applies to the left and right, and the third value applies to the bottom
if 4 values are specified: those values apply to the top, right, bottom, and left, respectively
If we want a group of elements to belong together, we should make them all relative to the same corner of the ancestor container or viewport (e.g., top and left). Otherwise, resizingthe viewport will cause them to move apart in unintended ways.
Scrolled Distance
In our example, the nav element overflows within the header:
This means it is positioned according to its placement in the HTML Tree.
If we want, we can choose to make its position:
position: relative;
Which then allows us to decidehow far from thetop:
position: relative;
top: 40px;
And how far from the left:
position: relative;
top: 40px;
left: 40px;
of its defaultstaticposition.
We can also choose to make its position:
position: sticky;
Such that when we scroll, its position remains constant.
We can also choose to take its position out of the HTML Tree entirely, and make it relativeto its nearest positioned ancestor instead by using:
position: absolute;
Finally, we can also choose to take its position out of the HTML Tree, and make it relativeto the viewport instead by using:
position: fixed;
Again, when an element’s position is notstatic, it can overlap other elements. This stacking order is controlled by the z-index which we will study in detail in Lesson 15.
And if positions are still confusing, many more examples will be shown in Unit 3.
To get its exactdistance from the top of the viewport, we can use offsetTop on the element:
If we can remember this clockwise order, we can use the insetshorthand which works exactly like padding, margin, and border-width.
inset: toprightbottomleft
Thus:
if 1 value is specified: that value is applied to all sides
if 2 values are specified: the first value applies to the top and bottom, and the second value applies to the right and left
if 3 values are specified: the first value applies to the top, the second value applies to the left and right, and the third value applies to the bottom
if 4 values are specified: those values apply to the top, right, bottom, and left, respectively
Again, if we want a group of elements to move together even after resizingtheviewport, we should make them all relative to the same corner of the ancestor container or viewport.
Scrolled Distance
Lastly, we can also determine how muchhas beenscrolled.
To determine how far from the top, we can use scrollTop on the parent element:
Similarly, scrollBottom and scrollRight do not exist because they are redundant, too.
Concept Quiz
Take my Programming Concept Quiz to check your understanding! For every correct cboice, 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 the next lesson, we will learn how to generateanycolor we can imagine! There are two major color systems: the RGB system and the HSL system. We will cover both as well as the HEX shorthand. Moreover, we will also learn how to add transparency to our colors which will help us make some really cool layering 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 ❤️