2

Below is a <div> element with it's position property set to sticky:


<div style="position: sticky;"> </div>.


When I insert two sticky <div> elements in a page, they both stick to the top of the page, and stick, in that sense that sticky elements are working, however, they stick at the exact same spot and cover each other up. In my head I imagined that they would both get to the top of the page, when the user scrolls the page, and stick, but I thought they would stack, but as I stated, they don't, one just sits under the other.

Here is an extremely simplified version of my current project. I want the two blocks to stick, one right above the the other.

<html>
<body>

    <div style="display: block; position: sticky; width: 100% height: 25px; background: #555">
        DIV ONE #1
    </div>

      
    <div style="display: block; position: sticky; width: 100% height: 25px; background: #555">
        DIV TWO #2
    </div>

</body>
</html>

So my question is, how can I add two sticky <div> elements, to the same HTML document, and have one <div> stick to the top of the page when the user scrolls, and the other <div> stick to the bottom of the first <div>, rather than also sticking to the top of the page and covering the that stuck first, up?


To ensure that what I am saying is understood, I have added an interactive example.

Below, the example will show you what is happening within my project — Div Alpha is being covered by Div Beta, and I want Div Beta to stick to the bottom of Div Alpha, so that it doesn't block it.


<!DOCTYPE html>
<html>

<head>
  <style>
    .div-alpha {
      display: block;
      text-align: center;
      position: sticky;
      top: 0;
      width: 200px;
      height: 200px;
      font-size: 30px;
      border: 5px solid #FF20B0;
      background-color: #000000;
      color: #FF20B0;
    }
    
    .div-beta {
      display: block;
      text-align: center;
      position: sticky;
      top: 0;
      width: 200px;
      height: 200px;
      font-size: 30px;
      border: 5px solid #80E000;
      background-color: #002040;
      color: #80E000;
    }
    
    h1 {
      color: #401480;
    }
    
    p.lorem-ipsum {
      width: 350px;
      font-size: 18px;
      color: #001064
    }
    
    p.p-alpha {
      font-size: 14px;
      color: #FF20B0;
    }
    
    p.p-beta {
      font-size: 14px;
      color: #80E000;
    }
  </style>

</head>

<body>

  <h1>Testing Sticky Divs</h1>
  ---

  <br>

  <div class="div-alpha">
    DIV ALPHA
    <p class="p-alpha">The other div covers me up, and I don't want to be covered up!</p>
  </div>

  <br>

  <div class="div-beta">
    DIV BETA
    <p class="p-beta"> I don't want to cover the other div, but I do anyway :..(</p>
  </div>

<!-- The Code Below is silly filler code that has been inserted so that the page will scroll up & down, which is required for observing the behavior of elements that have their "position" property set to "sticky" (i.e. "position: sticky;") -->

  <br>
  <br>
  <br>
  <br>

  <h2>Lorem Ipsum Text</h2>

  ---
  <p class="lorem-ipsum">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Mi ipsum faucibus vitae aliquet nec. Tempus quam pellentesque nec nam aliquam. Purus non enim praesent elementum facilisis leo
    vel fringilla est. Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Eu consequat ac felis donec et odio pellentesque. In ante metus dictum at tempor commodo. Amet massa vitae tortor condimentum. Sapien eget mi proin sed libero enim sed faucibus
    turpis. Tortor at risus viverra adipiscing at. Leo urna molestie at elementum eu facilisis sed. Pharetra diam sit amet nisl suscipit adipiscing. Cursus sit amet dictum sit amet justo donec. Euismod nisi porta lorem mollis. Massa ultricies mi quis
    hendrerit. Lorem ipsum dolor sit amet consectetur. Facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum dui. Mi in nulla posuere sollicitudin aliquam ultrices sagittis. Ornare arcu odio ut sem nulla pharetra. Faucibus et molestie
    ac feugiat sed lectus. Commodo quis imperdiet massa tincidunt nunc. At augue eget arcu dictum varius duis. Potenti nullam ac tortor vitae purus faucibus ornare suspendisse sed. Et molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Convallis
    posuere morbi leo urna molestie at. Enim sit amet venenatis urna cursus eget nunc scelerisque viverra. Tristique senectus et netus et malesuada fames ac. Faucibus ornare suspendisse sed nisi lacus sed viverra tellus. Ut aliquam purus sit amet luctus
    venenatis lectus. Posuere urna nec tincidunt praesent. Aenean et tortor at risus viverra adipiscing at in. Justo eget magna fermentum iaculis eu. Placerat vestibulum lectus mauris ultrices eros in. Pharetra vel turpis nunc eget lorem dolor. Blandit
    turpis cursus in hac habitasse platea dictumst quisque. Nisi porta lorem mollis aliquam ut porttitor leo. Lectus nulla at volutpat diam ut venenatis. Proin nibh nisl condimentum id venenatis. Arcu felis bibendum ut tristique et egestas quis ipsum.
    Feugiat nibh sed pulvinar proin gravida. Odio facilisis mauris sit amet. Gravida in fermentum et sollicitudin ac. Magna etiam tempor orci eu lobortis elementum nibh. Donec ultrices tincidunt arcu non sodales. Consequat ac felis donec et odio. Amet
    mattis vulputate enim nulla aliquet porttitor lacus luctus. Sagittis purus sit amet volutpat consequat mauris nunc. Id interdum velit laoreet id donec ultrices tincidunt arcu non. Diam sit amet nisl suscipit. Viverra tellus in hac habitasse platea
    dictumst vestibulum. Praesent tristique magna sit amet purus gravida.
  </p>

</body>

</html>
JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
  • Related to [pure CSS multiple stacked position sticky?](https://stackoverflow.com/q/54689034/104380) – vsync Aug 27 '22 at 10:08
  • @vsync Geez it has been a while since I asked this. If you have a way of doing it other than CSS, or position sticky, Id certainly be happy to see it. – JΛYDΞV Aug 28 '22 at 22:22

3 Answers3

3
So I figured it out:

Getting 2 Divs to Stick, w/o Covering One Another


There are two ways you can configure the Sticky <div> elements so that they don't cover each other when you scroll down the page.


#1


The first way is to set the property top of the lower div, to be the same combined height as the top div. The key word here is COMBINED which means: The padding and borders need to be added to the height to get an accurate value for top, otherwise the divs will still partially cover one another.


#2

The most simple, straight forward method, would be to create a parent div that is sticky, and then place the two original divs inside of it. Remove the position: sticky; property from the original two <div> elements, so that position sill be set to its default value. Its important that when doing this, you make sure that only the parent container has its position property set to sticky (i.e. position: sticky), or else you'll get undesired results. Below is the questions code rewritten using solution #2.



<!DOCTYPE html>
<html>

<head>
    <style>
        .div-alpha {
            display: block;
            text-align: center;
            width: 200px;
            height: 200px;
            font-size: 30px;
            text-decoration: underline;
            border: 5px solid #08C8FF;
            background-color: #900040;
            color: #08C8FF;
        }

        .div-beta {
            display: block;
            text-align: center;
            width: 200px;
            height: 200px;
            font-size: 30px;
            text-decoration: underline;
            border: 5px solid #EE1054;
            background-color: #00307A;
            color: #EE1054;
        }

        .div-gamma {
            display: block;
            text-align: center;
            position: sticky;
            top: 0;
        }

        p {
            width: 350px;
            font-size: 18px;
        }
    </style>

</head>

<body>

    <h1>Testing Sticky Divs</h1>
    ---

    <br>

    <div class="div-gamma">
        <div class="div-alpha">DIV ALPHA</div>
        <div class="div-beta">DIV BETA</div>
    </div>

    <br>
    <br>
    <br>
    <br>

    <h3>Lorem Ipsum Text</h3>
    ---
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
        magna aliqua. Mi ipsum faucibus vitae aliquet nec. Tempus quam pellentesque nec nam aliquam. Purus non enim
        praesent elementum facilisis leo vel fringilla est. Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Eu
        consequat ac felis donec et odio pellentesque. In ante metus dictum at tempor commodo. Amet massa vitae tortor
        condimentum. Sapien eget mi proin sed libero enim sed faucibus turpis. Tortor at risus viverra adipiscing at.
        Leo urna molestie at elementum eu facilisis sed. Pharetra diam sit amet nisl suscipit adipiscing. Cursus sit
        amet dictum sit amet justo donec. Euismod nisi porta lorem mollis. Massa ultricies mi quis hendrerit. Lorem
        ipsum dolor sit amet consectetur. Facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum dui.

        Mi in nulla posuere sollicitudin aliquam ultrices sagittis. Ornare arcu odio ut sem nulla pharetra. Faucibus et
        molestie ac feugiat sed lectus. Commodo quis imperdiet massa tincidunt nunc. At augue eget arcu dictum varius
        duis. Potenti nullam ac tortor vitae purus faucibus ornare suspendisse sed. Et molestie ac feugiat sed lectus
        vestibulum mattis ullamcorper. Convallis posuere morbi leo urna molestie at. Enim sit amet venenatis urna cursus
        eget nunc scelerisque viverra. Tristique senectus et netus et malesuada fames ac. Faucibus ornare suspendisse
        sed nisi lacus sed viverra tellus. Ut aliquam purus sit amet luctus venenatis lectus. Posuere urna nec tincidunt
        praesent. Aenean et tortor at risus viverra adipiscing at in. Justo eget magna fermentum iaculis eu. Placerat
        vestibulum lectus mauris ultrices eros in.

        Pharetra vel turpis nunc eget lorem dolor. Blandit turpis cursus in hac habitasse platea dictumst quisque. Nisi
        porta lorem mollis aliquam ut porttitor leo. Lectus nulla at volutpat diam ut venenatis. Proin nibh nisl
        condimentum id venenatis. Arcu felis bibendum ut tristique et egestas quis ipsum. Feugiat nibh sed pulvinar
        proin gravida. Odio facilisis mauris sit amet. Gravida in fermentum et sollicitudin ac. Magna etiam tempor orci
        eu lobortis elementum nibh. Donec ultrices tincidunt arcu non sodales. Consequat ac felis donec et odio. Amet
        mattis vulputate enim nulla aliquet porttitor lacus luctus. Sagittis purus sit amet volutpat consequat mauris
        nunc. Id interdum velit laoreet id donec ultrices tincidunt arcu non. Diam sit amet nisl suscipit. Viverra
        tellus in hac habitasse platea dictumst vestibulum. Praesent tristique magna sit amet purus gravida.
    </p>

</body>

</html>
JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
0

Here is an example

div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background-color: yellow;
  padding: 50px;
  font-size: 20px;
}

<div class="sticky">
<p> This is your sticky box </p>
</div>
<div>
<p>This is your other divs and properties </p>
</div>
Gimnath
  • 824
  • 9
  • 11
  • Yes, but then that does not stick to the bottom of the top element. I want them to both stick, but stack when they reach top of page. in other words stick together one on top of the other. – JΛYDΞV Dec 21 '19 at 03:26
-1

This is what I do to make a navbar that has a functioning responsive mobile drop-down menu. Sounds like you already figured it out, but I thought id give ya some feedback. At the surface, the paradigm, is to put all objects that are supposed to stick in a single sticky container, however; implementing it is much harder than it sounds. Good Luck!


<!DOCTYPE HTML>
<html lang='us-en'>
<head>
<style type='text/css'>
.nav {
      position: sticky;
      position: -webkit-sticky;
      top: 0;
      left: 0;
      display: grid;
      grid-template-columns: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
}

.nav-bar {
      background-color: #000;
      display: block;
      width: 100%;
}

.nav-bar a {
      display: inline-block;
      font-size: 26px);
      text-decoration: none;
      margin: 16px 4px 0 12px;
}



/*!!! ~~~ ICONS ~~~ */
#home {
      display: block;
      float: left;
      padding: 12px;
      font-size: 38px !important;

}

#bars {
      display: none;
      float: right;
      padding: 4px;
      font-size: 38px !important;
}



/*! ~~~ Drop & Drop-Items  ~~~ */
.nav-drop {
      background-color: #000;
      display: none;
      width: 100%;
}

.nav-drop button {
      display: block;
      width: 54%;
      margin: 12px 23%;
      border: 1px solid #0FF;
      padding: 1px;
}

</style>
</head>


<!-- BODY'S MARKUP -->
<body>

<div class="nav">
      <div class="nav-bar">
            <i id="home" class="fa fa-home" aria-hidden="true" onclick="go2('home')">&nbsp; </i>
            <a href="home">HOME &nbsp;| </a>
            <a href="about">ABOUT &nbsp;| </a>
            <a href="contact">CONTACT &nbsp;| </a>
            <a href="forum">FORUM</a>
            <i id="bars" class="fa fa-bars" aria-hidden="true" onclick="dropMenu()"></i>
      </div>
      <div id="nav-drop" class="nav-drop">
            <button onclick="go2('about')">ABOUT</button>
            <button onclick="go2('contact')">CONTACT</button>
            <button onclick="go2('forum')">FORUM</button>
      </div>
</div>
</body>
</html>