あなたが探しているのは、ウィンドウのサイズに応じて別のボックスが収まる/収まらない場合にのみコンテナのサイズを変更する、このようなものだと思います。CSS は動的コンテンツに基づいてセグメント (ボックスの全幅) で縮小できないため、この機能は現在のところ純粋な CSS では使用できません。
CSS
#container {
background:blue;
text-align: left;
font-size:0;
display: inline-block;
padding-left:5px;
padding-bottom:5px;
/* for ie6/7: */
*display: inline;
zoom:1;
}
.box {
display:inline-block;
background:red;
height:50px;
margin-top:5px;
margin-right:5px;
width: 100px;
height: 100px;
}
そして純粋なJavaScript
var boxAmount = 0;
var newWidth = 0;
setNewWidth();
window.onresize = function () {
setNewWidth();
};
function setNewWidth() {
var outerContainer = document.getElementsByTagName('body')[0];
var outerWidth = outerContainer.offsetWidth;
var box = document.getElementsByClassName('box');
var boxWidth = box[0].offsetWidth;
var innerContainer = document.getElementById('container');
var containerPadding = parseInt(window.getComputedStyle(innerContainer, null).getPropertyValue('padding-left'), 10)
+ parseInt(window.getComputedStyle(innerContainer, null).getPropertyValue('padding-right'), 10);
boxAmount = (outerWidth - containerPadding) / (boxWidth + containerPadding);
boxAmount = Math.floor(boxAmount);
if (boxAmount <= box.length) {
newWidth = boxAmount * boxWidth + boxAmount * 5;
}
innerContainer.style.width = newWidth + 'px';
}
ボックスの周りに別のコンテナがある場合のバージョンは次のとおりです
興味のある方はjQuery版をどうぞ