Solution using jQuery
Here is a prototype of how I would solve this problem.
The HTML:
<div class="widget-wrap">
<div class="widget-content">
<img class="medium_thumb_rounded" src="http://placekitten.com/100/100">
...
</div>
</div>
and the CSS:
.widget-wrap {
outline: 2px dashed blue;
text-align: center;
margin: 0 50px;
}
.widget-content {
outline: 2px dotted blue;
display: inline-block;
vertical-align: bottom;
}
.medium_thumb_rounded {
display: block;
float: left;
width: 100px;
}
and the JavaScipt/jQuery:
var $max_w = 600;
var $img_w = 100;
$(window).resize(function () {
contentResizer();
});
// On load, initially, make sure to set the size.
contentResizer();
function contentResizer() {
var $parent_w = $(".widget-wrap").width();
if ($parent_w < $max_w) {
var $set_w = $parent_w;
} else {
var $set_w = $max_w;
}
var $trimmed_w = Math.floor($set_w / $img_w) * $img_w;
$(".widget-content").width($trimmed_w);
}
Fiddle Demo: http://jsfiddle.net/audetwebdesign/jJCak/
How This Works
I define a containaing block div.widget-wrap
that will text-align: center
its content.
The .widget-content
is an inline-block
type and will hold the images. Use vertical-align
to get rid of some extra white space due to source code line breaks and so on.
Within the .widget-content
, I floated the images to the left and set the width to 100px. You can play with margins and padding as needed.
The jQuery Action
There are two parameters, the image width of 100px and the maximum width of images 600px corresponding to 6 images, again you can determine what is best.
Upon resizing the window, the function contentResizer()
checks the width of the .widget-wrap
and compares it to the $max_w
. If the window shrinks enough, calculate a new width for .widget-content
by rounding down the with to the next whole number of images.
You also want to make sure that you allow for the case of the window getting larger (alternate clause in if
statement).
Finally, call the contentResizer()
once upon loading the page.
Why This Can't Be Done With CSS Alone
The CSS parser will not determine the width of a containing block based on the content of the descendant elements (except for tables, which is uses a different sizing algorithm).
In this application, if you set the width
to a fixed value and use margin: 0 auto
, the block will center. If you don't set the width and use margin: 0 Xpx
, you again will have a centered block with left/right margin of Xpx.
However, if you rely on width: auto
and margin: 0 auto
, the CSS spec does not specify what to do, so the width fills up the width of its parent container.
The reason for this is that the CSS parser formats the content from left to right, top to bottom in a single a pass (except for tables). Ideally, the CSS parser would set the container width, lay out the images, and then go back and calculate the width, and then shrink-to-fit the container width, BUT this is not how things work.
There is a shrink-to-fit algorithm used in table-cells and in some cases for absolutely positioned elements that are overflowing, but this did not help in your application.