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

后端 未结 10 1104
深忆病人
深忆病人 2020-12-28 14:57

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

.navigation ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.naviga         


        
相关标签:
10条回答
  • 2020-12-28 15:18

    If you want to keep using your floated LI in your code, you can use this:

    JSFiddle: https://jsfiddle.net/Lvujttw3/

    <style>
    .wrapper {
        width: 100%;
        background-color:#80B5EB;
        text-align: center;
        }
    .intWrapper {
        display: inline-block;
        }
    .mainMenu {
        padding: 0;
        min-height: 40px;
        margin:auto;
        }
    ul {
        list-style-type: none;
        }
    ul li {
        float: left;
        font-size: 15px;
        line-height: 40px;
        }
    ul li A {
        display: block;
        color: white;
        font-weight: bold;
        font-family: Arial;
        text-decoration: none;
        min-height: 40px;
        padding: 0 36px;
        }
    </style>
    
    <div class="wrapper">
        <div class="intWrapper"> 
            <ul class="mainMenu">   
                <li><a href="one.htm" style='background-color:red'>ITEM ONE</a>
                </li><li><a href="two.htm">ITEM TWO</a>
                </li><li><a href="three.htm" style='background-color:red'>ITEM THREE</a>        
                </li>   
            </ul></div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-28 15:19

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

    0 讨论(0)
  • There are few settings like float, margin which may affect this code to work properly. It works in IE7 too. I got this code from an article over at CSS Wizardry.

    .navigation ul 
    {
        list-style: none;
        padding: 0;
        margin: 0;
        text-align: center;/*make this change*/
     }
    .navigation li
     {
        float: none;/*make this change*/
        display:inline;/*make this change*/
        margin: 0 1.15em;
     /* margin: 0 auto; */
     }
     .navigation li a {
        display:inline-block;/*make this change*/
     }
     .navigation 
     {
      /*width: auto;*/
      /*margin: 0 auto;*/
        text-align: center;
     }
    
    0 讨论(0)
  • 2020-12-28 15:21

    This one works great with me! (if I'm correct: IE7+)

    Fiddle: http://jsfiddle.net/fourroses666/zj8sav9q/3/

    .nav{list-style:none; text-align:center;}
    .nav ul{list-style:none;}
    .nav li{display:inline;}
    .nav a{text-decoration:none; font-size:20px; line-height:20px; display:inline-block;}
    
    <nav class="nav" role="navigation" aria-label="main navigation">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu</a></li>
        <li><a href="#">Onze producten</a></li>
        <li><a href="#">Impressie</a></li>
        <li><a href="#">Media</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>
    
    0 讨论(0)
提交回复
热议问题