これらのdivをラップし、ラップをすべての子の合計に等しく設定する必要があります。width
width
このフィドルの例を見てください!
HTML
<body>
<div id="wrap" class="clearfix">
<div class="test1 floatL">Div 01</div>
<div class="test2 floatL">Div 02</div>
</div>
</body>
CSS
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.floatL {
float: left;
}
.test1 {
width: 500px;
height: 50px;
background-color: #D9D9D9;
}
.test2 {
width: 700px;
height: 30px;
background-color: #CCC;
}
/* helpers */
.clearfix:before,
.clearfix:after {
content: '\0020';
display: block;
overflow: hidden;
visibility: hidden;
width: 0;
height: 0;
}
.clearfix:after {
clear: both;
}
.clearfix {
zoom: 1;
}
Jクエリ
$(function() {
var sum=0;
$('#wrap > div').each( function(){ sum += $(this).width(); });
$('#wrap').width( sum );
});
これが行うことは、 の最初の子それぞれのを収集し、それら#wrap
を合計して の幅に設定することです#wrap
。