简体   繁体   English

在'ul'中垂直对齐'li'中间

[英]Vertically align 'li' middle in 'ul'

I have a simple unordered list like so: 我有一个简单的无序列表,如下所示:

<ul class="flavours">   
     <li>Chocolate</li>
     <li>Caramel</li>
     <li>Watermelon</li>
</ul>

<ul class="flavours"> has a min-height of 200px and it grows as more <li> elements are added to the list via jQuery ui drag and drop library. <ul class="flavours">min-height200px ,随着更多<li>元素通过jQuery ui拖放库添加到列表中,它会增长。

When there is just one or two <li> elements in the list, how can I vertically align these in the middle of the whole <ul> . 当列表中只有一个或两个<li>元素时,如何在整个<ul>的中间垂直对齐它们。 So as when the height is 200px the items would be dead center, horizontally and vertically. 因此当高度为200px ,物品将是死点,水平和垂直。 Currently they are just positioned center top. 目前他们只是位于中心位置。

Centering things vertically tends to be a tricky affair if you want cross-browser support, especially for IE and old browsers - unless you're willing to use JavaScript or a table. 如果你想要跨浏览器支持,特别是对于IE和旧浏览器,除非你愿意使用JavaScript或表格,否则垂直居中往往是一件棘手的事情。 All common browsers support vertical centering within a table cell, so one option is to forget centering the <li> elements in the <ul> and instead create a table with height: 100% and one cell inside, with vertical-align: middle . 所有常用的浏览器支持一个表格单元格内垂直居中,所以一个选择是忘记定心<li>中的元素的<ul>和,而不是创建一个tableheight: 100%和内一个小区,以vertical-align: middle It's not a popular answer but sometimes using a table is the most practical, as is well argued in this SO response . 这不是一个流行的答案,但有时使用表是最实用的,正如在这个SO响应中所论证的那样。

If you do not want to use a table and can't use the display:table-cell for browser support, then you'll probably need to use JavaScript. 如果您不想使用表并且不能使用display:table-cell来支持浏览器,那么您可能需要使用JavaScript。 Again, I would try to center the ul in a container, not the li in the ul . 同样,我会尽量居中UL在一个容器中,而不是liul The approach is generally to find the height of the container, find the height of the ul , take the difference, cut it in half, and set the top or the margin-top of the ul to that value, depending on whether you want to use absolute positioning or not. 方法通常是找到容器的高度,找到ul的高度,取差异,将其切成两半,并将ultopmargin-top设置为该值,具体取决于您是否要是否使用absolute定位。

The exact best solution is hard to give without seeing the context of the list in your page. 如果没有看到页面中列表的上下文,很难给出最精确的解决方案。

Here's a fiddle using absolute positioning and JavaScript. 这是一个使用absolute定位和JavaScript 的小提琴 Using margin-top and default position in this context is tricky because of margin collapse with the outer div, but if you can use it then you can use margin: 0 auto to do the horizontal centering, which is nice because then you can ignore the width. 在这种情况下使用margin-top和默认position是棘手的,因为外部div会导致边距崩溃,但是如果你可以使用它,那么你可以使用margin: 0 auto来进行水平居中,这很好,因为你可以忽略宽度。

Flexbox can do this: Flexbox可以这样做:

http://jsfiddle.net/vUSmV/2/ http://jsfiddle.net/vUSmV/2/

.flavours {
  min-height: 10em;
  display: -webkit-box;
  display: -moz-box;
  display: -webkit-flexbox;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-orient: vertical;
  -moz-box-orient: vertical;
  -webkit-box-direction: normal;
  -moz-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -moz-box-pack: center;
  -webkit-flex-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-flex-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;

}

Using Flexbox with inline list items that wrap (note that wrapping reduces browser support to Chrome, Opera, and IE10): 将Flexbox与包装的内联列表项一起使用(请注意,包装会减少浏览器对Chrome,Opera和IE10的支持):

http://jsfiddle.net/vUSmV/4/ http://jsfiddle.net/vUSmV/4/

.flavours {
  min-height: 10em;
  border: 1px solid;
  display: -webkit-flexbox;
  display: -ms-flexbox;
  display: -webkit-flex;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-flex-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-flex-line-pack: center;
  -ms-flex-line-pack: center;
  -webkit-align-content: center;
  align-content: center;
  -webkit-flex-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}
@supports (flex-wrap: wrap) {
  .flavours {
    display: flex;
  }
}

.flavours li {
    margin: .5em 1em;
}

Or you can convert your list into a table-cell element: 或者您可以将列表转换为table-cell元素:

http://jsfiddle.net/vUSmV/1/ http://jsfiddle.net/vUSmV/1/

.flavours {
  min-height: 10em;
  display: table-cell;
  vertical-align: middle;
}

I would personally center everything within a parent div ... A working JSFiddle can be found here . 我个人将所有内容放在父div ...... 在这里可以找到一个工作的JSFiddle

HTML: HTML:

<div>
<ul class="flavours">   
 <li>Chocolate</li>
 <li>Caramel</li>
 <li>Watermelon</li>
</ul>
</div>

CSS: CSS:

div{
height: 200px;
display: table-cell;
vertical-align: middle;
border: 1px solid black;
}

Or you can just add the same styling to the ul if you would prefer. 或者你可以根据需要为ul添加相同的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM