Mr_Green Mr_Green - 1 month ago 10
Javascript Question

Displaying an element outside overflow hidden container

I have a

<table>
which is made scrollable by wrapping it in a
<div>
of fixed height.

This
<table>
has a dropdown component (blue container in
2nd
column of
1st
row in the image and jsfiddle given below
) which is hiding behind the container
<div>
. I want it to display all the options Instead.

current output

JSFIDDLE (example)

How should I bring the dropdown component outside the container
<div>
to display all the options as in the image below?

desired output

If I remove the
pRelative
container, then the dropdown is fully visible - But when I scroll, the dropdown does not scroll along with it's container.

Thanks in advance.

PS: Looking for CSS/javascript solution only.

T J T J
Answer

You can change positioning of the dropdown to fixed and handle the scroll using js, like the following.

var main = document.getElementsByClassName('main')[0];
var dd = document.getElementsByClassName('pAbsolute')[0];
var offset = dd.getBoundingClientRect().top;
main.onscroll = function() {
  var st = main.scrollTop;
  ddt = (offset - st);
  dd.style.top = ddt + 'px';
}
.main {
  height: 100px;
  overflow-y: scroll;
  overflow-x: hidden;
}
.pRelative {
  position: relative;
}
.pAbsolute {
  position: fixed;
}
.dropdown {
  width: 100px;
  background-color: cornflowerblue;
  z-index: 1000;
}
.option {
  border-top: 1px solid green;
  border-bottom: 1px solid green;
}
table td {
  border: 1px solid black;
  padding: 10px;
}
<div class="main">
  <table>
    <tr>
      <td>row1 column1</td>
      <td>
        <div class="pRelative">
          <div class="pAbsolute dropdown">
            <div class="option">Zero</div>
            <div class="option">One</div>
            <div class="option">Two</div>
            <div class="option">Three</div>
            <div class="option">Four</div>
            <div class="option">Five</div>
            <div class="option">Six</div>
          </div>
        </div>
      </td>
      <td>row1 column3</td>
    </tr>
    <tr>
      <td>row2 column1</td>
      <td>row2 column2</td>
      <td>row2 column3</td>
    </tr>
    <tr>
      <td>row3 column1</td>
      <td>row3 column2</td>
      <td>row3 column3</td>
    </tr>
    <tr>
      <td>row4 column1</td>
      <td>row4 column2</td>
      <td>row4 column3</td>
    </tr>
    <tr>
      <td>row5 column1</td>
      <td>row5 column2</td>
      <td>row5 column3</td>
    </tr>
    <tr>
      <td>row6 column1</td>
      <td>row6 column2</td>
      <td>row6 column3</td>
    </tr>
    <tr>
      <td>row7 column1</td>
      <td>row7 column2</td>
      <td>row7 column3</td>
    </tr>
    <tr>
      <td>row8 column1</td>
      <td>row8 column2</td>
      <td>row8 column3</td>
    </tr>
  </table>
</div>

Demo

Update

You can fix the margin-top issue by creating a new stacking context.

(tested only in safari 6.1 mac - unfortunately doesn't works in any latest browsers) Updated Demo or Another Demo

Update

The only possible cross browser workaround i could find for hiding the fixed elements overflow is to clip the container (this requires it to be a positioned element)

var main = document.getElementsByClassName('main')[0];
var dd = document.getElementsByClassName('pAbsolute')[0];
var offset = dd.getBoundingClientRect().top;
main.onscroll = function() {
  var st = main.scrollTop;
  ddt = (offset - st);
  dd.style.top = ddt + 'px';
}
.main {
  height: 100px;
  overflow-y: scroll;
  overflow-x: hidden;
  margin-top: 100px;
  position: absolute;
  clip: rect(auto, auto, 99999px, auto);
}
.pRelative {
  position: relative;
}
.pAbsolute {
  position: fixed;
}
.dropdown {
  width: 100px;
  background-color: cornflowerblue;
  z-index: 1000;
}
.option {
  border-top: 1px solid green;
  border-bottom: 1px solid green;
}
table td {
  border: 1px solid black;
  padding: 10px;
}
<div class="main">
  <table>
    <tr>
      <td>row1 column1</td>
      <td>
        <div class="pRelative">
          <div class="pAbsolute dropdown">
            <div class="option">Zero</div>
            <div class="option">One</div>
            <div class="option">Two</div>
            <div class="option">Three</div>
            <div class="option">Four</div>
            <div class="option">Five</div>
            <div class="option">Six</div>
          </div>
        </div>
      </td>
      <td>row1 column3</td>
    </tr>
    <tr>
      <td>row2 column1</td>
      <td>row2 column2</td>
      <td>row2 column3</td>
    </tr>
    <tr>
      <td>row3 column1</td>
      <td>row3 column2</td>
      <td>row3 column3</td>
    </tr>
    <tr>
      <td>row4 column1</td>
      <td>row4 column2</td>
      <td>row4 column3</td>
    </tr>
    <tr>
      <td>row5 column1</td>
      <td>row5 column2</td>
      <td>row5 column3</td>
    </tr>
    <tr>
      <td>row6 column1</td>
      <td>row6 column2</td>
      <td>row6 column3</td>
    </tr>
    <tr>
      <td>row7 column1</td>
      <td>row7 column2</td>
      <td>row7 column3</td>
    </tr>
    <tr>
      <td>row8 column1</td>
      <td>row8 column2</td>
      <td>row8 column3</td>
    </tr>
  </table>
</div>

Demo

Comments