[英]Horizontally centering a list with left-floated, left-aligned items
I have a div
containing several section
s. 我有一个包含几个
section
的div
。 One of the section
s contains an ol
, and all of its li
s are floated left. 其中一个
section
包含一个ol
,并且它的所有li
都向左浮动。 I am trying to make the ol
shrink to contain as many li
s as it can on one line, and horizontally center the ol
while keeping the li
s left-aligned. 我试图让
ol
缩小到包含多达li
S作为它可以在同一行,并且水平居中的ol
,同时保持了li
的左对齐。
Here's what I have: http://jsbin.com/ocokog/1/edit 这就是我所拥有的: http : //jsbin.com/ocokog/1/edit
<div id="wrapper">
<section id="section">
<ol id="list">
<li id="i0"></li>
<li id="i1"></li>
<li id="i2"></li>
<li id="i3"></li>
<li id="i4"></li>
</ol>
</section>
</div>
Non-decorative CSS: 非装饰性CSS:
#wrapper {
width: 85%;
text-align: center;
}
#section {
display: inline-block;
}
#list {
list-style-type: none;
margin: 0;
padding: 0;
overflow: auto;
}
#list > li {
width: 100px;
height: 100px;
float: left;
}
When the li
s can all fit on one line, the list is centered: 如果
li
可以全部放在一行上,则列表居中:
When the li
s fall onto a second line, the section
and ol
grow and the whole thing is left-aligned: 当
li
落到第二行时, section
和ol
增长,整个部分左对齐:
I am trying to make it look like this (depending on the width available, also the section
can shrink if necessary): 我试图让它看起来像这样(取决于可用的宽度,如果需要,该
section
也可以收缩):
Setting a width or max width and using margin: auto
also doesn't quite work: http://jsbin.com/ocokog/6/edit 设置宽度或最大宽度并使用
margin: auto
也不太起作用: http : //jsbin.com/ocokog/6/edit
Why won't the ol
shrink? ol
为什么不缩小? How can I fix this? 我怎样才能解决这个问题?
These questions are similar but different: 这些问题相似但不同:
The ol
is centered. ol
以中心为中心。 You've set it to be 65%
width. 你已将它设置为
65%
宽度。 If you look at the orange border when you re-size it, it's always centered. 如果在重新调整大小时查看橙色边框,它始终居中。
Let me try to rephrase this a little. 让我试着改写一下。
ol
is a block element, the same way that a div
is a block element. ol
是块元素,与div
是块元素的方式相同。 This means that it fills up the space that it is in. This is represented by the orange border in your example. 这意味着它填满了它所在的空间。这在您的示例中由橙色边框表示。 What you want, is for the
ol
to take up less than 100% of the space. 你想要的是,
ol
占据的空间不到100%。 There are many different ways to this, but you always start with the list taking up as much space as it can. 有许多不同的方法,但你总是从列表中占用尽可能多的空间。
One of the ways is to set the width. 其中一种方法是设置宽度。 The problem with this is that you don't want a fixed width.
这个问题是你不想要一个固定的宽度。 So you tried setting the width by using percentages.
所以你尝试使用百分比来设置宽度。 This doesn't work because there are many intervals where a percentage will cause the
li
to not be centered. 这不起作用,因为有许多间隔,百分比将导致
li
不居中。
Another method would be to set the left and right margin's of the ol
. 另一种方法是设置
ol
左右边距。 This works the same as setting the width. 这与设置宽度相同。
There just isn't a way to center something in an element that you also want to float:left;
没有办法在你想要
float:left;
的元素中居中东西float:left;
. 。 I think the confusion comes in because you think that you can make the
ol
wrap around the li
. 我想混淆的来源,因为你认为你可以让
ol
周围的包裹li
。 The problem is, that isn't how the ol
works. 问题是,这不是
ol
工作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.