This is meant for a menu.
For example I have a div element with 3 spans in it, all of which have some margin, max-width and float (left or right).
It is positioned s
.container {
padding: 10px;
}
.parent {
width: 100%;
background: #7b7b7b;
display: flex;
justify-content: space-between;
height: 4px;
}
.child {
color: #fff;
background: green;
padding: 10px 10px;
border-radius: 50%;
position: relative;
top: -8px;
}
<div class="container">
<div class="parent">
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
</div>
</div>
In the 'old days' you'd use a table and your menu items would be evenly spaced without having to explicitly state the width for the number of items.
If it wasn't for IE 6 and 7 (if that is of concern) then you can do the same in CSS.
<div class="demo">
<span>Span 1</span>
<span>Span 2</span>
<span>Span 3</span>
</div>
CSS:
div.demo {
display: table;
width: 100%;
table-layout: fixed; /* For cells of equal size */
}
div.demo span {
display: table-cell;
text-align: center;
}
Without having to adjust for the number of items.
Example without table-layout:fixed
- the cells are evenly distributed across the full width, but they are not necessarily of equal size since their width is determined by their contents.
Example with table-layout:fixed
- the cells are of equal size, regardless of their contents. (Thanks to @DavidHerse in comments for this addition.)
If you want the first and last menu elements to be left and right justified, then you can add the following CSS:
div.demo span:first-child {
text-align: left;
}
div.demo span:last-child {
text-align: right;
}
If someone wants to try a slightly different approach, they can use FLEX.
HTML
<div class="test">
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
</div>
CSS
.test {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
}
.test > div {
margin-top: 10px;
padding: 20px;
background-color: #FF0000;
}
Here is the fiddle: http://jsfiddle.net/ynemh3c2/ (Try adding/removing divs as well)
Here is where I learned about this: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
This is the quick and easy way to do it
<div>
<span>Span 1</span>
<span>Span 2</span>
<span>Span 3</span>
</div>
css
div{
width:100%;
}
span{
display:inline-block;
width:33%;
text-align:center;
}
Then adjust the width
of the span
s for the number you have.
Example: http://jsfiddle.net/jasongennaro/wvJxD/
I have managed to do it with the following css combination:
text-align: justify;
text-align-last: justify;
text-justify: inter-word;
You just need to display the div with id #menu
as flex container like this:
#menu{
width: 800px;
display: flex;
justify-content: space-between;
}