顶部导航项目不居中

时间:2014-05-21 13:28:10

标签: html css

我正试图将我的链接置于中间位置,但因为我在每个角落都有社交图标,所以它似乎不在中间。我得出了这个结论,因为箭头具有相同的左或右位置。我怎样才能做到这一点。行动中的代码可以在这里看到:

http://95.85.11.40/topnav/

HTML:

  <header>
    <nav>
      <div class="container">

        <img class="logoimage" src="css/img/messages.png">
    <img class="arrowleft" src="css/img/arrow-left.png">
    <ul>
      <li class="yellow"><a href="index.html">Firefox</a></li>
      <li class="green"><a href="index.html">Chrome</a></li>
      <li class="active"><a href="index.html">Safari</a></li>
      <li class="red"><a href="index.html">Opera</a></li>
      <li class="purple"><a href="index.html">Internet Explorer</a></li>
    </ul>

    <img class="arrowright" src="css/img/arrow-right.png">
                <img class="twitterlink" src="css/img/twitter_32.png">
                <img class="facebooklink" src="css/img/facebook_32.png">





      </div>
    </nav>
  </header>

的CSS:

div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}

article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
  display: block;
}

body {
  line-height: 1;
    margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}

ol, ul {
  list-style: none;
}

blockquote, q {
  quotes: none;
}

blockquote:before, blockquote:after,
q:before, q:after {
  content: '';
  content: none;
}

.logoimage {
  height: 36px;
position: absolute;
left: 10px;
}

.facebooklink {
  height: 36px;
position: absolute;
margin-top: 4px;
right: 45px;
height: 28px;
width: 28px;
}

.twitterlink {
  height: 36px;
position: absolute;
margin-top: 4px;
right: 10px;
height: 28px;
width: 28px;
}

.arrowright {
  height: 36px;
position: absolute;
margin-top: 4px;
right: 400px;
height: 28px;
width: 28px;
}

.arrowleft {
  height: 36px;
position: absolute;
margin-top: 4px;
left: 400px;
height: 28px;
width: 28px;
}



body {
  font: 13px/18px 'Lucida Grande', Tahoma, Verdana, sans-serif;
  color: #404040;
  background: #ecedef;
}

.container {
  margin: 0 auto;
  width: 540px;
}



nav {
  height: 36px;
  background: #464b4c;
  border-top: 1px solid #353939;
  border-bottom: 1px solid #2e3131;
  background-image: -webkit-linear-gradient(top, #464b4c, #3f4344);
  background-image: -moz-linear-gradient(top, #464b4c, #3f4344);
  background-image: -o-linear-gradient(top, #464b4c, #3f4344);
  background-image: linear-gradient(to bottom, #464b4c, #3f4344);
  -webkit-box-shadow: inset 0 1px rgba(255, 255, 255, 0.06);
  box-shadow: inset 0 1px rgba(255, 255, 255, 0.06);
}
nav a {
  -webkit-transition: 0.1s ease-out;
  -moz-transition: 0.1s ease-out;
  -o-transition: 0.1s ease-out;
  transition: 0.1s ease-out;
  -webkit-transition-property: background-color, line-height;
  -moz-transition-property: background-color, line-height;
  -o-transition-property: background-color, line-height;
  transition-property: background-color, line-height;
}
nav ul {
  float: left;
  border-left: 1px solid #353939;
  border-left: 1px solid rgba(0, 0, 0, 0.2);
  border-right: 1px solid #4d5354;
  border-right: 1px solid rgba(255, 255, 255, 0.06);
}
nav li {
  float: left;
}
 nav a {
  display: block;
  padding: 0 20px;
  line-height: 36px;
  color: #ddd;
  text-decoration: none;
  text-shadow: 0 -1px #2e3131;
  border-left: 1px solid #4d5354;
  border-left: 1px solid rgba(255, 255, 255, 0.06);
  border-right: 1px solid #353939;
  border-right: 1px solid rgba(0, 0, 0, 0.2);
  cursor: pointer;
}
nav a:hover {
  background: #4d5354;
  background: rgba(255, 255, 255, 0.05);
}

2 个答案:

答案 0 :(得分:1)

从箭头中删除position: absolute并更改其样式

.arrowleft {
    float: left;
    height: 28px;
    margin-top: 4px;
    width: 28px;
}

...

.arrowright {
    float: right;
    height: 28px;
    margin-top: 4px;
    width: 28px;
}

最后从float:left

中移除ul

最终结果(在Firefox 29上)

enter image description here

答案 1 :(得分:1)

或者,您可以像这样构建菜单:

<div id="wrapper">
<img class="arrowleft" src="css/img/arrow-left.png">
<ul>
  <li class="yellow"><a href="index.html">Firefox</a></li>
  <li class="green"><a href="index.html">Chrome</a></li>
  <li class="active"><a href="index.html">Safari</a></li>
  <li class="red"><a href="index.html">Opera</a></li>
  <li class="purple"><a href="index.html">Internet Explorer</a></li>
</ul>
<img class="arrowright" src="css/img/arrow-right.png">
</div>

并应用此css:

#wrapper {
    text-align:center;
    font-size:0px;
}
.arrowleft, .arrowright, ul, li {
    display:inline-block;
    zoom:1;
    *display:inline;
}

请务必从floats中的元素中移除#wrapper,以使其正确居中。另请记住在新的li声明中指定正确的字体大小。

zoom:*display:inline是IE的修复,因为它们解决了内联块没有应用于没有内联和hasLayout属性的元素的错误。 font-size:0修复了元素之间应用的空间,因为它们是新行上的内联项。