简体   繁体   English

用CSS垂直对齐导航栏

[英]Verticaly aligning nav bar with css

I am making a website for fun and i put in a navigation bar. 我正在制作一个好玩的网站,并放入了导航栏。 The only problem is that the navigation bar is not vertically centred and is being pushed down a row by the links leaving an unwanted white space between the logo and nav bar. 唯一的问题是导航栏没有垂直居中,并且被链接向下推了一行,在徽标和导航栏之间留下了多余的空白。 The links are also too high causing them (when hovering over) to not remain within the navigation area. 链接也太高,导致它们(悬停时)无法保留在导航区域内。 Is there any fix for this? 有没有解决办法? Code below: 代码如下:

Html: HTML:

<nav id="nav">
    <ul>
        <li><a href="http://www.google.com">hello</a></li>
        <li><a href="http://www.google.com">hello</a></li>
    </ul>       
</nav>

Css: CSS:

.container{
 width:1020px;
 margin:auto;
 position:relative;
 }


    #nav{
width:1020px;
height:50px;
background-color:#EBB035;
}
#nav ul{
display:block;
list-style-type:none;
}
#nav li{
display:inline;
}
#nav a:link{
color: #000000;
font-weight: 700;
padding: 8px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
}
#nav a:visited{
color: #000000;
font-weight: 700;
padding: 8px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
}
#nav a:hover{
background-color:#FFCC00;
color: #000000;
font-weight: 700;
padding: 8px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
}

Thank you in advance! 先感谢您!

Add positioning to #nav and #nav ul as follows; 将定位添加到#nav#nav ul ,如下所示; and you'll have result like this 你就会有结果, 这样的

#nav {
    width:1020px;
    height:50px;
    background-color:#EBB035;
    position: absolute;
}
#nav ul {
    display:block;
    position: relative;
    list-style-type:none;
    margin-top: 1em;
}

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

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