how can I center this menu with CSS

后端 未结 4 1329
粉色の甜心
粉色の甜心 2021-01-15 06:29

I have this menu fixed, how can I center this menu?
I already tried with margin: 0 auto, and float: left, but it doesn\'t work.
Are there any way to center ??
he

相关标签:
4条回答
  • 2021-01-15 06:58

    You can change the #mainnav li as from float: left; to display: inline-block;

    #main-nav li {
        position: relative;
        margin: 0 auto;
        padding: 0;
        list-style: none;
        display: inline-block;
    }
    

    and then you can use text-align: center; on the ul.

    #main-nav {
    ...
         text-align: center;
    ...
    }
    

    Working Demo: http://jsfiddle.net/rSc9s/

    0 讨论(0)
  • 2021-01-15 06:58

    To make it vertically center need to remove display from the #main-nav li

    #main-nav li {
        position: relative;
        margin: 0 auto;
        padding: 0;
        list-style: none;
    }
    

    Add text-align: center; in #main-nav

    0 讨论(0)
  • 2021-01-15 06:58

    try this:

    <style>
    #main-nav li {
        margin: 0 auto;
        padding: 0;
        list-style: none;
        display: inline-block;
        position: relative;
    }
    </style>
    
    0 讨论(0)
  • 2021-01-15 07:06
    #main-nav { list-style: none outside; text-align: center; }
    
    #main-nav li { display: inline; }
    
    #main-nav li a { display: inline-block; }
    

    I think this is what you are looking for

    0 讨论(0)
提交回复
热议问题