2

I am looking to take three divs and stack them horizontally (right next to each other) in a containing div. This containing div has a fluid width so that when the browser is scaled in (this is for a responsive design) the three child divs scale proportionally. In addition the three child divs have to be positioned individually so that the left-most div is on the left, the middle div is centered, and the right-most div is all the way to the right side of containing div.

I've tried to accomplish this by setting the container div to display: table and the three child divs to display: table-cell -- this works great except for I can't get the three child divs to be positioned in the way in which I described above. I tried border-spacing on the parent div; however, this doesn't work well with my goal.

Here's my Fiddle with code: http://jsfiddle.net/mkerny45/97mt7/7/

Screenshot of desired result: http://d.pr/i/KUfd (Here you will see three child divs: left, middle, right in a containing div. The left and right divs are all the way to the left/right side respectively and center div is in center of containing div. The margin is is also depicted in the photo. I would like for the entire containing div and child divs to scale down proportionally and have the child divs always stay positioned in their appropriate location.)

Code

<div class="articles">
  <article>
    <img src="http://placehold.it/380x214/000000&text=Left" />
  </article>

  <article>
    <img src="http://placehold.it/380x214/3D6AA2&text=Middle" />
  </article>

  <article>
    <img src="http://placehold.it/380x214/98BD56&text=Right" />
  </article>
</div>

Matt
  • 2,317
  • 7
  • 41
  • 52

1 Answers1

2

You can achieve this using text-align:justify to space the div's evenly so they are flush to the edges. Then you add a span class with a width of 100% to force the width of the wrapper.

DEMO -> http://jsfiddle.net/spUKQ/2/

HTML

<div id="container">
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
    <span class="stretch"></span>
</div>

CSS

#container {
    border: 2px dashed #444;
    height: 125px;

    text-align: justify;
    -ms-text-justify: distribute-all-lines;
    text-justify: distribute-all-lines;

    /* just for demo */
    min-width: 612px;
}

.box1, .box2, .box3 {
    width: 150px;
    height: 125px;
    vertical-align: top;
    display: inline-block;
    *display: inline;
    zoom: 1
}
.stretch {
    width: 100%;
    display: inline-block;
    font-size: 0;
    line-height: 0
}

/* just for demo */
.box1, .box3 {
    background: #ccc
}
.box2 {
    background: #0ff
}
rgdigi
  • 1,701
  • 2
  • 21
  • 31
  • 2
    Please link to the [original answer](http://stackoverflow.com/questions/6865194/fluid-width-with-equally-spaced-divs/6880421#6880421), there's quite a bit of extra information included (also in the comments). Thanks! – thirtydot Jan 09 '13 at 17:16
  • sorry couldn't find the link, just had a page saved – rgdigi Jan 09 '13 at 17:18
  • @reekogi - How would I got about making the images scale proportionally? I implemented your code and it's working except for when the images bump into one another they drop to another line rather than scaling down proportionally. – Matt Jan 09 '13 at 17:25
  • 1
    Use percentages and add display:block; width:100%; to the image -> http://jsfiddle.net/spUKQ/4/ Drag the result window size to see it in action – rgdigi Jan 09 '13 at 17:29
  • @reekogi - is this what the CSS3 property Flexbox is attempting to solve even though the browser support for that is very limited? – Matt Jan 09 '13 at 17:34
  • Indirectly yeah but it's good for loads of responsive uses -> https://developer.mozilla.org/en-US/docs/CSS/Using_CSS_flexible_boxes – rgdigi Jan 09 '13 at 17:51