11

I have seen this solution for evenly spacing DIVs: (Fluid width with equally spaced DIVs) but it requires that the DIVs are all of the same width. This will not work in my case. I have 5 DIVs that are all of different widths and would like to space them evenly. Is this possible without javascript?

For example:

<!-- The text should not wrap, but be on one line for each div -->
<div class="item" id="item1">Item One</div>
<div class="item" id="item2">Item # Two</div>
<div class="item" id="item3">Item Three</div>
<div class="item" id="item4">Item Four</div>
<div class="item" id="item5">Item Five, the Last</div>

I need it to work in IE8+, Firefox 4+, Chrome, Safari.

EDIT: One other requirement: the divs should have no space to the right of the last DIV or left of the first DIV. So the whitespace between them is equal to the difference between the sum of their widths and the width of the container div.

Community
  • 1
  • 1
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330

4 Answers4

19

you could use : display:flex; on a parent container

.evenly {
  display:flex;
  justify-content:space-between;
  border:solid;
}
.evenly div {
  white-space:nowrap;
  background:gray;
}
<div class="evenly">
  <div class="item" id="item1">Item One</div>
<div class="item" id="item2">Item # Two</div>
<div class="item" id="item3">Item Three</div>
<div class="item" id="item4">Item Four</div>
<div class="item" id="item5">Item Five, the Last</div>
</div>

DEMO: http://codepen.io/anon/pen/wxtqJ

extra border and background for demo purpose.

If you need an alternative that works for older nav, you can use text-align:justify properties: http://codepen.io/anon/pen/FnDqr .

G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
3

I prefer display:flex; like said in the answer before. But there is another solution if i understood the requirements correctly. Since i don't have a IE8 TestVM here, I let the testing to you. You can use display: table;

Following solution:

.items
{
    display: table;
    width: 100%;
}
.item
{
    display:table-cell;
}

With this HTML

<div class="items">
    <div class="item" id="item1">Item One</div>
    <div class="item" id="item2">Item # Two</div>
    <div class="item" id="item3">Item Three</div>
    <div class="item" id="item4">Item Four</div>
    <div class="item" id="item5">Item Five, the Last is pretty long</div>
</div>

Test fiddle: http://fiddle.jshell.net/HLFXH/

Nico O
  • 13,762
  • 9
  • 54
  • 69
  • @DonRhummy thank you, i'am aware of that. It's a shame, but we are used to frustration with IE... arent we? ;) – Nico O Feb 06 '14 at 19:15
  • I have the strange feeling that I missed some of the requirement... Because if it was that easy, there would have been more answers... @op if I missed something please add a comment. – Nico O Feb 06 '14 at 19:24
  • 1
    @DonRhummy you did See my answer is not about display-flex Right? If the answer does Not fit please Tell me whats missing – Nico O Feb 06 '14 at 20:02
0

I was checking solution for this issue as I also wanted to show structure similar to yours but only three divs. so,I added one more div inside .item

<div class="items">
    <div class="item" id="item1"><div>Item One</div></div>
    <div class="item" id="item2"><div>Item # Two</div></div>
    <div class="item" id="item3"><div>Item Three</div></div>
    <div class="item" id="item4"><div>Item Four</div></div>
    <div class="item" id="item5"><div>Item Five, the Last is pretty long</div></div>
</div>

And I gave ".item" class width in percent or px. I took the width of longest div and used justify-content as space-between, then align the individual div inside ".item" accordingly.

Hope this will help.

0

There is this simple solution if you are able to use flexbox

<div class="container">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

css

.container {
   display: flex;
   justify-content: space-between;
}
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41