Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If <strong>css3</strong> is an option, this can be done using the css <code>calc()</code> function.</p> <h2>Case 1: Justifying boxes on a single line ( <a href="http://jsfiddle.net/danield770/3ngvz/3/" rel="nofollow noreferrer">FIDDLE</a> )</h2> <p>Markup is simple - a bunch of divs with some container element.</p> <p>CSS looks like this:</p> <pre><code>div { height: 100px; float: left; background:pink; width: 50px; margin-right: calc((100% - 300px) / 5 - 1px); } div:last-child { margin-right:0; } </code></pre> <p>where <em>-1px</em> to fix an IE9+ calc/rounding bug - see <a href="https://stackoverflow.com/q/16658482/703717"><strong>here</strong></a></p> <h2>Case 2: Justifying boxes on multiple lines ( <a href="http://jsfiddle.net/danield770/Vfffg/16/" rel="nofollow noreferrer">FIDDLE</a> )</h2> <p>Here, in addition to the <code>calc()</code> function, <code>media queries</code> are necessary.</p> <p>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).</p> <p>This sounds like a lot of work, but if you're using LESS or SASS this can be done quite easily </p> <p>(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)</p> <p>Below is an example using LESS: (You can copy/paste this code <a href="http://less2css.org/" rel="nofollow noreferrer"><strong>here</strong></a> to play with it, [it's also the code I used to generate the above mentioned fiddle])</p> <pre><code>@min-margin: 15px; @div-width: 150px; @3divs: (@div-width * 3); @4divs: (@div-width * 4); @5divs: (@div-width * 5); @6divs: (@div-width * 6); @7divs: (@div-width * 7); @3divs-width: (@3divs + @min-margin * 2); @4divs-width: (@4divs + @min-margin * 3); @5divs-width: (@5divs + @min-margin * 4); @6divs-width: (@6divs + @min-margin * 5); @7divs-width: (@7divs + @min-margin * 6); *{margin:0;padding:0;} .container { overflow: auto; display: block; min-width: @3divs-width; } .container &gt; div { margin-bottom: 20px; width: @div-width; height: 100px; background: blue; float:left; color: #fff; text-align: center; } @media (max-width: @3divs-width) { .container &gt; div { margin-right: @min-margin; } .container &gt; div:nth-child(3n) { margin-right: 0; } } @media (min-width: @3divs-width) and (max-width: @4divs-width) { .container &gt; div { margin-right: ~"calc((100% - @{3divs})/2 - 1px)"; } .container &gt; div:nth-child(3n) { margin-right: 0; } } @media (min-width: @4divs-width) and (max-width: @5divs-width) { .container &gt; div { margin-right: ~"calc((100% - @{4divs})/3 - 1px)"; } .container &gt; div:nth-child(4n) { margin-right: 0; } } @media (min-width: @5divs-width) and (max-width: @6divs-width) { .container &gt; div { margin-right: ~"calc((100% - @{5divs})/4 - 1px)"; } .container &gt; div:nth-child(5n) { margin-right: 0; } } @media (min-width: @6divs-width){ .container &gt; div { margin-right: ~"calc((100% - @{6divs})/5 - 1px)"; } .container &gt; div:nth-child(6n) { margin-right: 0; } } </code></pre> <p>So basically you first need to decide a box-width and a minimum margin that you want between the boxes.</p> <p>With that, you can work out how much space you need for each state.</p> <p>Then, use calc() to calcuate the right margin, and nth-child to remove the right margin from the boxes in the final column.</p> <p>The <strong>advantage</strong> of this answer over the accepted answer which uses <code>text-align:justify</code> 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.</p> <p>Regarding <strong>browser support</strong>: This will work on IE9+,Firefox,Chrome,Safari6.0+ - (see <a href="http://caniuse.com/#search=calc" rel="nofollow noreferrer"><strong>here</strong></a> for more details) <strike>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 :) ] </strike> &lt;-- FIXED <a href="https://stackoverflow.com/q/16658482/703717">HERE</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload