Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

position sticky is not sticking properly [duplicate]

Tags:

html

css

In the snippet given below element with position sticky does not sticks to the end of the page.

body {
  margin: 0;
}

div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;
}
<!DOCTYPE html>
<html>

<body>



  <div class="sticky">I am sticky!</div>

  <div style="margin-bottom:2000px;
            border:2px solid black ;">
    <p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p>
    <p>Scroll back up to remove the stickyness.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint
      efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint
      efficiantur his ad. Eum no molestiae voluptatibus.</p>
  </div>

</body>

</html>

but when you add a border to the body , position sticky works fine.

body {margin:0;
      border:5px solid red;}
      
      div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;
}
<!DOCTYPE html>
<html>

<body>



<div class="sticky">I am sticky!</div>

<div style="margin-bottom:2000px;
            border:2px solid black ;">
  <p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p>
  <p>Scroll back up to remove the stickyness.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>

</body>
</html>

I have 2 questions .

  1. in first snippet why did element with position sticky did not stick to the end of the page just like in second snippet?

  2. why did element with position sticky started sticking all the way to the end of the page in second snippet when i added a border in body element ?

like image 560
noob Avatar asked Sep 16 '20 08:09

noob


People also ask

Why isnt my position sticky working?

That can happen for many reasons: Position sticky will most probably not work if overflow is set to hidden, scroll, or auto on any of the parents of the element. Position sticky may not work correctly if any parent element has a set height. Many browsers still do not support sticky positioning.

How do you make position sticky work with the overflow property?

If you really want position: sticky to work on all modern browsers, then you should consider not using overflow: hidden on the <body> or any wrapper surrounding the main content, but rather put a wrapper around elements that will overflow the viewport. Then, those wrappers should use overflow: hidden.

Does position sticky work in Chrome?

It is available on Chrome (yay), Firefox and Safari. Here are more details about position:sticky : Specification.


2 Answers

The scrollable element needs to be of type inline e.g. inline, inline-block, inline-flex or inline-grid:

It's quite interesting behaviour you have discovered though - somehow adding a border completely changes the height of the element so that it's full height instead of just fitting the content. That part I can't explain only the normal solution of using an inline element to make it full height 🤔

body {
  margin: 0;
  display: inline-block;
}

div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;
}
<!DOCTYPE html>
<html>

<body>



  <div class="sticky">I am sticky!</div>

  <div style="margin-bottom:2000px;
            border:2px solid black ;">
    <p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p>
    <p>Scroll back up to remove the stickyness.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint
      efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint
      efficiantur his ad. Eum no molestiae voluptatibus.</p>
  </div>

</body>

</html>
like image 117
Dominic Avatar answered Sep 21 '22 10:09

Dominic


This documentation really explains why this issue happens

These are the points which i understood

  1. A sticky position is Identical to relative, except that its offsets are automatically adjusted in reference to the nearest ancestor scroll container’s scrollport in whichever axes the inset properties are not both auto, to try to keep the box in view within its containing block as the user scrolls. This positioning scheme is called sticky positioning.

In your case , the ancestor element is no other than body

Body element's height and width are set as autoby default

Once when you added a paragraph , the 'body's height is adjusted to it's height(say 110vh)

Which will let your sticky element to stick till 110vh Even when you add margin-bottom of 200vh to a div , this will never change the height of body it still remains 110vh (why right ? answer below).

If there is no border, padding, inline part, block formatting context created, or clearance to separate the margin-top of a block from the margin-top of one or more of its descendant blocks; or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block from the margin-bottom of one or more of its descendant blocks, then those margins collapse. The collapsed margin ends up outside the parent.


In your case,since the parent body doesn't has no border ,mannual height ,... which resulted your division margin of 2000px to go outside the body

Conclusion :

  1. sticky position is very relative to its parent element's height.
  2. Analyse whether there is a margin collision between the parent and children element or not. 3.To avoid margin collision , we can use border , inline part, block formatting , etcc on the parent element .

I have commented some css attributes you can add on the parent to avoid margin collision.

body {
/*border :1px solid transparent;*/
/*display:inline-block;*/
/*padding :1px;*/
float:left;
}

div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;
}
<!DOCTYPE html>
<html>

<body >



  <div class="sticky">I am sticky!</div>

  <div style="margin-bottom:2000px;
            border:2px solid black ; background:#aaa;">
    <p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p>
    <p>Scroll back up to remove the stickyness.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint
      efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint
      efficiantur his ad. Eum no molestiae voluptatibus.</p>
  </div>

</body>

</html>
like image 38
Sandrin Joy Avatar answered Sep 19 '22 10:09

Sandrin Joy