将导航栏居中

发布于 2025-01-02 21:26:07 字数 1692 浏览 0 评论 0 原文

使用以下 CSS,我尝试在页面顶部创建一个导航栏(固定在顶部),但我希望它居中,而不是位于屏幕的绝对左侧。

ul#list-nav
{
    position:fixed;
    top:0;
    left:0;
    margin:auto;
    padding:0px;
    width:100%;
    height:28px;
    font-size:120%;
    display:inline;
    text-decoration:none;
    list-style:none;
    background-color: #1F1F1F;
    border:none;
    z-index:1000;
}

ul#list-nav li
{
    float:left;
}
ul#list-nav a
{
    background-color:#1F1F1F;
    color:#C4C4C4;
    /*display:block;*/
    padding:5px 15px;
    text-align: center;
    text-decoration:none;
    font-size:14px;
}

ul#list-nav a:hover
{
    background-color:#4D4D4D;
    text-decoration:none;
}

ul#list-nav a:active
{
    background-color:#9C9C9C;
    text-decoration:none;
}

到目前为止的尝试使其成为垂直列表,或使按钮从中心开始(而不是居中)。我怎样才能做到这一点?

编辑:下面是 HTML ...这个列表是唯一被设计样式的东西。

<ul id="list-nav">
            <li><a href="/ourbs/index.php">Home</a></li>
            <li><a href="/ourbs/categories/projects.php">Projects</a></li>
            <li><a href="/ourbs/categories/opinion.php">Opinion</a></li>
            <li><a href="/ourbs/categories/humour.php">Humour</a></li>      
            <li><a href="/ourbs/categories/games.php">Games</a></li>
            <li><a href="/ourbs/categories/movies.php">Movies</a></li>
            <li><a href="/ourbs/categories/tvshows.php">TV Shows</a></li>
</ul>

EDIT2:如果这有帮助的话,这是一个 jsFiddle http://jsfiddle.net/752jU/1/

Using the following CSS, I'm trying to make a navigation bar at the top of the page (fixed to the top) but instead of it being on the absolute left of the screen, I want it centered.

ul#list-nav
{
    position:fixed;
    top:0;
    left:0;
    margin:auto;
    padding:0px;
    width:100%;
    height:28px;
    font-size:120%;
    display:inline;
    text-decoration:none;
    list-style:none;
    background-color: #1F1F1F;
    border:none;
    z-index:1000;
}

ul#list-nav li
{
    float:left;
}
ul#list-nav a
{
    background-color:#1F1F1F;
    color:#C4C4C4;
    /*display:block;*/
    padding:5px 15px;
    text-align: center;
    text-decoration:none;
    font-size:14px;
}

ul#list-nav a:hover
{
    background-color:#4D4D4D;
    text-decoration:none;
}

ul#list-nav a:active
{
    background-color:#9C9C9C;
    text-decoration:none;
}

Attempts so far make it a vertical list, or make the buttons start in the center (rather than be centered). How can I accomplish this?

EDIT: below is the HTML ... this list is the only thing being styled.

<ul id="list-nav">
            <li><a href="/ourbs/index.php">Home</a></li>
            <li><a href="/ourbs/categories/projects.php">Projects</a></li>
            <li><a href="/ourbs/categories/opinion.php">Opinion</a></li>
            <li><a href="/ourbs/categories/humour.php">Humour</a></li>      
            <li><a href="/ourbs/categories/games.php">Games</a></li>
            <li><a href="/ourbs/categories/movies.php">Movies</a></li>
            <li><a href="/ourbs/categories/tvshows.php">TV Shows</a></li>
</ul>

EDIT2: here's a jsFiddle if this helps
http://jsfiddle.net/752jU/1/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹倦 2025-01-09 21:26:07

您只需要一个包装器 div 即可完成此操作...

http://jsfiddle.net /752jU/5/

注意:通过使用 display: inline 我的答案也是 擅长IE 6 和7,如果这很重要。

CSS:

div#wrapper {
    position: fixed;
    top: 0;
    width: 100%;
    text-align: center;
    height:28px;
    background-color: #1F1F1F;
    border: none;
    z-index: 1000;
}
ul#list-nav {
    padding: 0px;
    width: auto;
    font-size: 120%;
    text-decoration: none;
    list-style: none;
}
ul#list-nav li {
    display: inline;
}

HTML:

<div id="wrapper">
    <ul id="list-nav">
        <li><a href="/index.php">Home</a></li>
        <li><a href="/ourbs/categories/projects.php">Projects</a></li>
        <li><a href="/categories/opinion.php">Opinion</a></li>
        <li><a href="/categories/humour.php">Humour</a></li>        
        <li><a href="/categories/games.php">Games</a></li>
        <li><a href="/categories/movies.php">Movies</a></li>
        <li><a href="/categories/tvshows.php">TV Shows</a></li>
    </ul>
</div>

You only need one wrapper div to accomplish this...

http://jsfiddle.net/752jU/5/

Note: By using display: inline my answer is also good in IE 6 & 7, if that matters.

CSS:

div#wrapper {
    position: fixed;
    top: 0;
    width: 100%;
    text-align: center;
    height:28px;
    background-color: #1F1F1F;
    border: none;
    z-index: 1000;
}
ul#list-nav {
    padding: 0px;
    width: auto;
    font-size: 120%;
    text-decoration: none;
    list-style: none;
}
ul#list-nav li {
    display: inline;
}

HTML:

<div id="wrapper">
    <ul id="list-nav">
        <li><a href="/index.php">Home</a></li>
        <li><a href="/ourbs/categories/projects.php">Projects</a></li>
        <li><a href="/categories/opinion.php">Opinion</a></li>
        <li><a href="/categories/humour.php">Humour</a></li>        
        <li><a href="/categories/games.php">Games</a></li>
        <li><a href="/categories/movies.php">Movies</a></li>
        <li><a href="/categories/tvshows.php">TV Shows</a></li>
    </ul>
</div>
权谋诡计 2025-01-09 21:26:07

您需要将列表插入到 3 个嵌套 div 中:

在 css 中:

div#container1
{
  position:fixed;
  top:0;
  left:0;
  width:100%
}

div#container2
{
  margin-left:auto;
  margin-right:auto;
  text-align: center;
}

div#container3
{  
  display:inline-block; 
}

然后在 html 中:

<div id="container1">
<div id="container2">
<div id="container3">
<ul id="list-nav">
...
</ul>
</div>
</div>
</div>

请参阅 http://jsfiddle.net /jZQ4v/ 用于正确居中的代码版本

You need to insert the list in 3 nested divs:

In your css:

div#container1
{
  position:fixed;
  top:0;
  left:0;
  width:100%
}

div#container2
{
  margin-left:auto;
  margin-right:auto;
  text-align: center;
}

div#container3
{  
  display:inline-block; 
}

Then in your html:

<div id="container1">
<div id="container2">
<div id="container3">
<ul id="list-nav">
...
</ul>
</div>
</div>
</div>

See http://jsfiddle.net/jZQ4v/ for a version of your code that center properly

ˇ宁静的妩媚 2025-01-09 21:26:07

使用:

#list-nav {
    padding: 0px;
    height: 28px;
    font-size: 120%;
    margin: auto;
    text-align: center;
    list-style: none;
    background-color: #1F1F1F;
    border: none;
    z-index: 1000;
}
#list-nav li {    
    display: inline;    
    text-align: center;
}
#list-nav a {   
    color: #C4C4C4;
    background-color: #1F1F1F;   
    display: inline;   
    text-decoration: none;
    padding: 5px 15px;    
    font-size: 14px;
}
#list-nav a:hover {  
    background-color: #4D4D4D;   
    text-decoration: none;
}
#list-nav a:active {   
    background-color: #9C9C9C;  
    text-decoration: none;
}

我删除了所有不必要的编码,这些编码没有以任何方式改变菜单。

请在此处查看现场演示:http://jsfiddle.net/YFDeX/1/

编辑:为了与 IE6+ 兼容而进行了更改

希望这会有所帮助。

Use:

#list-nav {
    padding: 0px;
    height: 28px;
    font-size: 120%;
    margin: auto;
    text-align: center;
    list-style: none;
    background-color: #1F1F1F;
    border: none;
    z-index: 1000;
}
#list-nav li {    
    display: inline;    
    text-align: center;
}
#list-nav a {   
    color: #C4C4C4;
    background-color: #1F1F1F;   
    display: inline;   
    text-decoration: none;
    padding: 5px 15px;    
    font-size: 14px;
}
#list-nav a:hover {  
    background-color: #4D4D4D;   
    text-decoration: none;
}
#list-nav a:active {   
    background-color: #9C9C9C;  
    text-decoration: none;
}

I removed all unnecessary coding which didn't alter the menu in any way.

See a live demo here: http://jsfiddle.net/YFDeX/1/

Edit: Altered for compatibility with IE6+

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文