CSS - How to force elements to 100% of remaining/available space of parent element without extending beyond it?

后端 未结 2 855
时光说笑
时光说笑 2021-02-20 04:06

This seems like a really amateur question, in my opinion, but nonetheless it\'s still a frustrating anomaly.

This is actually the 2nd part of a 2 part problem. The firs

相关标签:
2条回答
  • 2021-02-20 04:36

    I believe the proper solution to something like this is using a flexbox. Flexbox has great support the lately with all modern browsers.

    Use following for the parent

    .stretch-items {
      display: flex;
      flex-direction: column;  
    }
    

    And for the item that should grow

    .stretch-items .stretch {
       flex-grow: 1;  
    }
    

    Here is a codepen demonstrating it https://codepen.io/giorgosk/pen/NWWaqPO

    0 讨论(0)
  • 2021-02-20 04:45

    You can use absolute positioning.

    #b {
        width: 40em;
        height: 20em;
        position:relative;
        background: red;
    }
    #c {
        position: absolute;
        top: 1em;
        bottom: 1em;
        left: 1em;
        right: 1em;
        background: blue;
    }
    
    <div id="b">
        <div id="c">
        </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题