I would like to have a div, that is centered in its parent, but children inside of it would be left aligned. Wanting this, I obtained the following:
.centered {
text-align: center;
}
.container {
background: red;
padding: 10px;
display: inline-block;
}
.child {
width: 100px;
height: 100px;
float: left;
}
.child:nth-child(even) {
background: green;
}
.child:nth-child(odd) {
background: blue;
}
<div class="centered">
<div class="container">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</div>
http://jsfiddle.net/SbPRg/ (source: Having the floating children elements determine parent width).
The problem is that when you resize the page, there still is a problem whereas the main "red" div is wider than what I want, and shows a big part of red instead of resizing to a smaller container that 'fits' its children.
Would you have any idea of how to obtain something like this, without javascript ?
The main idea is to display a "gallery" of images, in a responsive way (if the screen is too small, display one image per row, up until three per row if the screen is wide).