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. 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
. It's not a popular answer but sometimes using a table is the most practical, as is well argued in this SO response.
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. Again, I would try to center the ul in a container, not the li
in the ul
. 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.
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. 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.