Absolute positioned div inside of block with position:fixed and scrollbars

14,750

Since the absolutely positioned element is inside .scroller and you don't want it to move when scrolling, the scrollable container should be .content instead to .scroller.

.content {
  height: 100px;
  overflow: auto;
}

Moreover, you should remove bottom: 0 from the fixed wrapper so that its height is given by its content, that is, 100px.

.scroller {
  position: fixed;
  border: 1px solid #ddd;
  width: 240px;
}
.content {
  height: 100px;
  overflow: auto;
}
.footer {
  position: absolute;
  bottom: 0;
}
<div class="scroller">
  <div class="content">
    <div>content</div><div>content</div><div>content</div><div>content</div>
    <div>content</div><div>content</div><div>content</div><div>content</div>
    <div>content</div><div>content</div><div>content</div><div>content</div>
  </div>
  <div class="footer">FOOTER</div>
</div>

In case you want multiple .content elements and don't want to scroll each one separately, you can wrap them all in a .scroller-inner container, and set the styles above to it.

.scroller {
  position: fixed;
  border: 1px solid #ddd;
  width: 240px;
}
.scroller-inner {
  height: 100px;
  overflow: auto;
}
.footer {
  position: absolute;
  bottom: 0;
}
<div class="scroller">
  <div class="scroller-inner">
    <div class="content">
      <div>content</div><div>content</div><div>content</div><div>content</div>
    </div>
    <div class="content">
      <div>content</div><div>content</div><div>content</div><div>content</div>
    </div>
    <div class="content">
      <div>content</div><div>content</div><div>content</div><div>content</div>
    </div>
  </div>
  <div class="footer">FOOTER</div>
</div>

Alternatively, if you know the height of the header, you can make the footer a fixed element, and use margins to correct its position. This is kinda hacky, though.

.scroller {
  position: fixed;
  border: 1px solid #ddd;
  width: 240px;
  height: 100px; /* val1 */
  top: 0; /* val2 */
  overflow: auto;
}
.footer {
  position: fixed;
  white-space: nowrap;
  top: 100px; /* val1 + val2 */
  line-height: 20px; /* val3 */
  font-size: 16px; /* val4 */
  margin-top: -18px; /* val3/2 + val4/2 */
}
<div class="scroller">
  <div class="content">
    <div>content</div><div>content</div><div>content</div><div>content</div>
  </div>
  <div class="content">
    <div>content</div><div>content</div><div>content</div><div>content</div>
  </div>
  <div class="content">
    <div>content</div><div>content</div><div>content</div><div>content</div>
  </div>
  <div class="footer">FOOTER</div>
</div>

Share:
14,750
accme
Author by

accme

Updated on June 09, 2022

Comments

  • accme
    accme almost 2 years

    I have a div with position: fixed, which contains two other divs inside: one with content and second which must always be positioned on the bottom of the main div.

    Here is an example:

    .scroller {
      position: fixed;
      border: 1px solid #ddd;
      width: 240px;
      height: 100px;
      top: 0;
      bottom: 0;
      overflow: auto;
    }
    .footer {
      position: absolute;
      bottom: 0;
    }
    <div class="scroller">
      <div class="content">
        <div>content</div><div>content</div><div>content</div><div>content</div>
        <div>content</div><div>content</div><div>content</div><div>content</div>
        <div>content</div><div>content</div><div>content</div><div>content</div>
      </div>
      <div class="footer">FOOTER</div>
    </div>

    The problem is that footer starts to move with other content when user scrolls content of the main block, despite of position:absolute of the footer block.

    Is there any way to stick footer to the bottom of the main fixed block without changing html structure?

    And what if main div contains many children and only last of them is the footer which we need to stick to bottom? Example:

    .scroller {
      position: fixed;
      border: 1px solid #ddd;
      width: 240px;
      height: 100px;
      top: 0;
      bottom: 0;
      overflow: auto;
    }
    .footer {
      position: absolute;
      bottom: 0;
    }
    <div class="scroller">
      <div class="content">
        <div>content</div><div>content</div><div>content</div><div>content</div>
      </div>
      <div class="content">
        <div>content</div><div>content</div><div>content</div><div>content</div>
      </div>
      <div class="content">
        <div>content</div><div>content</div><div>content</div><div>content</div>
      </div>
      <div class="footer">FOOTER</div>
    </div>