[英]How do I center element within a container while keeping them vertically aligned?
Not really sure how to properly title this. 不确定如何正确地标题这个。 It is best explained with images.
最好用图像解释。 I am trying to keep the items centered, but when there is too many to fit properly on one line, the new line should be aligned with the item above it, not centered within the container.
我试图将项目保持在中心位置,但是当在一行上有太多不能正确匹配时,新行应与其上方的项目对齐,而不是在容器中居中。
I am trying to achieve this: 我想要实现这个目标:
Instead of this:
而不是这个:
Here is my JSfiddle: 这是我的JSfiddle:
http://jsfiddle.net/6kwzy6La/ http://jsfiddle.net/6kwzy6La/
And my HTML: 我的HTML:
<div class="container2">
<div class="content">
<div id="box">
<p> Some text goes here </p>
</div>
<div id="box">
<p> Some text goes here </p>
</div>
<div id="box">
<p> Some text goes here </p>
</div>
<div id="box">
<p> Some text goes here </p>
</div>
<div id="box">
<p> Some text goes here </p>
</div>
</div>
</div>
http://jsfiddle.net/6kwzy6La/ http://jsfiddle.net/6kwzy6La/
Container1
{
text-align: center;
}
Container2
{
text-align: left;
}
One approach is to add an appropriate right margin to the last box if there are a odd number of boxes, like this 一种方法是在最后一个框中添加适当的右边距,如果有奇数个框,就像这样
#box:last-child:nth-child(2n + 1) {
margin-right:240px;
}
See http://jsfiddle.net/6kwzy6La/6/ 见http://jsfiddle.net/6kwzy6La/6/
Note that you should be using class
not id
as id
s should be unique within the document. 请注意,您应该使用
class
not id
因为id
在文档中应该是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.