-1

I have HTML/CSS layout problem:

I have 4 divs with the same width and height. I want to "justify them" so thay should be place from one side to the other with the same spaces between them. In other word: suppose the A, B, C, D are divs and "|" means start/end of row. So I want receive following effect of 3 divs in one row:

|A    B    C    D|

How to do it. I was able to do it with 3 divs, but how to do it with 4 divs?

raina77ow
  • 103,633
  • 15
  • 192
  • 229
P.K.
  • 1,746
  • 6
  • 25
  • 62
  • 2
    Can you post the code you've tried please? – j08691 Dec 10 '14 at 16:04
  • is the row wide enough ? if you could do it with 3 and it's not working with 4 then that suggests not. don't make total width 25% , unless you're using border-box, as they will drop down – Billy Dec 10 '14 at 16:09

3 Answers3

8

For full browser support, you can use the below:

#container {
    text-align: justify;
    -ms-text-justify: distribute-all-lines;
    text-justify: distribute-all-lines;
    width:100%;
}
#container>div {
    width: 100px;
    height: 100px;
    vertical-align: top;
    display: inline-block;
    *display: inline;
    zoom: 1;
    background:red;
}
span {
    width: 100%;
    display: inline-block;
    font-size: 0;
    line-height: 0
}
<div id="container">
    <div></div>
    <div></div>
    <div></div>
    <div></div> <span></span>
</div>
SW4
  • 69,876
  • 20
  • 132
  • 137
6

Using flexible boxes:

#wrapper {
  display: flex;
  justify-content: space-between;
}

#wrapper {
  display: flex;
  justify-content: space-between;
}
<div id="wrapper">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>
Oriol
  • 274,082
  • 63
  • 437
  • 513
2

Live example is here: http://jsfiddle.net/gLxr3cqk/1/

#container {
  text-align: justify;
}
#container > div {
  width: 100px; /* Declare your value. Can be in relative units. */
  height: 100px;
  display: inline-block;
  vertical-align: top;
  border: 1px black solid;

  /* IE fix. */
  *display: inline;
  zoom: 1;
}
#container:after {
  content: "";
  width: 100%;
  display: inline-block;
}
<div id="container">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
Oriol
  • 274,082
  • 63
  • 437
  • 513
P.K.
  • 1,746
  • 6
  • 25
  • 62