Reputation: 6207
#container
{
text-align: center;
background-color: green;
}
.box
{
display: inline;
margin-left: 50px;
}
<div id="container">
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
</div>
this is I have so far. I want to achieve this:
according to the number of blue box, only 3 may go to a line. The other goes to new line, but have to be centered. But I dont know how to do the margin, because no left-right margin is needed, still there has to be distances from each other
Upvotes: 0
Views: 51
Reputation: 115240
I think display:inline-block
is what you are after.
CSS
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#container
{
text-align: center;
background-color: green;
font-size:0;
margin-bottom:10px;
}
.box
{
display: inline-block;
width:33%;
margin-bottom:10px;
}
Upvotes: 1
Reputation: 362
<div id="container">
<center>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
<div class="box"><img src="http://developer-static.se-mc.com/wp-content/blogs.dir/1/files/2012/12/ScaleImages.jpg" width="100" height="100" /></div>
</center>
</div>
Upvotes: 0
Reputation: 339
.box{
margin: 0 auto;
}
This aligns items to the center. You can also set a margin value by changing the 0.
eg: margin: 20px auto;
Upvotes: 0
Reputation: 324710
Add width:400px
to your container (this will snugly fit three 100px images with 50px margins between them), and add the following rule:
.box:nth-child(3n+1) {margin-left:0}
Note also that to use display:inline-block
, you cannot have any spaces between the elements, as this will mess up your layout.
Upvotes: 1