Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distribute HTML list items evenly in an unordered list

Tags:

I want my li elements that form a horizontal menu to be distributed evenly across the width of my ul element. I normally float my li elements to the left and add some margin. But how do I put the proper spacing so they extend from the left of my ul element to the right edge?

Here's an example jsfiddle of a menu not distributed across the ul.

The spacing has to be the same between each li. But the li elements may be different lengths.

like image 947
at. Avatar asked Nov 12 '12 09:11

at.


People also ask

How Align Center unordered list HTML?

Center-align the <div> element, and change the display of <ul> to inline-block .

How do you evenly distribute items in CSS?

The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.

How do you align 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. Float the list items.

How do you make an unordered list a list with the items in bullets?

To create unordered list in HTML, use the <ul> tag. The unordered list starts with the <ul> tag. The list item starts with the <li> tag and will be marked as disc, square, circle, etc. The default is bullets, which is small black circles.


1 Answers

Yet another approach. This is something I use when trying to span a menu evenly across the page. It is nice if you have a dynamic menu that will change depending on certain conditions (like an admin page that only shows up if you are logged in as an admin).

CSS:

nav div ul { display: table; width: 100%; list-style: none; } nav div ul li { display: table-cell; text-align: center; } nav div ul li a { display: block; } 

I was kinda lazy, and just copied this from my current project, so you will have to adapt it to fit your code, but it is my preferred method of creating a horizontal menu

EDIT: You can make it look like it spans better by adding this:

CSS:

nav div ul li:first-child {     text-align: left; } nav div ul li:last-child {     text-align: right; } 

again, untested, just typed.

like image 177
FireCrakcer37 Avatar answered Oct 28 '22 23:10

FireCrakcer37