How to make a div fill a remaining horizontal space?

前端 未结 25 1393
野的像风
野的像风 2020-11-22 00:45

I have 2 divs: one in the left side and one in the right side of my page. The one in the left side has fixed width and I want the one of the right side to f

相关标签:
25条回答
  • 2020-11-22 01:33

    You can use the Grid CSS properties, is the most clear, clean and intuitive way structure your boxes.

    #container{
        display: grid;
        grid-template-columns: 100px auto;
        color:white;
    }
    #fixed{
      background: red;
      grid-column: 1;
    }
    #remaining{
      background: green;
      grid-column: 2;
    }
    <div id="container">
      <div id="fixed">Fixed</div>
      <div id="remaining">Remaining</div>
    </div>

    0 讨论(0)
  • 2020-11-22 01:34

    /* * css */

    #search {
     position: absolute;
     width: 100px;
    }
    .right-wrapper {
      display: table;
      width: 100%;
      padding-left:100px;
    }
    .right-wrapper #navigation {
     display: table-cell;
     background-color: #A53030;
    }
    

    /* * html */

    <div id="search"></div>
    <div class="right-wrapper">
       <div id="navigation"></div>
    </div>
    
    0 讨论(0)
  • 2020-11-22 01:34

    The easiest solution is to use margin. This will also be responsive!

    <div style="margin-right: auto">left</div>
    <div style="margin-left: auto; margin-right:auto">center</div>
    <div style="margin-left: auto">right</div>
    
    0 讨论(0)
  • 2020-11-22 01:35

    I had a similar issue and came up with the following which worked well

    CSS:

    .top {
    width: auto;
    height: 100px;
    background-color: black;
    border: solid 2px purple;
    overflow: hidden;
    }
    .left {
    float:left;
    width:100px;
    background-color:#ff0000;
    padding: 10px;
    border: solid 2px black;
    }
    .right {
    width: auto;
    background-color:#00FF00;
    padding: 10px;
    border: solid 2px orange;
    overflow: hidden;
    }
    .content {
    margin: auto;
    width: 300px;
    min-height: 300px;
    padding: 10px;
    border: dotted 2px gray;
    }
    

    HTML:

    <div class=top>top </div>
    <div>
        <div class="left">left </div>
        <div class="right">
            <div class="content">right </div>
        </div>
    </div>
    

    This method won't wrap when the window is shrunk but will auto expand the 'content' area. It will keep a static width for the site menu (left).

    And for auto expanding content box and left vertical box(site menu) demo:

    https://jsfiddle.net/tidan/332a6qte/

    0 讨论(0)
  • 2020-11-22 01:37

    If you are trying to fill remaining space in a flexbox between 2 items, add the following class to a an empty div between the 2 you want to seperate:

    .fill {
      // This fills the remaining space, by using flexbox. 
      flex: 1 1 auto;
    }
    
    0 讨论(0)
  • 2020-11-22 01:37

    I tried the above solutions for a liquid left, and a fixed right but non of them worked (I am aware the question is for the reverse but I think this is relevant). Here's what did work:

    .wrapper {margin-right:150px;}
    .wrapper .left {float:left; width:100%; margin-right:-150px;}
    
    .right {float:right; width:150px;}
    
    <div class="wrapper"><div class="left"></div></div>
    <div class="right"></div>
    
    0 讨论(0)
提交回复
热议问题