Horizontal Centering of Multi-Line List Items in Unordered List
14,012
Here is the best method I could find.
ul li {
/* make list elements fall inline as block elements */
position: relative;
display: inline-block;
/* next two lines only for display purposes */
text-align: center;
border:1px solid red;
}
/* horizontally center ul element */
ul { text-align:center; }
See the link for an example: http://jsfiddle.net/gfkPG/
Related videos on Youtube
Author by
John
Updated on May 20, 2022Comments
-
John almost 2 years
I have an unordered list that wraps onto a second line, and needs to be centered horizontally within the containing UL. Each LI is a set width and height. I've seen many approaches that work for a single line, but nothing I've tried is working when the list wraps to a second line. Would be ideal if this worked in IE7+ -- Thanks for the help.
See here for an illustration:
-
John about 13 yearsOMG LOL -- Such a simple solution -- so used to having to employ complicated work-arounds, I forgot to look at the obvious. Thanks much!
-
John about 13 yearsSorry, jumped the gun there, that doesn't work in IE7 for some reason.
-
John about 13 yearsThere's always got to be a hack for IE :) Here's what I found: zoom:1;*display:inline; in a conditional CSS did the trick. Thanks again.