In addition to the Adsy suggestion (to set the container to a fixed position), you can:
1) Use an absolute position on the container:
HTML:
<div class="container"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div>
CSS
.container { background: red; padding: 10px; position:absolute; } .child { width: 100px; height: 100px; float: left; } .child:nth-child(even) { background: green; } .child:nth-child(odd) { background: blue; }
http://jsfiddle.net/tKz8b/
2) Install the float in the container, which is good if you need its relative / static positioning:
HTML:
<div class="container"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div> <div class="clearfix"></div> <div>Next</div>
CSS
.container { background: red; padding: 10px; float: left; } .child { width: 100px; height: 100px; float: left; } .child:nth-child(even) { background: green; } .child:nth-child(odd) { background: blue; } .clearfix:after { content: "."; display: block; clear: both; visibility: hidden; line-height: 0; height: 0; }
http://jsfiddle.net/LYrWx/1/
source share