Expand a div to fill the remaining width

前端 未结 21 1109
一生所求
一生所求 2020-11-21 22:21

I want a two-column div layout, where each one can have variable width e.g.

相关标签:
21条回答
  • 2020-11-21 23:03

    Use calc:

    .leftSide {
      float: left;
      width: 50px;
      background-color: green;
    }
    .rightSide {
      float: left;
      width: calc(100% - 50px);
      background-color: red;
    }
    <div style="width:200px">
      <div class="leftSide">a</div>
      <div class="rightSide">b</div>
    </div>

    The problem with this is that all widths must be explicitly defined, either as a value(px and em work fine), or as a percent of something explicitly defined itself.

    0 讨论(0)
  • 2020-11-21 23:03

    <html>
    
    <head>
      <style type="text/css">
        div.box {
          background: #EEE;
          height: 100px;
          width: 500px;
        }
        div.left {
          background: #999;
          float: left;
          height: 100%;
          width: auto;
        }
        div.right {
          background: #666;
          height: 100%;
        }
        div.clear {
          clear: both;
          height: 1px;
          overflow: hidden;
          font-size: 0pt;
          margin-top: -1px;
        }
      </style>
    </head>
    
    <body>
      <div class="box">
        <div class="left">Tree</div>
        <div class="right">View</div>
        <div class="right">View</div>
        <div style="width: <=100% getTreeWidth()100 %>">Tree</div>
        <div class="clear" />
      </div>
      <div class="ColumnWrapper">
        <div class="Colum­nOne­Half">Tree</div>
        <div class="Colum­nOne­Half">View</div>
      </div>
    
    </body>
    
    </html>

    0 讨论(0)
  • 2020-11-21 23:04

    Use the CSS Flexbox flex-grow property to achieve this.

    html, body {
      height: 100%;
    }
    body {
      display: flex;
    }
    .second {
      flex-grow: 1;
    }
    <div style="background: #bef;">Tree</div>
    <div class="second" style="background: #ff9;">View</div>

    0 讨论(0)
  • 2020-11-21 23:04

    flex-grow - This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.

    If all items have flex-grow set to 1, the remaining space in the container will be distributed equally to all children. If one of the children has a value of 2, the remaining space would take up twice as much space as the others (or it will try to, at least). See more here

    .parent {
      display: flex;
    }
    
    .child {
      flex-grow: 1; // It accepts a unitless value that serves as a proportion
    }
    
    .left {
      background: red;
    }
    
    .right {
      background: green;
    }
    <div class="parent"> 
      <div class="child left">
          Left 50%
      </div>
       <div class="child right">
          Right 50%
      </div>
    </div>

    0 讨论(0)
  • 2020-11-21 23:04

    You can use W3's CSS library that contains a class called rest that does just that:

    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    
    <div class="w3-row">
      <div class="w3-col " style="width:150px">
        <p>150px</p>
      </div>
      <div class="w3-rest w3-green">
        <p>w3-rest</p>
      </div>
    </div>

    Don't forget to link the CSS library in the page's header:

    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    

    Here's the official demo: W3 School Tryit Editor

    0 讨论(0)
  • 2020-11-21 23:05

    Pat - You are right. That's why this solution would satisfy both "dinosaurs" and contemporaries. :)

    .btnCont {
      display: table-layout;
      width: 500px;
    }
    
    .txtCont {
      display: table-cell;
      width: 70%;
      max-width: 80%;
      min-width: 20%;
    }
    
    .subCont {
      display: table-cell;
      width: 30%;
      max-width: 80%;
      min-width: 20%;
    }
    <div class="btnCont">
      <div class="txtCont">
        Long text that will auto adjust as it grows. The best part is that the width of the container would not go beyond 500px!
      </div>
      <div class="subCont">
        This column as well as the entire container works like a table. Isn't Amazing!!!
      </div>
    </div>

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