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.
Center-align the <div> element, and change the display of <ul> to inline-block .
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With