Absolute position and Overflow:hidden

后端 未结 8 977
暖寄归人
暖寄归人 2020-11-29 03:16
相关标签:
8条回答
  • 2020-11-29 03:54

    I believe, every front-end developer encountered this situation, at least once. Let's say you need to absolute position something… And then you try to move it in some direction, and boom it disappears… You forgot the parent was set to overflow:hidden and now your element is lost in the hidden infinite vacuum.There is a css trick to do this.Please find the below demo example for it...

    <br><br><br>
    <div class="grand-parent">
      <div class="parent">P
        <div class="child">child</div>
      </div>
    </div>
    css code:
    
    
    .grand-parent {
      width:50px;
      height:50px;
      position:relative;
      background-color: grey;
    }
    .parent {
      width:10px;
      height:30px;
      overflow:hidden;
      background-color: blue;
    }
    .child {
      position:absolute; 
      width:50px;
      height:20px;
      background-color: red;
      top:-10px; 
      left:5px;
    }
    
    0 讨论(0)
  • 2020-11-29 03:56

    It's completely impossible to do what you want with both overflow: hidden and position: relative on the parent div.. instead you can introduce an extra child div and move overflow: hidden to that.

    See: http://jsfiddle.net/thirtydot/TFTnU/

    HTML:

    <div id="parent">
        <div id="hideOverflow">
            <div style="width:1000px;background:#ccc">sdfsd</div>
        </div>
      <div id="child">overflow "visible"</div>
    </div>
    

    CSS:

    #parent {
        position:relative;
        background:red;
        width:100px;
        height:100px
    }
    #child {
        position:absolute;
        background:#f0f;
        width:300px;
        bottom: 0;
        left: 0
    }
    #hideOverflow {
        overflow: hidden
    }
    

    #parent {
      position: relative;
      background: red;
      width: 100px;
      height: 100px
    }
    
    #child {
      position: absolute;
      background: #f0f;
      width: 300px;
      bottom: 0;
      left: 0
    }
    
    #hideOverflow {
      overflow: hidden
    }
    <div id="parent">
      <div id="hideOverflow">
        <div style="width:1000px;background:#ccc">sdfsd</div>
      </div>
      <div id="child">overflow "visible"</div>
    </div>

    0 讨论(0)
提交回复
热议问题