How to make a div fill a remaining horizontal space?

前端 未结 25 1439
野的像风
野的像风 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:45

    This seems to accomplish what you're going for.

    #left {
      float:left;
      width:180px;
      background-color:#ff0000;
    }
    #right {
      width: 100%;
      background-color:#00FF00;
    }
    <div>
      <div id="left">
        left
      </div>
      <div id="right">
        right
      </div>
    </div>

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

    @Boushley's answer was the closest, however there is one problem not addressed that has been pointed out. The right div takes the entire width of the browser; the content takes the expected width. To see this problem better:

    <html>
    <head>
        <style type="text/css">
        * { margin: 0; padding: 0; }
        body {
            height: 100%;
        }
        #left {
            opacity: 0;
            height: inherit;
            float: left;
            width: 180px;
            background: green;
        }
        #right {
            height: inherit;
            background: orange;
        }
        table {
                width: 100%;
                background: red;
        }
        </style>
    </head>
    <body>
        <div id="left">
            <p>Left</p>
        </div>
        <div id="right">
            <table><tr><td>Hello, World!</td></tr></table>
        </div>
    </body>
    </html>
    

    http://jsfiddle.net/79hpS/

    The content is in the correct place (in Firefox), however, the width incorrect. When child elements start inheriting width (e.g. the table with width: 100%) they are given a width equal to that of the browser causing them to overflow off the right of the page and create a horizontal scrollbar (in Firefox) or not float and be pushed down (in chrome).

    You can fix this easily by adding overflow: hidden to the right column. This gives you the correct width for both the content and the div. Furthermore, the table will receive the correct width and fill the remaining width available.

    I tried some of the other solutions above, they didn't work fully with certain edge cases and were just too convoluted to warrant fixing them. This works and it's simple.

    If there are any problems or concerns, feel free to raise them.

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

    I ran into this same problem trying to layout some jqueryUI controls. Although the common philosophy now is "use DIV instead of TABLE", I found in my case using TABLE worked much better. In particular, if you need to have straightforward alignment within the two elements (e.g., vertical centering, horizontal centering, etc.) the options available with TABLE give simple, intuitive controls for this.

    Here's my solution:

    <html>
    <head>
      <title>Sample solution for fixed left, variable right layout</title>
      <style type="text/css">
        #controls {
          width: 100%;
        }
        #RightSide {
          background-color:green;
        }
      </style>
    </head>
    
    <body>
    <div id="controls">
      <table border="0" cellspacing="2" cellpadding="0">
        <TR>
          <TD>
            <button>
    FixedWidth
            </button>
          </TD>
          <TD width="99%" ALIGN="CENTER">
            <div id="RightSide">Right</div>
          </TD>
        </TR>
      </table>
    </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 01:48

    The solution comes from the display property.

    Basically you need to make the two divs act like table cells. So instead of using float:left, you'll have to use display:table-cell on both divs, and for the dynamic width div you need to set width:auto; also. The both divs should be placed into a 100% width container with the display:table property.

    Here is the css:

    .container {display:table;width:100%}
    #search {
      width: 160px;
      height: 25px;
      display:table-cell;
      background-color: #FFF;
    }
    #navigation {
      width: auto;
      display:table-cell;
      /*background-color: url('../images/transparent.png') ;*/
      background-color: #A53030;
    }
    
    *html #navigation {float:left;}
    

    And the HTML:

    <div class="container">
       <div id="search"></div>
       <div id="navigation"></div>
    </div>
    

    IMPORTANT:For Internet Explorer you need to specify the float property on the dynamic width div, otherwise the space will not be filled.

    I hope that this will solve your problem. If you want, you can read the full article I wrote about this on my blog.

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

    Boushley's answer seems to be the best way to go in order to arrange this using floats. However, it isn't without its problems. Nested floating within the expanded element will not be available to you; it will break the page.

    The method shown basically "fakes it" when it comes to the expanding element - it isn't actually floating, it's just playing along with the fixed-width floated elements using its margins.

    The problem then is exactly that: the expanding element isn't floated. If you try and have any nested floating within the expanding element, those "nested" floated items aren't really nested at all; when you try to stick a clear: both; under your "nested" floated items, you'll end up clearing the top-level floats as well.

    Then, to use Boushley's solution, I'd like to add that you should place a div such as the following: .fakeFloat { height: 100%; width: 100%; float: left; } and place this directly within the expanded div; all the expanded div's contents should go then within this fakeFloat element.

    For this reason, I'd recommend using tables in this specific case. Floated elements really aren't designed to do the expansion that you'd like, whereas the solution using a table is trivial. An argument is generally made that floating is more proper for layouts, not tables.. but you aren't using floating here anyways, you're faking it - and that sort of defeats the purpose of the stylistic argument for this specific case, in my humble opinion.

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

    I have a very simple solution for this ! //HTML

    <div>
    <div id="left">
        left
    </div>
    <div id="right">
        right
    </div>
    

    //CSS

    #left {
    float:left;
    width:50%;
    position:relative;
    background-color:red;
    }
    #right {
    position:relative;
    background-color:#00FF00;}
    

    Link: http://jsfiddle.net/MHeqG/

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