Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 968
Robin Barnes Avatar asked Aug 31 '08 21:08

Robin Barnes


People also ask

How align div horizontally CSS?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do I align 3 divs horizontally in HTML?

Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side.


1 Answers

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

#container {    background-color: red;    overflow: hidden;    width: 200px;  }    #inner {    overflow: hidden;    width: 2000px;  }    .child {    float: left;    background-color: blue;    width: 50px;    height: 50px;  }
<div id="container">    <div id="inner">      <div class="child"></div>      <div class="child"></div>      <div class="child"></div>    </div>  </div>
like image 123
LucaM Avatar answered Sep 28 '22 15:09

LucaM