将Div中的水平UL导航栏居中

时间:2014-12-10 22:16:23

标签: html css

我知道之前已经问过这个问题,但经过几个小时的搜索后,我仍然无法将导航栏放到中心位置。我还是新手,但我认为这与我制作导航栏的方式有关,因为我在其他项目中遇到同样的问题。显示的代码是顶部栏,但我也有下栏的问题。 here是网站。

以下是相关代码。



    #topbar {
    width: 100%;
    height: 140px;
    background-color: #6d6e70;
    text-align: center;
    }

    ul.nav1 {
    list-style-type: none;
    position: absolute;
    width: 760px;
    border: 3px solid red;
    /*FIX CENTER IM LOSING MY MIND*/
    top: 35px;
    }

    li.hnav {
    width: 150px;
    height: 70px;
    float: left;
    display: inline;
    }

    a.hnav {
    padding-top: 25px;
    display: block;
    text-decoration: none;
    width: 150px;
    height: 45px;
    text-align: center;
    border-radius: 10px;
    transition: all .5s ease;
    font-family: Walkway;
    font-size: 20px;
    font-weight: bold;
    color: #fff;
    }

    a:hover {
    background-color: #fff;
    color: #6d6e70;
    }

    <div id="topbar">
    		<img src="AandAlogoFINAL1.png" alt="A and A Logo" id="logo">
    		
    		<ul class="nav1">
    			<li class="hnav"><a href="index.html" class="hnav">Home</a></li>
    			<li class="hnav"><a href="about.html" class="hnav">About</a></li>
    			<li class="hnav"><a href="framing.html" class="hnav">Framing</a></li>
    			<li class="hnav"><a href="gallery.html" class="hnav">Gallery</a></li>
    			<li class="hnav"><a href="community.html" class="hnav">Community</a></li>
    		</ul>
    		
    	</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

相对定位ul.nav1,给它margin: 0 autoheight: 77px

enter image description here

&#13;
&#13;
#topbar {
  width: 100%;
  height: 140px;
  background-color: #6d6e70;
  text-align: center;
}
ul.nav1 {
  list-style-type: none;
  position: relative;
  width: 760px;
  border: 3px solid red;
  margin: 0 auto;
  height: 77px;
}
li.hnav {
  width: 150px;
  height: 70px;
  float: left;
  display: inline;
}
a.hnav {
  padding-top: 25px;
  display: block;
  text-decoration: none;
  width: 150px;
  height: 45px;
  text-align: center;
  border-radius: 10px;
  transition: all .5s ease;
  font-family: Walkway;
  font-size: 20px;
  font-weight: bold;
  color: #fff;
}
a:hover {
  background-color: #fff;
  color: #6d6e70;
}
&#13;
<div id="topbar">
  <img src="AandAlogoFINAL1.png" alt="A and A Logo" id="logo">
  <ul class="nav1">
    <li class="hnav"><a href="index.html" class="hnav">Home</a>
    </li>
    <li class="hnav"><a href="about.html" class="hnav">About</a>
    </li>
    <li class="hnav"><a href="framing.html" class="hnav">Framing</a>
    </li>
    <li class="hnav"><a href="gallery.html" class="hnav">Gallery</a>
    </li>
    <li class="hnav"><a href="community.html" class="hnav">Community</a>
    </li>
  </ul>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个,快捷方便:

ul.nav1 {
    list-style-type: none;
    border: 3px solid red;
    width: 760px;
    display: inline-block;
    margin: 35px auto;
}

我访问了您的网站并确认其在您的网页上有效。

enter image description here

答案 2 :(得分:0)

删除位置:ul绝对位置,只需添加:

.nav1{ 
    display: inline-block;
}

然后在你的ul上添加margin-top。