CSS - Position
Tags: css, layout, position
Published: 2020-09-02 06:12:27

<!-- wp:paragraph -->
<p>The position CSS property is one of the most important layout properties to know as a beginner web developer. With it, we can control the position and the "place" of the elements in the natural flow of the document, and even it's behavior. The different position values control the behavior of the element in the page, and the top, right, bottom, and left control the location of the element. In addition to them, the z-index property can help us to create a stack of elements, meaning "behind" and "over" each other.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are 4 types of positions:<br><strong>Static elements</strong>: These are all the HTML elements by default, we can say that a static element, is an element that is not positioned.<br><strong>Relative positioned</strong>: These are elements that are positioned as&nbsp;<em>relative</em>, meaning they are still part of the natural document flow, and they take up space, but they can be moved to some other place with the use of top, bottom, left, right, and z-index properties.<br><strong>Absolute positioned</strong>: These are the elements that are positioned as&nbsp;<em>absolute&nbsp;</em>or as&nbsp;<em>fixed</em>. These elements are taken out of the natural flow, which means they do not take space in the document and are positioned by the top, right, bottom, right, and the z-index properties.<br><strong>Sticky positioned</strong>: These are the elements that are positioned as&nbsp;<em>sticky.&nbsp;</em>These elements are a combination of relative and fixed positions. At first, it gets treated as a relative element, until it gets to a certain threshold in the parent container, from that point it will behave as a fixed element and will be stuck to the screen until we move past the parent container element.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>position: static</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is the default value of every HTML element, it is said that statically positioned elements are not positioned. A static element is not affected by the top, right, bottom, left, and z-index properties.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":327,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-22.png?w=1024" alt="" class="wp-image-327"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":4} -->
<h4>position: relative</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This property allows us to take the element and offset it with the top, bottom, left, and right properties without affecting other elements. The element is not taken out of the natural flow of the document, so it takes the space in it as if it was static.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":328,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-23.png?w=1024" alt="" class="wp-image-328"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":4} -->
<h4>position: absolute</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This property will take the element completely out of the document natural flow, so even the space that the element should have in the flow will be taken by other elements.<br>The final position of the element will be determined by the top, left, right, and bottom properties.<br>The position of the element will be relative to the closest positioned ancestor, and if not found any, it will be relative to the HTML root element.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":329,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-24.png?w=1024" alt="" class="wp-image-329"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":4} -->
<h4 id="block-5739c384-674f-406c-a75c-8751098cc7db">position: sticky</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This property will behave like the relative property, it means it has its own place in the natural flow in the document. To make it behave like a sticky element, we have to add also the threshold in which it will behave like it, we do is using the top and bottom properties for parent elements with a scroll in the y-axis and the left and right properties for the parent element that has a scroll in the x-axis.<be> This property will make the element stick to that place in the screen as long as the parent element is visible, or we reached the threshold we declared.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><span style="text-decoration:underline;">Notes</span>:<br>The sticky property is not supported by IE at all, and it has many other conditions that may cause it not to work, so please be aware of the limitations when using this property.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":332,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-26.png?w=1024" alt="" class="wp-image-332"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":4} -->
<h4>position: fixed</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This property acts like the absolute, it takes the element out of the natural flow, and its space gets taken by other elements. This property lets us place an element on a fixed place on the screen, the difference between fixed and absolute is that in fixed, the element stays in the same place even if the user scrolls.<br>The position of the element will be relative to the container which is established by the viewport.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":333,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wingedblackcatcode.files.wordpress.com/2020/08/image-27.png?w=1024" alt="" class="wp-image-333"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>The directional properties</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The directional properties can be categories into 3 groups, horizontal properties - left and right, vertical properties - top and bottom, and the depth property - z-index.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>top property</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The top property lets us specify the vertical position of the positioned element, from the top perspective. It has different effects based on the position value of the element.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>static</strong>: The top property does not affect the element.</li><li><strong>relative</strong>: It controls how much to move the element down (away from the top) relative to the elements' original location.</li><li><strong>absolute and fixed</strong>: It controls the distance between the element's top edge and the top edge of the containing block.</li><li><strong>sticky</strong>: It sets the point for the element where it should become sticky in it's containing block.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>bottom property</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The bottom property lets us specify the vertical position of the positioned element, from the bottom perspective. It has different effects based on the position value of the element.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>static</strong>: The bottom property does not affect the element.</li><li><strong>relative</strong>: It controls how much to move the element up (away from the bottom) relative to the elements' original location.</li><li><strong>absolute and fixed</strong>: It controls the distance between the element's bottom edge and the bottom edge of the containing block.</li><li><strong>sticky</strong>: It sets the point for the element where it should become sticky in it's containing block.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><span style="text-decoration:underline;">Notes:</span><br>In case both top and bottom properties are used, the position property is set to absolute or fixed and the height of the element is not specified, both the top and bottom properties will be used to stretch the element vertically.<br>In any other case, the top property will be used to place the element and the bottom property will be ignored.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>right property</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The right property lets us specify the horizontal position of the positioned element, from the right perspective. It has different effects based on the position value of the element.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>static</strong>: The right property does not affect the element.</li><li><strong>relative</strong>: It controls how much to move the element left (away from the right) relative to the elements' original location.</li><li><strong>absolute and fixed</strong>: It controls the distance between the element's right edge and the right edge of the containing block.</li><li><strong>sticky</strong>: It sets the point for the element where it should become sticky in it's containing block.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>left property</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The left property lets us specify the vertical position of the positioned element, from the top perspective. It has different effects based on the position value of the element.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>static</strong>: The left property has no effect on the element.</li><li><strong>relative</strong>: It controls how much to move the element right (away from the left) relative to the elements' original location.</li><li><strong>absolute and fixed</strong>: It controls the distance between the element's left edge and the left edge of the containing block.</li><li><strong>sticky</strong>: It sets the point for the element where it should become sticky in it's containing block.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><span style="text-decoration:underline;">Notes:</span><br></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In case both left and right properties are used, and the width of the element is not specified, both the left and right properties will be used to stretch the element horizontally.<br>In any other case, the left property will be used if the text direction is LtR and if the text direction is RtL the right property will be used.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Values</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The values for the top, bottom, left and right properties are all the same and have the same effect.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>length</strong>: Can be negative, positive, or null. Usually, the length is a number with a length unit, such as px, rem, vh, etc'</li><li><strong>percentage</strong>: In the case of the top and bottom properties, it represents the percentage of the containing block's height. In the case of the right and left properties, it represents the percentage of the containing block's width.</li><li><strong>auto</strong>: This is the default value for all elements. It behaves differently between relative and absolute positioned elements.<ul><li>In relatively positioned elements, if the property is auto it does not affect the element, and the element is not moved at all.</li><li>In absolute positioned elements, each property controls the position of the element, if one of them is set to auto, then the other will decide on the position, if both elements are set to auto, the element will not be moved in that axis.</li></ul></li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>z-index property</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The z-index property controls the z-axis position of a positioned element and its descendants. The higher the value the more in front the element will be, and will overlap elements with lower z-index values.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This property affects the positioned element in two ways:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>The z-axis position in its stacking context, meaning if it will overlap other elements or be overlapped by other elements.</li><li>Establishes a new stacking context inside the element for its descendant elements.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":5} -->
<h5>Values</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The z-index can receive two types of values, 'auto' and an integer.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>auto</strong>: This is the default value for all the elements. This will give the element the z-index value of the parent element.</li><li><strong>an integer</strong>: This will create a stacking context inside the element and its stack level will be '0' compared to its descendants. It will also determine the elements stack level in its own stack context.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4>This concludes my article that explains the position property, and the top, bottom, left, right ,and the z-index properties and their different values.</h4>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Hope you like it, and stay tuned for my next article soon!</h4>
<!-- /wp:heading -->