CSS Positioning - Sticky
Published: 2020-10-09 14:08:49

Let's talk a bit more about sticky positioning today.

### Sticky Positioning

  We'll examine how best to use it two examples that will show us the situations we might need to use it also. We have our simple Html document with a navigation bar "main-nav" and some text as content .

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ccp5oj7x2k9z9cfokupc.JPG)

Results in

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jqmn1fq8u2dm49br66wn.JPG)

  We have our content displayed in the correct order and all, but there is an issue here. When you'll try to scroll the page you'll notice the header scrolls with the rest of the content. Let's try and fix it by using fixed positioning.  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gc8tz3x8gqdn0ns6szx7.JPG)

Page rendered

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oyqfnard28ljw5ati37h.JPG)

  Now our header will stick at the top of the screen when we scroll but there is an issue with space between the header and the rest of the content. Let's make another change.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gu3fqfhqbsfyal49mr99.JPG)

And the rendering

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/exjqy30hhqukfptsaqo2.JPG)

  This is a perfect example for fixed position usage. But what if we had another section in our page above our header, how would that look like?

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0dvt5bmbxq9v22uheszd.JPG)

It results in

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fscuc833dhyge4snqjx1.JPG)

  As you can see we removed the positioning and we added the new section above our header, but our header doesn't stick to the top when we scroll the page and all is not well with the content on our page. Let's use sticky and see what happens.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gqf2lxi8n8jnpfc43mpf.JPG)

Renders

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cy6i4yu1xvulowr6qh7t.JPG)

  So now we have our picture above the header and while scrolling the page we can see the header sticking to the top as soon as it reaches it. This creates the desired effect for us.

   Let's take a look at a move involved example now.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/c6ujvi582d7cgq8s4my9.JPG)

It renders

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fhd0gj4qfb3uzjmp42d0.JPG)

  Here we have 4 groups with a header and a list-content for each of the groups. How can we use sticky position here? 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/icod5bbd6atsfbp7mzi3.JPG)

Resulting in

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/91yq13ejc578et4bo5y2.JPG)

  Let's check how sticky position behaves here. All our headers were told to stick to the top of the screen once they reached it. So A header reaches the top, sticks there and once all content from A header's list-content is off the screen, A transfers the sticky to B header because it reached the top. And it will behave like that all the way till the end of our page.
  This is all in the way we structured our elements in our document and the parent-child relationships between the headers - list content and their groups.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/59pr4qe44ulrq472s9wk.JPG)

  This is exactly why sticky position was created, for such situations as our example. And is where we can enjoy the way our content gets displayed on the browser dynamically.

  There is still an issue with sticky position though and it has to do with browser compatibility. Only an estimated 90% of operational browsers actually fully support sticky right now. Other browsers might support it only partially and some not at all. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8gvmisvb4npmlwzju6i9.JPG)

  We are going to have to make some changes in our styling to get through those problems, either with prefixes for individual browsers or with the second example to ensure our page will function even if the browser does not support sticky position.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l6s9utcatf95hm665jb9.JPG)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7qgg31v0y4rcn8qw6nfp.JPG)


Thank you for your time.
That's all for today,
Stay safe and Zen.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/swhe2lcsdmc31arpaiuz.jpg)