【问题标题】:Drop down menu text not centering下拉菜单文本不居中
【发布时间】:2014-08-06 16:31:00
【问题描述】:

我正在尝试将下拉菜单的每个标题中的文本居中,同时让菜单的实际“下拉”部分中的文本向左对齐。

我已经按照我想要的方式对齐了下拉部分,但我似乎无法对齐标题。

HTML 代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
      <title>Clothing Selector</title></head>
        <link rel="stylesheet" type="text/css"href="main.css" />

  <body background="Pictures/background.png">    
      <h2 id="home_subhead">Keep Track Of Your Clothes :-)</h2>

<video id="band_shirts_banner" width="20%" autoplay loop>
  <source src="band_shirts_banner.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>


<div id="menu">
      <ul class="menu" align="center">
        <li><a>Shirts</a>
          <ul>
              <li><a href="band_shirts.html">Band Shirts</a></li>
            <li><a href="night_shirts.html">Sleeping Shirts</a></li>
            <li><a href="trash_shirts.html">Trash Shirts</a></li>
          </ul>
        </li>

        <li><a>Pants</a>
          <ul>
             <li><a href="royal_bones.html">Royal Bones</a></li>
            <li><a href="rude.html">Rude</a></li>   
          </ul>
        </li>

        <li><a>Shoes</a>
          <ul>
              <li><a href="converse.html">Converse</a></li>
            <li><a href="boots.html">Boots</a></li>   
          </ul>
        </li>

      </ul>
</div>


  </body>
</html>

CSS 代码:

ul, ul ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

.menu > li > a {
    background-color: #616975;
    background-image: -webkit-gradient(linear, left top, left bottom, from(rgb(114, 122, 134)),to(rgb(80, 88, 100)));
    background-image: -webkit-linear-gradient(top, rgb(255, 0, 0), rgb(0, 0, 0));<!---------COLOR OF BAR @ BOTTOM OF DROPDOWN LIST---------->
    background-image: -moz-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
    background-image: -o-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
    background-image: -ms-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
    background-image: linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100));
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,StartColorStr='#727a86', EndColorStr='#505864');
    border-bottom: 0.0625em solid #33373d;
    -webkit-box-shadow: inset 0px 1px 0px 0px #878e98;
    -moz-box-shadow: inset 0px 1px 0px 0px #878e98;
    box-shadow: inset 0px 1px 0px 0px #878e98;
    width: 100%;
    height: 2.75em;
    line-height: 2.75em;
    text-indent: 2.75em;
    display: block;
    position: relative;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-weight: 600;
    color: #fff;
    text-shadow: 0px 1px 0px rgba(0,0,0,.5);
}

.menu ul li a {
    background: #fff;
    border-bottom: 0.0625em solid #efeff0;
    width: 100%;
    height: 2.75em;
    line-height: 2.75em;
    text-indent: 2.75em;
    display: block;
    position: relative;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 0.923em;
    font-weight: 400;
    color: black;<!---------DROPDOWN MENU TEXT COLOR---------->

}
.menu ul li:last-child a {
    border-bottom: 0.0625em solid #33373d;<!---------COLOR OF BAR @ BOTTOM OF DROPDOWN LIST---------->

}
.menu > li > a:hover, .menu > li > a.active {
    background-color: #35afe3;
    background-image: -webkit-gradient(linear, left top, left bottom, from(rgb(69, 199, 235)),to(rgb(38, 152, 219)));
    background-image: -webkit-linear-gradient(top, rgb(255, 0, 0), rgb(255, 0, 0));<!---------COLOR OF HEADER WHEN HOVERING---------->
    background-image: -moz-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
    background-image: -o-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
    background-image: -ms-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
    background-image: linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219));
    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,StartColorStr='#45c7eb', EndColorStr='#2698db');
    border-bottom: 0.0625em solid #103c56;<!---------COLOR OF THIN BAR @ BOTTOM OF HEADER---------->
    -webkit-box-shadow: inset 0px 1px 0px 0px #6ad2ef;
    -moz-box-shadow: inset 0px 1px 0px 0px #6ad2ef;
    box-shadow: inset 0px 1px 0px 0px #6ad2ef;
}

.menu > li > ul li a:before{
    content: '▶ ';
    font-size: .5em;
    color: black;<!---------COLOR OF ARROWS ON THE SIDE OF DROPDOWN LIST---------->
}

.menu > li > ul li:hover a,
.menu > li > ul li:hover a span,
.menu > li > ul li:hover a:before {
    color: #32373D;<!---------COLOR OF TEXT IN DROPDOWN LIST---------->
}

.menu li {
    float:left;
    width:15em;
}

ul li ul{
    display: none;
    }

    ul li:hover ul{
    display: block; /* display the dropdown */
    }

看到“裤子”不是在中间吗?

感谢任何帮助

【问题讨论】:

  • jsfiddle.net/j8C23/2,这就是你想要的吗?这是text-indent.menu &gt; li &gt; a中设置为0的已删除帖子的代码

标签: css html drop-down-menu


【解决方案1】:

您只需在您想要在 CSS 或 HTML 代码中居中的部分添加text-align: center;

或者你可以使用&lt;center&gt;标签到你想要居中的每个标题。

我所做的是在 CSS 中删除了 .menu &gt; li &gt; a 设计中的 text-indent: 2.75em;

我认为这就是您的标题文本没有居中的原因。

【讨论】:

  • 我已经尝试过了,但发生了一些奇怪的事情......文本居中,但它显示为悬停在菜单上而不是菜单内部,以及将其他标签从那里敲出地点。
  • 从你的 CSS 代码中移除 text-indent: 2.75em;
  • 谢谢!这正是我所追求的!我 16 岁,对编程很陌生。既然如此,我并不完全明白这一切意味着什么。正在努力,但现在,我想我会参考这个网站 lol
猜你喜欢
  • 2013-03-22
  • 2014-03-08
  • 2011-09-26
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 2012-11-12
  • 2014-01-15
  • 2014-07-05
相关资源
最近更新 更多