Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why absolute element does not keep position when parent scroll

.wrap {
  max-height: 200px;
  overflow-y: auto;
  position: relative;
  border: 2px solid green;
}

.content {
  height: 1000px;
}

.absolute {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: red;
}
<div class="wrap">
  <div class="content"></div>
  <div class="absolute">absolute</div>
</div>

As you can see in snippet. When I scroll the .wrap, the .absolute also go away too. Why this happen. And how can I keep the .absolute on the bottom.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

With position: sticky;, like so:

.wrap {
  max-height: 200px;
  overflow-y: auto;
  position: relative;
  border: 2px solid green;
}

.content {
  height: 1000px;
}

.absolute {
  position: sticky;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: red;
}
<div class="wrap">
  <div class="content"></div>
  <div class="absolute">absolute</div>
</div>

For a broader explanation on the why, you may read this article about positioning.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading