tags:

views:

5165

answers:

7

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-axis) scrollbar appear to allow the user to scroll left and right to view everything.

This is my CSS so far:

div#container {
    height: 275px;
    width: 1000px;
    overflow-x: auto;
    overflow-y: hidden;
    max-height: 275px;
}

div#container div.block {
    float: left;
    margin: 3px 90px 0 3px;
}

The problem is that the floating DIVs will not continue past the width of the container. After putting three of the floating DIV's they will continue on beneath. If I change overflow-y to auto, then the vertical scrollbar appears and I can scroll down.

How can I change this to make the floating DIVs continue on without going beneath each other?

A: 

Use:

    div#container {
        overflow: auto;
    }

Or add a clearing div below the three divs with the style:

    {
        clear: both
    }
Rony
A: 

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>

Ron

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. :-)

Ron Savage
Down voted for suggesting tables.
Matthew James Taylor
@Matthew James Taylor: That's just stupid, you don't know if a table solution is or isn't appropriate because the contents of the divs / cells are not known to us.
jeroen
A: 

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.

jeroen
Down voted for suggesting tables. It's not 1995!
Matthew James Taylor
That's just stupid, you don't know if a table solution is or isn't appropriate because the contents of the divs / cells are not known to us.
jeroen
Matt's original question stated that he wanted 'multiple floated divs' so a table solution is most probably not suitable.
Matthew James Taylor
True, which is why I suggested a css / javascript solution. My comment about tables merely refered to Ron Savage's solution which will work always but uses tables.
jeroen
+3  A: 
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>.

pd
Inline-block is not supported by all browsers so it should never be used.
Matthew James Taylor
It's quite well supported, actually, as long as you're aware of the one issue with IE 6/7. It won't work in IE 5.5 or FF 2, but neither of those represent anything close to a significant share of users these days.
pd
A: 

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>
Emily
A: 

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;
}
Matthew James Taylor
Hey it's Matthew James Taylor! I find your site a very useful resource. And good answer +1
alex
Down voted because the SPAN solution works better and for an unlimited number of inner blocks - don't need to know an estimated width.
Ron Savage
Downvoted because you basically just copied my solution without the javascript addition in case the number of blocks is unknown.
jeroen
@Ron you may be right, a span version could be best except it may not be valid HTML to put block-level elements inside inline elements. See this question: http://stackoverflow.com/questions/746531/is-it-wrong-to-change-a-block-element-to-inline-with-css-if-it-contains-another-b
Matthew James Taylor
@Alex it's good to see a fellow Aussie on here! Thanks for the upvote ;)
Matthew James Taylor
@Matthew James Taylor No one is recommending a solution that uses a block element inside an inline element.
pd
A: 

It sounds like you are doing gallery with div's?

What exactly are you using the divs for?

It may be easier to use a ul/li with spans inside of the li to get the same effect without all the headaches of floating divs.

SkyLar