Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolutely positioned element inside container with overflow: auto

Tags:

css

We have a modal with position: fixed and overflow-y: auto.

This works well when we have lots of components that overflow since then the scroll bar is shown.

However when we have a custom calendar field inside the modal which opens a popup/dropdown calendar and that element is outside one of the sides of the container, it's not shown.

screenshot

Is there a way to make the popup/dropdown shown while keeping the overflow-y: auto of the modal? Like so:

screenshot2

Codepen to elaborate: http://codepen.io/anon/pen/jWmNMa

.modal {
  position: fixed;
  background-color: pink;
  height: 200px;
  width: 200px;
  left: 30%;

  /* comment out this to show dropdown*/
  overflow: auto;
}

.dropdown {
  background-color: lime;
  height: 80px;
  width: 80px;
  position: absolute;
  left: -50px;
}

html:

<div class="modal">
  <div class="dropdown">
    This is content in a dropdown.
  </div>
  Long long overflowing text...
</div>
like image 490
Cotten Avatar asked Jan 07 '16 15:01

Cotten


2 Answers

In your case, it's not possible for an absolutely positioned child element to appear outside of the parent .modal element when it has overflow: auto set on it (unless you set the position of the .dropdown element to fixed).

The easiest work-around would be to wrap the text and other contents, and then set overflow: auto on that element. For instance, you could wrap it with a .content element, and then set height: 100% and overflow: auto in order to add a scrollbar and hide the overflow for those specific elements.

Updated Example

<div class="modal">
  <div class="dropdown">
    This is content in a dropdown.
  </div>
  <div class="content">...</div>
</div>
.modal {
  position: fixed;
  background-color: pink;
  height: 200px;
  width: 200px;
  left: 30%;
}
.modal .content {
  height: 100%;
  overflow: auto;
}
like image 121
Josh Crozier Avatar answered Oct 21 '22 22:10

Josh Crozier


This worked for me.

Of course, you're limited by the inconvenient effect of position:fixed; but this does achieve your desired result.

.dropdown {
  background-color: lime;
  height: 80px;
  width: 80px;
  position: fixed;
  margin-left:-50px;
}

Hope that helps.

like image 28
fnune Avatar answered Oct 21 '22 21:10

fnune