I have a fluid width container DIV.
Within this I have 4 DIVs all 300px x 250px...
<div id="container">
<div class="box1"> </div>
<div class="box2"> </div>
<div class="box3"> </div>
<div class="box4"> </div>
</div>
What I want to happen is box 1 to be floated left, box 4 to be floated right and box 2 and 3 to be spaced evenly between them. I want the spacing to be fluid as well so as the browser is made smaller the space becomes smaller also.
See: http://jsfiddle.net/thirtydot/EDp8R/
text-align: justify
combined with.stretch
is what's handling the positioning.display:inline-block; *display:inline; zoom:1
fixesinline-block
for IE6/7, see here.font-size: 0; line-height: 0
fixes a minor issue in IE6.The extra
span
(.stretch
) can be replaced with:after
.This still works in all the same browsers as the above solution.
:after
doesn't work in IE6/7, but they're usingdistribute-all-lines
anyway, so it doesn't matter.See: http://jsfiddle.net/thirtydot/EDp8R/3/
There's a minor downside to
:after
: to make the last row work perfectly in Safari, you have to be careful with the whitespace in the HTML.Specifically, this doesn't work:
And this does:
You can use this for any arbitrary number of child
div
s without adding aboxN
class to each one by changingto
This selects any div that is the first child of the
#container
div, and no others below it. To generalize the background colors, you can use the CSS3 nth-order selector, although it's only supported in IE9+ and other modern browsers:becomes:
See here for a jsfiddle example.
in
jQuery
you might target the Parent directly.This will let the
parent
grow horizontally as thechildren
are beng added.NOTE: This assumes that the
'.children'
have awidth
andHeight
SetHope that Helps.
If css3 is an option, this can be done using the css
calc()
function.Case 1: Justifying boxes on a single line ( FIDDLE )
Markup is simple - a bunch of divs with some container element.
CSS looks like this:
where -1px to fix an IE9+ calc/rounding bug - see here
Case 2: Justifying boxes on multiple lines ( FIDDLE )
Here, in addition to the
calc()
function,media queries
are necessary.The basic idea is to set up a media query for each #columns states, where I then use calc() to work out the margin-right on each of the elements (except the ones in the last column).
This sounds like a lot of work, but if you're using LESS or SASS this can be done quite easily
(It can still be done with regular css, but then you'll have to do all the calculations manually, and then if you change your box width - you have to work out everything again)
Below is an example using LESS: (You can copy/paste this code here to play with it, [it's also the code I used to generate the above mentioned fiddle])
So basically you first need to decide a box-width and a minimum margin that you want between the boxes.
With that, you can work out how much space you need for each state.
Then, use calc() to calcuate the right margin, and nth-child to remove the right margin from the boxes in the final column.
The advantage of this answer over the accepted answer which uses
text-align:justify
is that when you have more than one row of boxes - the boxes on the final row don't get 'justified' eg: If there are 2 boxes remaining on the final row - I don't want the first box to be on the left and the next one to be on the right - but rather that the boxes follow each other in order.Regarding browser support: This will work on IE9+,Firefox,Chrome,Safari6.0+ - (see here for more details)
However i noticed that on IE9+ there's a bit of a glitch between media query states. [if someone knows how to fix this i'd really like to know :) ]<-- FIXED HEREThe easiest way to do this now is with a flexbox:
http://css-tricks.com/snippets/css/a-guide-to-flexbox/
The CSS is then simply:
demo: http://jsfiddle.net/QPrk3/
However, this is currently only supported by relatively recent browsers (http://caniuse.com/flexbox). Also, the spec for flexbox layout has changed a few times, so it's possible to cover more browsers by additionally including an older syntax:
http://css-tricks.com/old-flexbox-and-new-flexbox/
http://css-tricks.com/using-flexbox/
Other posts have mentioned flexbox, but if more than one row of items is necessary, flexbox's
space-between
property fails (see the end of the post)To date, the only clean solution for this is with the
CSS Grid Layout Module (Codepen demo)
Basically the relevant code necessary boils down to this:
1) Make the container element a grid container
2) Set the grid with an 'auto' amount of columns - as necessary. This is done for responsive layouts. The width of each column will be 120px. (Note the use of
auto-fit
(as apposed toauto-fill
) which (for a 1-row layout) collapses empty tracks to 0 - allowing the items to expand to take up the remaining space. (check out this demo to see what I'm talking about) ).3) Set gaps/gutters for the grid rows and columns - here, since want a 'space-between' layout - the gap will actually be a minimum gap because it will grow as necessary.
4) and 5) - Similar to flexbox.
Codepen demo (Resize to see the effect)
Browser Support - Caniuse
Currently supported by Chrome (Blink), Firefox, Safari and Edge! ... with partial support from IE (See this post by Rachel Andrew)
NB:
Flexbox's
space-between
property works great for one row of items, but when applied to a flex container which wraps it's items - (withflex-wrap: wrap
) - fails, because you have no control over the alignment of the last row of items; the last row will always be justified (usually not what you want)To demonstrate:
Codepen (Resize to see what i'm talking about)
Further reading on CSS grids:
This worked for me with 5 images in diferent sizes.
This works because of justify-content:space-between, and it's on a list, displayed horizontally.
On CSS
On html