Robin Barnes Robin Barnes - 4 months ago 16
HTML Question

CSS - Make divs align horizontally

I have a container div with a fixed width and height, with overflow: hidden.

I want a horizontal row of float: left divs within this container. Divs which are floated left will naturally push onto the 'line' below after they read the right bound of their parent. This will happen even if the height of the parent should not allow this. This is how this looks:

![Wrong][1] - removed image shack image that had been replaced by an advert

How I would like it to look:

![Right][2] - removed image shack image that had been replaced by an advert

Note: the effect I want can be achieved by using inline elements & white-space: no-wrap (that is how I did it in the image shown). This, however, is no good to me (for reasons too lengthy to explain here), as the child divs need to be floated block level elements.

Answer

You may put an inner div in the container that is enough wide to hold all the floated divs.

<div id="container" style="background-color:red;overflow:hidden;width:200px">
  <div id="inner" style="overflow:hidden;width: 2000px">
     <div style="float:left;background-color:blue;width:50px;height:50px">
     </div>
     <div style="float:left;background-color:blue;width:50px;height:50px">
     </div>
     <div style="float:left;background-color:blue;width:50px;height:50px">
     </div>
     ...
  </div>
</div>