CSS - Make divs horizontally aligned

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

I want a horizontal line float: left divs inside this container. Divs that float to the left will naturally be pushed to the β€œline” below, after they read the correct border of their parent. This will happen even if the height of the parent should not allow this. Here's what it looks like:

! [False] [1] - image of the deleted image that has been replaced by the ad

How I would like:

! [Right] [2] - image of the deleted image that has been replaced by the ad

Note. The effect I want can be achieved with inline elements and white space: no-wrap (this is how I did it in the image shown). This, however, is not good for me (for reasons too long to explain here), since child divs should be placed on block level elements.

+81
html css alignment
Aug 31 '08 at 21:05
source share
7 answers

You can put the inner div in a container that is wide enough to hold all floating 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> 
+94
Aug 31 '08 at 21:29
source share
β€” -

style="overflow:hidden" for parent div and style="float: left" for all child divs it is important to make the alignment of divs horizontal for older browsers such as IE7 and below.

For modern browsers, you can use style="display: table-cell" for the entire child divs , and it will be displayed horizontally.

+28
Apr 13 '11 at 11:17
source share

This is similar to what you want:

 #foo { background: red; max-height: 100px; overflow-y: hidden; } .bar { background: blue; width: 100px; height: 100px; float: left; margin: 1em; } 
 <div id="foo"> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> </div> 
+5
Aug 31 '08 at 21:24
source share

you can use the clip property:

 #container { position: absolute; clip: rect(0px,200px,100px,0px); overflow: hidden; background: red; } 

note the position: absolute and overflow: hidden needed for clip work.

+5
Aug 31 '08 at 21:28
source share

Float: left, display: inline-block, both will not be able to align the elements horizontally if they exceed the width of the container.

It is important to note that the container should not be wrapped if items MUST be displayed horizontally: white-space: nowrap

+4
Aug 28 '15 at 17:57
source share

Now you can use css flexbox to align divs horizontally and vertically if you need to. the general formula is as follows

 parent-div { display:flex; flex-wrap: wrap; justify-content: center ; /* for horizontal aligning of child divs */ align-items : center ; /* for vertical aligning */ } child-div { width: /* yoursize for each div */ ; } 
+3
Jul 17 '16 at 10:58
source share

Swim them to the left. In Chrome, at least you don't need to have a wrapper, id="container" , in the LucaM example.

+1
Jul 30 '12 at 7:57
source share



All Articles