After some search, I did not find a proper way to center a list of <li>
into a fixed width div
.
Take a look at the page.. It doesn't work either!
After some search, I did not find a proper way to center a list of <li>
into a fixed width div
.
Take a look at the page.. It doesn't work either!
Could either be
or
depends on how it should look like (or combining those)
I have been looking for the same case and tried all answers by change the width of
<li>
.Unfortunately all were failed to get the same distance on left and right of the
<ul>
box.The closest match is this answer but it needs to adjust the change of width with padding
See the result below, all distance between
<li>
also to the<ul>
box are the same.You may check it on this jsFiddle:
http://jsfiddle.net/qwbexxog/14/
This is a better way to center UL's inside of any DIV container.
This CSS solution does not use Width and Float properties. Float:Left and Width: 70%, will cause you headaches when you need to duplicate your menu on different pages with different menu items.
Instead of using width, we use padding and margin to determine the space around the text/menu item. Also, instead of using Float:Left in the LI element, use display:inline-block.
By floating your LI left, you literally float your content to the left and then you must use one of the Hacks mentioned above to center your UL. Display:inline-block creates your Float property for you (sort of). It takes your LI element and turns it into a block element that lays side by side each other (not floating).
With Responsive design and using frameworks like Bootstrap or Foundation, there will be issues when trying to float and center content. They have some built-in classes, but it's always better to do it from scratch. This solution is much better for dynamic menus (Such as Adobe Business Catalyst menu system).
Reference for this tutorial can be found at: http://html-tuts.com/center-div-image-table-ul-inside-div/
HTML
CSS
I love flexbox:
To center the ul and also have the li elements centered in it as well, and make the width of the ul change dynamically, use display: inline-block; and wrap it in a centered div.
Update
Here is a jsFiddle link to the code above.
Interesting but try this with floated li elements inside the ul: Example here
The problem now: the ul needs a fixed width to actually sit in the center. However we want to be it relative to the container width (or dynamic), margin: 0 auto on the ul does not work.
A better way is to let go of UL/Li list and use a different approach example here