简体   繁体   English

HTML / CSS - 中心对齐UL导航菜单

[英]HTML / CSS - Center align a UL navigation menu

Im trying to centre align a image based navigation menu, i have read a few different posts, but none of them seem to be working. 我试图中心对齐基于图像的导航菜单,我已经阅读了几个不同的帖子,但它们似乎都没有工作。

My HTML is as follows: 我的HTML如下:

<ul id="nav">
    <li id="films"><a href="#">Films</a></li>
    <li id="music"><a href="#">Music</a></li>
    <li id="contact"><a href="#">Contact</a></li>
    <br class="clear">
</ul>

and my CSS is as follows: 我的CSS如下:

#nav {
    width: 566px;
    list-style: none;
    margin: 0;
    padding: 0;
    margin: 0 auto;
}
#nav li {
    float: left;
    margin: 0 10px;
}
#nav li a {
    display: block;
    text-indent: -9999px;
    overflow: hidden;
    height: 16px;
}

#nav li#films a {
    background: url(images/FILMS.png) no-repeat;
    width: 59px;
}
#nav li#music a {
    background: url(images/MUSIC.png) no-repeat;
    width: 70px;
}
#nav li#contact a {
    background: url(images/CONTACT.png) no-repeat;
    width: 107px;
}
.clear {
    clear: both;
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you 谢谢

li a needs to be display: block or inline-block to remove the text with text-indent: -9999px; li a需要display: blockinline-block以删除带有text-indent: -9999px;的文本text-indent: -9999px; .

The display: inline-block on #nav li places each link next to each other. display: inline-block #nav li上的display: inline-block将每个链接放在#nav li

Have a fiddle! 有一个小提琴!

HTML HTML

<ul id="nav">
    <li id="films"><a href="#">Films</a>

    </li>
    <li id="music"><a href="#">Music</a>

    </li>
    <li id="contact"><a href="#">Contact</a>

    </li>
</ul>

CSS CSS

#nav {
    width: 306px;
    /* Wide enough for all links */
    list-style: none;
    padding: 0;
    margin: 0 auto;
    text-align: center;
}
#nav li {
    margin: 0 10px;
    display: inline-block;
}
#nav li a {
    text-indent: -9999px;
    display: block;
    height: 16px;
}
#nav li#films a {
    background: url(http://www.placehold.it/59X16) no-repeat;
    width: 59px;
}
#nav li#music a {
    background: url(http://www.placehold.it/70X16) no-repeat;
    width: 70px;
}
#nav li#contact a {
    background: url(http://www.placehold.it/107X16) no-repeat;
    width: 107px;
}

Change to this: 改为:

#nav {
    width: 566px;
    list-style: none;
    padding: 0;
    margin: 0 auto;
    text-align: center;
}
#nav li {
    margin: 0 10px;
    display: inline-block;
}
#nav li a {
    text-indent: -9999px;
    overflow: hidden;
    height: 16px;
}

JSFiddle 的jsfiddle

try to edit these lines like this: 尝试像这样编辑这些行:

#nav li a {
display: block;
text-indent: -9999px;
overflow: hidden;
height: 16px;
background: no-repeat center center;
}

#nav li#films a {
background-image: url(images/FILMS.png);
width: 59px;
}
#nav li#music a {
background-image: url(images/MUSIC.png);
width: 70px;
}
#nav li#contact a {
background-image: url(images/CONTACT.png);
width: 107px;
}

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

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