Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to centrally align a float: left ul/li navigation menu with css?

So I have the following CSS in place to display a horizontal navigation bar using:

.navigation ul {   list-style: none;   padding: 0;   margin: 0; }  .navigation li {   float: left;   margin: 0 1.15em;   /*    margin: 0 auto;*/ }  .navigation {   /*    width: auto;*/   /*    margin: 0 auto;*/   text-align: center; } 

My question is: how do I align the navigation bar centrally above the title?

like image 431
Adam Taylor Avatar asked Jun 28 '09 15:06

Adam Taylor


People also ask

How do I center my UL list in CSS?

Just give the list centered text (e.g. ul. nav { text-align: center; } ) and the list items inline-block (e.g. ul. nav li { display: inline-block; } ). If you want to do it with margin for whatever reason, look into width: fit-content; .

How do I center a navigation menu in CSS?

Make your div container the 100% width. and set the text-align: element to center in the div container. Then in your <ul> set that class to have 3 particular elements: text-align:center; position: relative; and display: inline-block; that should center it.

How do you center a float in CSS?

There is no way to float center in CSS layout. So, we can center the elements by using position property. Example 1: This example set the position of elements exactly at the center of the screen.


1 Answers

Here's a solution that doesn't require specifying the width of your ul.

like image 51
Ben Marini Avatar answered Oct 01 '22 02:10

Ben Marini