如何在菜单栏中对齐文本?

时间:2015-04-01 02:10:56

标签: jquery html css

我一直试图让文字对齐中心,但我无法让它发挥作用。在每个菜单项之后,文本也会在其框内向右爬行。我开始感到恼火,并在整个地方扔了一堆text-align: center;,但我不知道还有什么可以做的。

我有一个栏,用于强调您悬停的菜单以及活动项目的不同颜色条。

screenshot

HTML:

<div class="menu">
    <div id="cssmenu">
        <ul>
            <li><a href="index.html"><span>Home</span></a></li>
            <li><a href="about.html"><span>About</span></a></li>
            <li class="active"><a href="news.html"><span>News</span></a></li>
            <li><a href="gallery.html"><span>Gallery</span></a></li>
            <li class="last"><a href="contact.html"><span>Contact</span></a></li>
        </ul>
    </div>
</div>  

CSS:

/*-- Menu Logo --*/
.logo {
    float: left;
    /*background: #DE5491;*/
    padding: 23px 20px;
    width: 406px;
    height: 80px;
    vertical-align: central;
}

.menu {
    float: right;
    width: 53%;
}

/*--menu--*/
#cssmenu li,
#cssmenu span,
#cssmenu a {
    /*margin: 0;*/
    padding: 0;
    position: relative;
    text-transform: uppercase;
    text-align: center;
}

#cssmenu {
    padding-top: 40px;
}

#cssmenu:after,
#cssmenu ul:after {
    content: '';
    display: block;
    clear: both;
}

#cssmenu a {
    color: #000;
    display: inline-block;
    line-height: 49px;
    padding: 0 51px;
    text-decoration: none;
    font-weight: normal;
    font-size: 1.5em;
    font-family: 'bebas_neueregular';
    text-align: center;
}

#cssmenu ul {
    /*list-style: none;*/
}

#cssmenu > ul {
    /*float: left;*/
    text-align: center;
}

#cssmenu > ul > li {
    float: left;
    /*display:block;*/
    text-align: center;
    /*width: 24.9999%;*/
    width: 19.9999%;
}

#cssmenu > ul > li > a {
    color: #000;
}

#cssmenu > ul > li:hover:after {
    content: '';
    display: block;
    padding-top: 46px;
    border-bottom: 10px solid #003; /*-- Menu Bar Hover Color --*/
    -webkit-transition: .2s all linear;
    -moz-transition: .2s all linear;
    -o-transition: .2s all linear;
    transition: .2s all linear;
}

#cssmenu > ul > li.active:after {
    content: '';
    display: block;
    padding-top: 46px;
    border-bottom: 10px solid #252425; /*-- Menu Bar Color --*/
    -webkit-transition: .2s all linear;
    -moz-transition: .2s all linear;
    -o-transition: .2s all linear;
    transition: .2s all linear;
}

3 个答案:

答案 0 :(得分:1)

这是因为这条规则的发生:

#cssmenu a {
  color: #000;
  display: inline-block;
  line-height: 49px;
  text-decoration: none;
  font-weight: normal;
  font-size:1.5em;
  font-family: 'bebas_neueregular';
  text-align:center;
}

需要移除padding: 0 51px;,因为它会导致您的锚点太宽而无法容纳它。

答案 1 :(得分:0)

&#13;
&#13;
/*-- Menu Logo --*/

.logo {
  float: left;
  /*background: #DE5491;*/
  padding: 23px 20px;
  width: 406px;
  height: 80px;
  vertical-align: central;
}
.menu {
  float: right;
  width: 53%;
}
/*--menu--*/

#cssmenu li,
#cssmenu span,
#cssmenu a {
  /*margin: 0;*/
  padding: 0;
  position: relative;
  text-transform: uppercase;
  text-align: center;
}
#cssmenu {
  padding-top: 40px;
}
#cssmenu:after,
#cssmenu ul:after {
  content: '';
  display: block;
  clear: both;
}
#cssmenu a {
  color: #000;
  display: inline-block;
  line-height: 49px;
  /*padding: 0 51px; */
  text-decoration: none;
  font-weight: normal;
  font-size: 1.5em;
  font-family: 'bebas_neueregular';
  text-align: center;
}
#cssmenu ul {
  list-style: none;
}
#cssmenu > ul {
  /*float: left;*/
  text-align: center;
}
#cssmenu > ul > li {
  float: left;
  /*display:block;*/
  text-align: center;
  /*width: 24.9999%;*/
  width: 19.9999%;
}
#cssmenu > ul > li > a {
  color: #000;
}
#cssmenu > ul > li:hover:after {
  content: '';
  display: block;
  padding-top: 46px;
  border-bottom: 10px solid #003;
  /*-- Menu Bar Hover Color --*/
  -webkit-transition: .2s all linear;
  -moz-transition: .2s all linear;
  -o-transition: .2s all linear;
  transition: .2s all linear;
}
#cssmenu > ul > li.active:after {
  content: '';
  display: block;
  padding-top: 46px;
  border-bottom: 10px solid #252425;
  /*-- Menu Bar Color --*/
  -webkit-transition: .2s all linear;
  -moz-transition: .2s all linear;
  -o-transition: .2s all linear;
  transition: .2s all linear;
}
&#13;
<div class="menu">
  <div id="cssmenu">
    <ul>
      <li><a href="index.html"><span>Home</span></a>
      </li>
      <li><a href="about.html"><span>About</span></a>
      </li>
      <li class="active"><a href="news.html"><span>News</span></a>
      </li>
      <li><a href="gallery.html"><span>Gallery</span></a>
      </li>
      <li class="last"><a href="contact.html"><span>Contact</span></a>
      </li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个......取消#css&gt;的宽度ul&gt; li,为#cssmenu ul和margin添加固定宽度:0px auto到#cssmenu li,#cssmenu span,#cssmenu a

 /*--menu--*/
    #cssmenu li,
    #cssmenu span,
    #cssmenu a {
    margin: 0px auto;
      padding: 0;
      position: relative;
      text-transform: uppercase;
      text-align:center;
      list-style:none;
    }

    #cssmenu ul {
      width: 960px;
    }