What I have:
A parent div .parent which takes the entire window width=100% as width but a certain minimum width of, let’s say, min-width=800px.
Now I have about 20 .child divs which, let’s say, use a width of width=300px, an undefined height, margin:20px and display:inline-block.
The problem now is that on e.g. smaller screens two divs will be displayed each row but they won’t be centered anymore since their neighbour dropped into the next row.
I tried float:center and margin:5px auto 5px auto; on the children, but float doesn’t seem to work at all and the margin just results in 0 margin
So what I want is:
horizontally center all child divs in the parent while still using e.g. 80% of the screen width by adjusting the margins between them. Part 2 is more of an optional thing
How it looks right now (not working of course)
HTML
<div class="buttons">
<div class="host 1"> </div>
<div class="host 2"> </div>
<div class="host 3"> </div>
<div class="host 4"> </div>
<div class="host 5"> </div>
...
</div>
CSS
.buttons {
position:relative;
width:100%;
margin:50px 0 0 0;
padding:0;
}
.host {
display:inline-block;
padding:0;
margin:20px 20px 5px 20px;
height:20px;
width:300px;
float:center;
}
To center them horizontally, add
text-align:center;to the parent:Demo
Equal margins all around
Probably there are hundreds of better ways to do it, but
…that’s mine on a saturday lazy afternoon 🙂
Demo (resize the window)
(same html and css as above)
Obviously it’s written this way for clarity, but you can reduce it to:
and if you don’t want the "orphans" to be centered, here’s an even smaller version:
…that comes with a demo.
If anyone has a shorter solution, i’ld love to learn it 🙂