How to get Floating DIVs inside fixed-width DIV to continue horizontally?

前端 未结 9 493
情歌与酒
情歌与酒 2020-12-02 12:43

I have a container DIV with a fixed height and width (275x1000px). In this DIV I want to put multiple floating DIVs each with a width of 300px, and have a horizontal (x-axi

相关标签:
9条回答
  • 2020-12-02 12:59

    The table solution should work very well.

    If you don't want to use tables, you can also put all .block divs in another div inside the #container and give that "in-between-div" a fixed - calculated - width using javascript after loading the page.

    Of course if you already know how many .blocks you have / if the number is fixed, you can give the "in-between-div" a fixed width using css.

    0 讨论(0)
  • 2020-12-02 13:00

    You need an extra div with a large width to contain the blocks, then they will extend wider than the container div and not drop down to a new line.

    The HTML:

    <div id="container">
        <div id="width">
            <div class="block">
                <!-- contents of block -->
            </div>
            <div class="block">
                <!-- contents of block -->
            </div>
            <div class="block">
                <!-- contents of block -->
            </div>
            <!-- more blocks here -->
        </div>
    </div>
    

    The CSS:

    #container {
        height: 275px;
        width: 1000px;
        overflow-x: auto;
        overflow-y: hidden;
        max-height: 275px;
    }
    #container #width {
        width:2000px; /* make this the width you need for x number of blocks */
    }
    #container div.block {
        float: left;
        margin: 3px 90px 0 3px;
    }
    
    0 讨论(0)
  • 2020-12-02 13:00

    Wrap your floated divs in another div with the wider width.

    <div style="width:230px;overflow-x:auto;background-color:#ccc;">
        <div style="width:400px">
            <div style="height:100px;width:100px;float:left;border:1px solid #000;"></div>
            <div style="height:100px;width:100px;float:left;border:1px solid #000;"></div>
            <div style="height:100px;width:100px;float:left;border:1px solid #000;"></div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-02 13:02

    Put the divs you want to scroll in a table like so:

    <div style='width:1000;border:2 solid red;overflow-x:auto'>
       <table><tr>
          <td><div style='width:300;height:200;border:1 solid black'>Cell 1&nbsp;</div></td>
          <td><div style='width:300;height:200;border:1 solid black'>Cell 2&nbsp;</div></td>
          <td><div style='width:300;height:200;border:1 solid black'>Cell 3&nbsp;</div></td>
          <td><div style='width:300;height:200;border:1 solid black'>Cell 4&nbsp;</div></td>
          <td><div style='width:300;height:200;border:1 solid black'>Cell 5&nbsp;</div></td>
       </tr></table>
    </div>
    

    Edit: I tried 3 of these suggested solutions - they all work fine in Google Chrome - but the first one (container1) doesn't work in IE (go figure) - so the SPAN solution gets my vote :-) :

    <html>
    <body>
    <style>
    div#container1 
       {
       height: 275px;
       width: 100%;
       overflow: auto;
       white-space: nowrap;
       border:2 solid red;
       }
    
    div#container1 div.block 
       {
       width: 300px;
       height: 200px;
       display: inline-block;
       border: 1 solid black;
       }
    
    div#container2 
       {
       height: 275px;
       width: 100%;
       overflow: auto;
       white-space: nowrap;
       border:2 solid red;
       }
    
    div#container2 span.block 
       {
       width: 300px;
       height: 200px;
       display: inline-block;
       border: 1 solid black;
       }
    
    div#container3 
       {
       height: 275px;
       width: 100%;
       overflow: auto;
       white-space: nowrap;
       border:2 solid red;
       }
    
    div#container3 div.block 
       {
       width: 300px;
       height: 200px;
       display: inline-block;
       border: 1 solid black;
       }
    
    </style>
    <p>
    <div id='container1'>
          <div class='block'>Cell 1&nbsp;</div>
          <div class='block'>Cell 2&nbsp;</div>
          <div class='block'>Cell 3&nbsp;</div>
          <div class='block'>Cell 4&nbsp;</div>
          <div class='block'>Cell 5&nbsp;</div>
    </div>
    <p>
    <div id='container2'>
          <span class='block'>Cell 1&nbsp;</span>
          <span class='block'>Cell 2&nbsp;</span>
          <span class='block'>Cell 3&nbsp;</span>
          <span class='block'>Cell 4&nbsp;</span>
          <span class='block'>Cell 5&nbsp;</span>
    </div>
    <p>
    <div id='container3'>
       <table><tr>
          <td><div class='block'>Cell 1&nbsp;</div></td>
          <td><div class='block'>Cell 2&nbsp;</div></td>
          <td><div class='block'>Cell 3&nbsp;</div></td>
          <td><div class='block'>Cell 4&nbsp;</div></td>
          <td><div class='block'>Cell 5&nbsp;</div></td>
       </tr></table>
    </div>
    </body>
    </html>
    

    Edit 2:

    I ran this test page through browsershots.org, to see how different browsers handle it. Conclusion: Browser compatibility sucks. :-)

    http://browsershots.org/http://dot-dash-dot.com/files/test_div2.htm

    The table solution worked more often - but the span option (which is cleaner) only broke on browsers I've never heard of. :-)

    0 讨论(0)
  • 2020-12-02 13:07
    div#container {
        height: 275px;
        width: 1000px;
        overflow: auto;
        white-space: nowrap;
    }
    
    div#container span.block {
        width: 300px;
        display: inline-block;
    }
    

    The trick here is only elements that behave as inline by default will behave properly when set to inline-block in Internet Explorer, so the inner containers need to be <span> instead of <div>.

    0 讨论(0)
  • 2020-12-02 13:08

    #row {
        white-space: nowrap; /* important */
        overflow: auto;
    }
    
    .items {
        display: inline-block;
    }
    <div id="row">
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 1" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 2" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 3" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 4" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 5" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 6" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 7" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 8" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 9" />
        </div>
        <div class="items">
            <img src="//placehold.it/200/100" alt="item 10" />
        </div>
    </div>

    The trick here is the "white-space: nowrap" property of the parent which simply tells all it's child elements to continue horizontally and the "display: inline-block" property of it's children. You don't need to add any other property to make this work.

    JS Fiddle: http://jsfiddle.net/2c4jfetf/

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