{"id":25065,"date":"2022-11-24T02:44:26","date_gmt":"2022-11-24T10:44:26","guid":{"rendered":"https:\/\/coderpad.io\/?p=25065"},"modified":"2023-06-05T13:49:37","modified_gmt":"2023-06-05T20:49:37","slug":"understanding-the-css-position-property-and-how-it-works-with-example","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/understanding-the-css-position-property-and-how-it-works-with-example\/","title":{"rendered":"Understanding the CSS Position Property and How It Works With Example"},"content":{"rendered":"\n<p>When using HTML to build web pages, elements are arranged after each other vertically by default. You will always want to add styling to either change your page&#8217;s layout or organize its elements so that elements can be positioned horizontally on your screen. This is possible with CSS <a href=\"https:\/\/coderpad.io\/blog\/development\/a-guide-to-css-flexbox\/\"><code>flexbox<\/code><\/a> and CSS <a href=\"https:\/\/coderpad.io\/blog\/development\/a-very-very-in-depth-guide-on-css-grid\/\"><code>grid<\/code><\/a>. However, more is needed to create stunning web pages.<\/p>\n\n\n\n<p>The CSS position property fills in the blanks by allowing you to create an asymmetrical web page where you can place HTML elements wherever you want by detaching each element from the normal flow and other elements, making your web page more appealing and easy to navigate.<\/p>\n\n\n\n<p>In this article, you will learn what the CSS position property means, how it works, the various values and how they can help you properly position items on your webpage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is the CSS position property?<\/h2>\n\n\n\n<p>As the name implies, the CSS <code>position<\/code> property is used to position an element on a web page. It specifies and lets you take an element from your web page&#8217;s normal flow layout and position it wherever you want it to be using other CSS properties like <code>top<\/code>, <code>right<\/code>, <code>bottom<\/code>, and <code>left<\/code>.<\/p>\n\n\n\n<p>The position property has five values that perform different functions, all aimed at helping you position elements. This article will explain how these values work and their differences, making it easy for you to know where and when to use a particular value.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CSS position values<\/h2>\n\n\n\n<p>Here are the five CSS position values:<\/p>\n\n\n\n<p>1. <code>static<\/code><\/p>\n\n\n\n<p>2. <code>relative<\/code><\/p>\n\n\n\n<p>3. <code>absolute<\/code><\/p>\n\n\n\n<p>4. <code>fixed<\/code><\/p>\n\n\n\n<p>5. <code>sticky<\/code><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">selector<\/span> {\n  <span class=\"hljs-attribute\">position<\/span>: static | relative | absolute | fixed | sticky;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To get started, let&#8217;s make use of a parent div with five <code>div<\/code> elements and a paragraph to identify each <code>div<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"parent-container\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"div-item container1\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>This is container One<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"div-item container2\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>This is container Two<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"div-item container3\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>This is container Three<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"div-item container4\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>This is container Four<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"div-item container5\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>This is container Five<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Let&#8217;s style these divs to look like this.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf46051499.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Five divs with the text <strong>This is container One to Four<\/strong> styled in different colors.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">CSS static position<\/h3>\n\n\n\n<p>The CSS <code>static<\/code> position is the default value for the <code>position<\/code> property. It is the position HTML elements have by default. This <code>position<\/code> value means that an element will follow based on the normal flow of the page and will follow the standard positioning rules as expected with or without any CSS styling.<\/p>\n\n\n\n<p>This means when you do not apply the <code>position<\/code> property to an element, it is <code>static<\/code>, meaning most elements you work with are statically positioned. They are not affected by the <code>top<\/code>, <code>right<\/code>, <code>bottom<\/code>, <code>left<\/code>, and <code>z-index<\/code> properties.<\/p>\n\n\n\n<p>In the example below, a position of <code>static<\/code> is applied to the second <code>div<\/code> container, but you will notice that it does not affect the element&#8217;s output.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container2<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#7f0df4<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: static;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf4657a92f.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Five blocks of <strong>This is container<\/strong> in a static position.<\/figcaption><\/figure>\n\n\n\n<p>Nothing is exciting about this position value, but it is worth knowing that it exists \ud83d\ude42. Let&#8217;s explore other position values that change an element&#8217;s position from the default position.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">CSS relative position<\/h3>\n\n\n\n<p>The CSS <code>relative<\/code> position is used to position an element according to the normal flow of the webpage. This position value alone does nothing. For example, if you apply the position of <code>relative<\/code> to the third <code>div<\/code> container.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container3<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ce4dcd<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: relative;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>There will be no change to the element and the entire elements in the web page because It works just like the <code>static<\/code> value with more advantages.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf46a62063.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The relative position alone does not affect how each element is displayed.<\/figcaption><\/figure>\n\n\n\n<p>The advantage of using this property is that it gives you access to directional properties that you can use to move any element without affecting the offset position of the <code>relative<\/code> positioned element. These directional properties include <code>top<\/code>, <code>left<\/code>, <code>right<\/code>, <code>bottom<\/code>, and <code>z-index<\/code>. For example, you can move the element down and towards the right with the <code>top<\/code> and <code>left<\/code> properties.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container3<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ce4dcd<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: relative;\n  <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">50px<\/span>;\n  <span class=\"hljs-attribute\">left<\/span>: <span class=\"hljs-number\">40px<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf46f6cdb5.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The <strong>This is container Three<\/strong> is overlapped on the <strong>This is container Four<\/strong> block.<\/figcaption><\/figure>\n\n\n\n<p>You&#8217;ll notice that the offset does not affect the surrounding elements because the space the element initially occupies is left empty. At this point, you can begin to ask yourself the use of the <code>relative<\/code> position.<\/p>\n\n\n\n<p>Surprisingly, it is not particularly useful because you want to avoid a situation in which other elements are unaffected. This position value can be used to set the <code>z-index<\/code> or specify the stack order of an element, or it can be used as a container for an &#8220;absolutely&#8221; positioned element.<\/p>\n\n\n\n<p>For example, in the preceding example, the third element is stacked on top of the fourth. Adding <code>z-index<\/code> alone to the fourth element will have no effect, but adding a relative position will allow you to stack the element on top of the third.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container4<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ef74ff<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: relative;\n  <span class=\"hljs-attribute\">z-index<\/span>: <span class=\"hljs-number\">1<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf474397c3.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Elements are stacked on each order as a result of the <code>z-index<\/code> property.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">CSS absolute position<\/h3>\n\n\n\n<p>The <code>absolute<\/code> position is used to remove an element from the normal flow of a web page and then position it relative to the element&#8217;s nearest positioned ancestor (any element with a position value that is not <code>static<\/code>).<\/p>\n\n\n\n<p>When an element has an absolute position, the elements around it act as if that element does not exist on the web page. Depending on how the elements were previously arranged, the element stays on top of one another and collapses the available space.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container2<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#7f0df4<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: absolute;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf478f03af.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The <code>absolute<\/code> position value causes the third element to take the place of the second.<\/figcaption><\/figure>\n\n\n\n<p>In the preceding example, the third element takes the place of the second element, leaving the second element to linger. You&#8217;ll also notice that the element is no longer the full width. This is because <code>absolute<\/code> positioned elements have the width set to <code>auto<\/code>.<\/p>\n\n\n\n<p>The absolutely defined element can now be positioned using directional properties such as <code>top<\/code>, <code>left<\/code>, <code>right<\/code>, and <code>bottom<\/code>. It is also important to understand that this element will be positioned closest to the ancestor. This means that the parent container should be relative to serve as a positioned ancestor.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.parent-container<\/span> {\n  \/\/ ...\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ddd<\/span>;\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">2px<\/span> solid <span class=\"hljs-number\">#000<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: relative;\n}\n\n<span class=\"hljs-selector-class\">.container2<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#7f0df4<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: absolute;\n  <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">left<\/span>: <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf47cb951f.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The first element is affected by the <code>top<\/code> and <code>left<\/code> properties.<\/figcaption><\/figure>\n\n\n\n<p>Because the top and left positions were set to the starting point, the element is now positioned at the top of the container. You can use this to place an element in the center or at a specific location on your website.<\/p>\n\n\n\n<p>If the parent container is not set to a <code>relative<\/code> position, the element will be positioned at the top left of the next positioned ancestor, which is now the body of the entire page.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf48316ee4.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\"><strong>This is container Two<\/strong> is moved outside the blocks of elements.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">CSS fixed position<\/h3>\n\n\n\n<p>As the name implies, this fixed position is used to fix an element to a specific position of your web page. It removes an element from the normal flow of the web page and positions it relative to the viewport (the part of the document visible in your browser) rather than the nearest positioned ancestor as absolute.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container4<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ef74ff<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: fixed;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf489784e3.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The fourth element in the container block is fixed.<\/figcaption><\/figure>\n\n\n\n<p>You will use directional properties to specify a position you want the element to be positioned.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container4<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ef74ff<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: fixed;\n  <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">50%<\/span>;\n  <span class=\"hljs-attribute\">left<\/span>: <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf48dcb13c.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The fourth container element is fixed and moved outside the parent container.<\/figcaption><\/figure>\n\n\n\n<p>This is mostly used when you want to move a button to the bottom of your page, such as the <strong>scroll up<\/strong> button. You can use the fixed position if you also want to fix the nav bar to the top of the webpage screen.<\/p>\n\n\n\n<p>When you position an element to be fixed, it will remain in that position even if you scroll through the page, distinguishing it from the absolute position. It also does not leave a blank space as position relative does.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf491e522d.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A GIF showcasing the fourth container fixed to the page as the parent container is scrolled through.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">CSS Sticky Position<\/h3>\n\n\n\n<p>The sticky position is very superb. It is a combination of <code>static<\/code> and <code>fixed<\/code> positions. This is because an element will remain in the normal flow of the web page as you scroll until it gets to the specified position, then it becomes fixed.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container3<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#ce4dcd<\/span>;\n  <span class=\"hljs-attribute\">position<\/span>: sticky;\n  <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the example above, all elements will scroll normally until the third element reaches the parent element&#8217;s top. It then sticks. It behaves like a static element until it gets to a specified position; then, it becomes fixed and appears to stick to that spot.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf49451da6.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The page moves in an elastic manner once scrolled through.<\/figcaption><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f For sticky positioning to work, You must specify at least one of the top, right, bottom, or left properties.<\/p>\n<\/blockquote>\n\n\n\n<p>This is perfect when you have a navbar positioned after your hero section and will like to stick to the top when a user scrolls to the top of the screen. Also, when you have a list of elements with separate headers which you want to stick as the user scrolls, you can achieve this with the sticky position.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf49853eac.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The illustration displays all <code>position<\/code> property values.<\/figcaption><\/figure>\n\n\n\n<p>Here is a demo that shows the use of all position values.<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=235759&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Distinguishing between the position property and CSS layout systems<\/h2>\n\n\n\n<p>The CSS flexbox and grid are layout systems that you can use to define basic and complex layouts. But you can&#8217;t place these elements anywhere you wish because you&#8217;re limited around the main (X) and cross (Y) Axis.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf45fb3eb0.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Illustrating how the CSS flexbox and grid layout systems arrange items on the webpage.<\/figcaption><\/figure>\n\n\n\n<p>With the CSS position property, you can place your content anywhere you wish by <strong>detaching<\/strong> each element from the other elements. In other words, you can move elements freely around your screen as you are not restricted to any axis.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_637cf4601b50e.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The CSS position property detaches each element and can be placed anywhere.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping up<\/h2>\n\n\n\n<p>In this article, you have learned about position property, how it works, and its various values. You have also learned the differences between these values, so you know when to use a particular CSS position property and its value.&nbsp;<\/p>\n\n\n\n<p>Have fun coding!<\/p>\n\n\n\n<p><em>I\u2019m Joel Olawanle, a frontend developer and technical writer interested in making the web accessible to everyone by always looking for ways to give back to the tech community. Follow me and connect with me on <\/em><a href=\"https:\/\/twitter.com\/olawanle_joel\" target=\"_blank\" rel=\"noopener\"><em>Twitter<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using HTML to build web pages, elements are arranged after each other vertically by default. You will always want to add styling to either change your page&#8217;s layout or organize its elements so that elements can be positioned horizontally on your screen. Learn what the CSS position property is and how you can properly position items on your webpage in this article.<\/p>\n","protected":false},"author":1,"featured_media":25159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[62],"keyword-cluster":[],"class_list":["post-25065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=25065"}],"version-history":[{"count":45,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25065\/revisions"}],"predecessor-version":[{"id":32611,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25065\/revisions\/32611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=25065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=25065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=25065"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=25065"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=25065"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=25065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}