html - 将标题定位在中心

标签 html css header nav

我的页眉有一些小问题,它必须是对称完美的,我在这方面很糟糕所以你能告诉我如何使它位于中心并保持链接之间的边距吗?

它应该是什么样子:

enter image description here

我也很想知道如何在悬停和简单的语言切换器上制作下划线。

我的代码:

  <nav id="nav">
    <ul style="text-transform:uppercase">
        <li><a href="#intro">Strona główna</a></li>
        <li><a href="#one">Portfolio</a></li>
        <li><a href="#"><img src="logo.png"></img></a></li>
        <li><a href="#two">O mnie</a></li>
        <li><a href="#contact">Kontakt</a></li>
    </ul>
  </nav>

CSS:

#nav {
    position: absolute;
    right: 0.5em;
    top: 0;
    height: 3em;
    line-height: 3em;
    margin-left:30%
}

    #nav ul {
        margin: 0;
    }

        #nav ul li {
            display: inline-block;
            margin-left: 0.5em;
            font-size: 0.9em;
        }

            #nav ul li a {
                display: block;
                color: inherit;
                text-decoration: none;
                height: 3em;
                line-height: 3em;
                padding: 0 0.5em 0 0.5em;
                outline: 0;
            }

最佳答案

从您的代码中删除了大量的 marginpadding

添加position: relative;, text-align: center;, width: 100%;#nav.

此外,将 margin:0;padding:0; 添加到 *

注意:由于某些原因,您在 li 中没有 img

对此的更改:

*{
    margin:0;
    padding:0;
}
#nav {
    position: relative;
    top: 0;
    height: 3em;
    line-height: 3em;
    text-align: center;
    width: 100%;
}
#nav ul {
    margin: 0;
}
#nav ul li {
    display: inline-block;
    font-size: 0.9em;
}
#nav ul li a {
    display: block;
    color: inherit;
    text-decoration: none;
    height: 3em;
    line-height: 3em;
    padding: 0 0.5em 0 0.5em;
    outline: 0;
}

用于悬停时的下划线。

试试这个:

#nav ul li a:hover {
    border-bottom: 3px solid #eee;
}

JSFiddle Demo

关于html - 将标题定位在中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24841873/

相关文章:

html - Chrome : Scrollbar is showing on Windows but NOT Mac

javascript - HTML 输入元素 (field.validate) 始终未定义

css - svg 轮廓和填充

php - php 提供下载后刷新

javascript - 使用双缓冲改进 Html5 Canvas ?

html - 标题下方的位置部分元素

javascript - 每行两个div的AngularJS ng-repeat网格

c - 删除 TCP、IP 和以太网 header 后,从数据包中获取 HTTP header

c++ - 由于嵌套不能#include,不能使用类原型(prototype)

javascript - 如何检测是否有更多数据异步获取到 DOM 中?