I have one div for "classroom" that contains div for each "students". Each "student" div contains an image. Here is the HTML:
<div class="classroom">
<div class="student">
<img class="student-image" src="http://dnqgz544uhbo8.cloudfront.net/_/fp/img/home/f.AmzRdUdc4pEtCuGvU03WXQ.jpg">
</div>
<div class="student">
<img class="student-image" src="http://dnqgz544uhbo8.cloudfront.net/_/fp/img/home/k.jXX55KhHUWZGTAb-GpPkdg.jpg">
</div>
<div class="student">
<img class="student-image" src="http://dnqgz544uhbo8.cloudfront.net/_/fp/img/home/c.ZKQXc2Kc8-po-OK6AhDbtw.jpg">
</div>
</div>
I want to display all the "students" divs in one line so I use the following css:
body {
padding: 0;
margin: 0;
overflow: hidden;
}
html, body {
height: 100%;
}
.classroom {
position: relative;
height: 100%;
}
.classroom .student {
position: relative;
height: 100%;
float: left;
}
.classroom .student .student-image {
height: 100%;
}
In order the students will have enough place in the "classroom" div, I use jQuery in order the calculate the width of the "classroom":
$(document).ready(function() {
var w = 0;
$(".student").each(function() {
w += $(this).width();
});
$(".classroom").width(w);
});
Unfortunately the result is not what I expected. The last "student" div is going down to the next line (as if no float: left;
was assigned). More weird is that when increasing the width of "Classroom" div in 1 pixel, the div returns to it position at the end of the first line.
I made those jsfiddles:
Here http://jsfiddle.net/U3gBG you can see the problem. click on the result panel and use the arrows keys in order to scroll down and right.
Here http://jsfiddle.net/U3gBG/1/ you can see the result of adding 1 to the width of the "classroom" div after calculation (the width of "classroom" equals to the sum of "students" width plus 1 pixel). This result is what I need.
I don't understand why do I need to increase the width of the parent div by 1? Why sum all the child divs width is not enough?