Displaying an element outside overflow hidden container

后端 未结 8 1106
你的背包
你的背包 2020-12-31 06:14

I have a

which is made scrollable by wrapping it in a
of fixed height.

This

has a drop
相关标签:
8条回答
  • 2020-12-31 06:58

    This is a trick that works. but be sure to test on your own.

    Chage Your Class From Position :relative To Position Fixed .

    .pRelative {
        position: fixed;
    }
    

    Here Is The DEMO

    0 讨论(0)
  • 2020-12-31 07:02
    try this ,
    
    
       <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>
    
    
    **Css**
    
    
    body{
      position:relative;
    }
    .main {
        height: 100px;
        overflow-y: scroll;
        overflow-x: hidden;
    }
    
    .pAbsolute {
        position: absolute;
    }
    .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;
    }
    
    0 讨论(0)
  • 2020-12-31 07:05

    The problem here is that you want the drop-down to "escape" its parent, while staying relative to it's parent. This is AFAIK not possible.

    If you position the drop-down absolutely, you "bind" it to the nearest element with position: relative in its direct parent chain, or if no such element to the html element. There is a "trick" where if you do not provide any top/bottom/left/right-values, the element will still position itself where it would have started if it was inline.

    This is why when we remove the relatively positioned wrapper the absolutely positioned drop-down "escapes" the overflow-y: hidden; value on .main (because it attaches to the html-element instead). This also means that its position will not be affected as long as the html-element is not being scrolled.

    When you have a relatively positioned wrapper inside the .main the drop-down gets cut off like anything else inside it.

    0 讨论(0)
  • 2020-12-31 07:07

    As long as the drop down is a child of .main it isn't possible with CSS alone. That's because your .main has overflow-y: scroll; You can't have your cake and eat it too.

    0 讨论(0)
  • 2020-12-31 07:08

    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

    0 讨论(0)
  • 2020-12-31 07:09

    You could get the desired functionality by using a good old fashioned <select> dropdown:

    Working Example

    <div class="main">
        <table>
            <tr>
                <td>row1 column1</td>
                <td>
                    <select class="dropdown">
                        <option class="option">Zero</option>
                        <option class="option">One</option>
                        <option class="option">Two</option>
                        <option class="option">Three</option>
                        <option class="option">Four</option>
                        <option class="option">Five</option>
                        <option class="option">Six</option>
                    </select>
                </td>
                <td>row1 column3</td>
            </tr>
    
    0 讨论(0)
提交回复
热议问题