I have DIV with flexible width set e.g. min-width:800px and max-width:1400px. In this DIV, there are many boxes with fix width 200px and display:inline-block. So depending on parent DIV width, these boxes fill the entire space.
My problem is the blank space on the right side which is caused by variable width of the parent div. Sometimes this blank space is small and looks fine, but with different widths of the parent div, this blank space is almost 200px.
I don't know, if I described my problem in enough detail, I hope this picture will help to describe my actual situation:
And this is what I would like to have:
This auto-margin could be easily achieved by using TABLE. However, I don't know the exact number of columns, since it depends on user's screen resolution. So I can't use table and rather stick with CSS.
Anyone has an idea how to solve this ? Thank you in advance for your comments and answers.
EDIT: I don't need support of IE6. I would like to support IE7, but IE7 is optional as I know there are limitations so I will probably use fixed width of "div.wrapper" in IE7
EDIT2 I need to handle multiple rows of these boxes, so they don't exceed the "div.wrapper" box and wrap correctly in multiple lines of boxes, not just in one long line.
EDIT3 I don't know the number of "columns" as this is very variable depending on user's screen resolution. So on big screen there could be 7 boxes in one row, and on small screens there could be just 4 boxes in one row. So I need solution that doesn't set fixed number of boxes in one row. Instead, when the boxes don't fit in one row, they should just wrap to a next row.
quite old but worth trying since multiple rows and text-align: justify; in the #container creates gaps when last row has less divs. I wanted everything to be floated left. So my idea was to use 2 wrappers.
as well as overflow: hidden; in css
drawback: margins are not auto set...
DEMO: http://jsfiddle.net/hexagon13/2avwf/52/
You can float them and just apply a wrapper to the
.box
which will allow you tomargin:auto;
the.box
relative to the floated wrapper.CSS:
HTML:
Demo: http://jsfiddle.net/2avwf/
I didn't make them 200px wide for the sake of the fiddle window. Just swap that
width:80px
out with the width you desire.If you want to make this a dynamic solution, in which the number of boxes in a row will vary from user to user based off their screen size, etc., simply make 3 or 4 width-defining box-wrapper classes:
Then with JQuery you can easily detect the width of
.wrapper
and assign an override class to the box wrappers:Something like this: http://jsfiddle.net/RcDky/
Try this:
This is as close as IE7-compatible CSS can get: http://jsfiddle.net/thirtydot/79mFr/
If this still isn't right, it's time to look at using JavaScript and hopefully also jQuery. If you define your requirements properly, it should be trivial to get this perfect with JavaScript.
HTML:
CSS:
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/79mFr/2/
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:
I answered a similar question here
This is possible in pure css3 using
media queries
and the csscalc()
routine.Of coarse this will only work on modern browsers. IE9+,Chrome,Firefox,
See this WORKING DEMO
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).
On my project I have faced with the same problem and I came to the next decision - the best way for me is to go with js, in my case you can have xxx count of block inside container, if there is enough space in 1st row the block from 2nd row goes up to the 1st row, and so on. here is an example http://jsfiddle.net/gVAjN/11/