Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal Centering of Multi-Line List Items in Unordered List

Tags:

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:

illustrationhttp://grab.by/8UIl

like image 479
John Avatar asked Feb 12 '11 02:02

John


People also ask

How do I center an unordered list horizontally?

If you want to make this navigational unordered list horizontal, you have basically two options: Make the list items inline instead of the default block. .li { display: inline; } This will not force breaks after the list items and they will line up horizontally as far as they are able.

How do you center align an unordered list?

To center align an unordered list, you need to use the CSS text align property. In addition to this, you also need to put the unordered list inside the div element. Now, add the style to the div class and use the text-align property with center as its value.

How do you place an element horizontally in HTML?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.


1 Answers

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/

like image 112
Moses Avatar answered Sep 24 '22 14:09

Moses