How to spread elements horizontally evenly?
CSS3 flexboxes have better browser support now, although you may need to add additional vendor prefixes for more browser coverage.
Modern Approach:
Just change the display
of the container element to flex
and then utilize the justify-content
property to specify how the browser should distribute the space around and between the flex items along the main axis.
In the example below, you will notice that justify-content: space-around
or justify-content: space-between
are used to space the elements out evenly.
.container {
display: flex;
}
.container.space-around {
justify-content: space-around;
}
.container.space-between {
justify-content: space-between;
}
<p>Using <code>justify-content: space-around</code>:</p>
<div class="container space-around">
<div>A</div>
<div>B</div>
<div>C</div>
</div>
<hr />
<p>Using <code>justify-content: space-between</code>:</p>
<div class="container space-between">
<div>A</div>
<div>B</div>
<div>C</div>
</div>
Try this (http://jsfiddle.net/BYEw5/):
<div class="container">
<div>A</div><div>B</div><div>C</div>
</div>
.container > div {
display: inline-block;
display: -moz-inline-box;
*display: inline; /* For IE7 */
zoom: 1; /* Trigger hasLayout */
width: 33%;
text-align: center;
}
Since you're dealing with inline-block, you can't have spaces between the tags (ugly, but it works), otherwise the space will be visible.
Edit 1:
Here is some more info on the inline-block issues: http://blog.another-d-mention.ro/programming/cross-browser-inline-block/, http://www.aarongloege.com/blog/web-development/css/cross-browser-inline-block/. You may also have to add display: -moz-inline-box;
.
Edit 2:
Also, 33%*3 is not 100%. If you truly want 100% and don't mind some space between the div
s you could do:
.container > div {
display: inline-block;
display: -moz-inline-box;
*display: inline; /* For IE7 */
zoom: 1; /* Trigger hasLayout */
margin-left: 2%;
width: 32%;
text-align: center;
}
.container > div:first-child {
margin-left: 0;
}
how about text-align:justify;
?
I know this is an ancient question but if someone still happens to look for this, there now is an even easier option using flexboxes: justifiy-content: space-evenly
. The name is pretty self explanatory. Here a reference
.container {
display: flex;
justify-content: space-evenly;
}
I'm not sure what your exact HTML is but try this: http://jsfiddle.net/k9FqG/
<div class="test">
<a href="">A</a>
<a href="">B</a>
<a href="">C</a>
<div class="clear"></div>
</div>
.clear {
clear:both;
}
.test {
width:350px;
text-align:center;
border:1px solid #ff0000
}
.test a {
display:block;
float:left;
width:33%;
}