简体   繁体   English

使用html和CSS将菜单居中出现问题

[英]Trouble centering my menu using html and css

Hello I am having trouble centering this menu. 您好,我在将此菜单居中时遇到麻烦。 I have tried changing the margins and also using the center tag and using a div with no success. 我尝试更改页边距,也使用center标签和使用div,但均未成功。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

Here is the JSFiddle: http://jsfiddle.net/G8gE2/ 这是JSFiddle: http : //jsfiddle.net/G8gE2/

Here is the css: 这是CSS:

ul# nave {
    margin - left: auto;
    margin - right;
    auto;
}
ul.drop a {
    display: block;
    color: #fff;
    font - family: arial;
    font - size: 18px;
    text - decoration: none;
}
ul.drop, ul.drop li, ul.drop ul {
    list - style: none;
    margin: ;
    padding: 0;
    border: 1px solid# fff;
    background: #009933; color:       # fff;
}
ul.drop {
    position: relative;
    z - index: 597;
    float: left;
}
ul.drop li {
    float: left;
    line - height: 1.3em;
    vertical - align: middle;
    zoom: 1;
    padding: 5px 10px;
}
ul.drop li.hover, ul.drop li: hover {
    position: relative;
    z - index: 599;
    cursor: default;
    background: #1e7c9a; }
     ul.drop ul{visibility:hidden; position: absolute; top:100%; left:  0;z-    index:598; width:195px; background:# 009933;
    border: 1px solid# fff;
}
ul.drop ul li {
    float: none;
}
ul.drop ul ul {
    top: -2px;
    left: 100 % ;
}
ul.drop li: hover > ul {
    visibility: visible
}

To align div in the middle you need to give width to your ul. 要在中间对齐div,您需要给ul指定宽度。 that is 那是

ul#nave
{
    width:500px;
}

and remove float:left from CSS ul.drop 并从CSS ul.drop中删除float:left

You could add the following css: 您可以添加以下css:

#nave {
    margin: 0 auto;
    width: 400px;
    float: none;
}

You need to define a fixed width to your menu so it could be centered. 您需要为菜单定义一个固定的宽度,以便可以居中。

http://jsfiddle.net/G8gE2/1/ http://jsfiddle.net/G8gE2/1/

Put this css to center your menu: CSS: 将此CSS置于菜单中心: CSS:

ul.drop {
margin: 0 auto;
position: relative;
width: 315px;
z-index: 597;

} }

Try this 尝试这个

ul#nave {
    margin: 0 auto;
    width: 50%;
    float:none
}

OR 要么

<div style="display: table; margin-right: auto; margin-left: auto;">
//Your Codes
</div>

DEMO 演示

You can: 您可以:

  • Wrap it with a container, with text-align: center and 100% width; 用文本对齐的容器包装:居中,宽度为100%;
  • Make the nave inline-block and remote the float: left 使中殿直列式块并使其漂浮在远程位置:向左

http://jsfiddle.net/G8gE2/3/ http://jsfiddle.net/G8gE2/3/

#menu_wrapper {
   width: 100%;
   text-align: center;
}
ul#nave {
   display: inline-block;   
}
#nave li:hover ul {
    left: -50%;
}

I added the above to your code 我将以上内容添加到您的代码中

Updated Fiddle Demo of Yours 您的最新小提琴演示

Hope this helps 希望这可以帮助

Happy coding 快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM