CSS - 菜单栏在悬停时更改宽度

时间:2013-06-09 14:39:03

标签: css

我创建了一个菜单和一个子菜单,应该在我将鼠标悬停在主菜单的列表元素上后显示。问题是主菜单列表元素的宽度在悬停时会发生变化,看起来令人费解。我尝试使用固定宽度,但菜单项之间的空间太大了。

我还尝试将子菜单设置为绝对位置,但问题是子菜单始终位于同一位置,而不是位于主菜单项的活动状态下。

I created a codepen for this issue

HTML:

<ul class="menubar">
  <li class="menubar-li"><a href="#">Unternehmen</a>
    <ul class="menubar-sub">
      <li class="menubar-sub-li"><a href="../html/profil.html">Profil</a>
      <li class="menubar-sub-li"><a href="../html/meilensteine.html">Meilensteine</a>
      <li class="menubar-sub-li"><a href="../html/team.html">Team</a>
      <li class="menubar-sub-li"><a href="../html/news.html">News</a>
      <li class="menubar-sub-li"><a href="../html/jobs.html">Jobs</a></li>
    </ul>
  </li>

  <li class="menubar-li"><a href="#">Kompetenzen</a>
    <ul class="menubar-sub">
  <li class="menubar-sub-li"><a href="../html/kreation.html">Kreation</a>
    <ul class="menubar-subsub">
      <li><a href="../html/strategie.html">Strategie</a>
      <li><a href="../html/design.html">Design</a>
      <li><a href="../html/online.html">Online</a>
      <li><a href="../html/reinzeichnung.html">Reinzeichnung</a></li>
    </ul>
  </li>

  <li class="menubar-sub-li"> <a href="../html/prepress.html">Prepress</a>
    <ul  class="menubar-subsub">
     <li><a href="../html/seitenproduktion.html">Seitenproduktion</a></li>
     <li><a href="../html/kreativretusche.html">Kreativretusche</a>
     <li><a href="../html/colormanagement.html">Colormanagement</a>
     <li><a href="../html/proofen.html">Proofen</a></li>
   </ul>
  </li>
  <li class="menubar-sub-li"><a href="../html/druck.html">Druck</a>
    <ul  class="menubar-subsub">
      <li><a href="../html/personalisiert.html">Personalisiert</a></li>
      <li><a href="../html/web2print.html">Web 2 Print</a></li>
    </ul>
  </li>

  <li class="menubar-sub-li"><a href="../html/katalogmanagement.html">Katalogmanagement</a></ul>
  </li>

  <li class="menubar-li"><a href="../html/portfolio.html">Portfolio</a></li>
    <li class="menubar-li"><a href="#">Service</a>
      <ul class="menubar-sub">
        <li class="menubar-sub-li"><a href="../html/mediapool.html">Mediapool</a></li>
        <li class="menubar-sub-li"><a href="../html/duon.html">DUON</a></li>
        <li class="menubar-sub-li"><a href="../html/datenupload.html">Datenupload</a></li>
        <li class="menubar-sub-li"><a href="../html/downloads.html">Downloads</a>
      </ul>
  </li>
  <li class="menubar-li"><a href="#">Kontakt</a>
     <ul class="menubar-sub">
       <li class="menubar-sub-li"><a href="../html/ansprechpartner.html">Ansprechpartner</a></li>
       <li class="menubar-sub-li"><a href="../html/anfahrt.html">Anfahrt</a></li>
       <li class="menubar-sub-li"><a href="../html/impressum.html">Impressum</a></li>
     </ul>
  </li>
</ul>

CSS:

.menubar {
    list-style: none;
    position: relative;
    bottom: 16px;
}

.menubar a {
    color: black;
    text-decoration:none;
    font-size: 13px;
    position: relative;
}

.menubar-li {
    float: left;
    height:29px;
    line-height:29px;
    vertical-align: middle;
    padding-left: 10px;
    padding-right: 10px;
    margin-right: 10px;
}

.menubar-li:hover {
    background-color: #94ba1d;
    cursor: pointer;
}

.menubar-li:hover .menubar-sub {
    display: block;
}

.menubar-sub {
    display: none;
    list-style: none;
    margin-top: 1px;
    padding-bottom: 10px;
    padding-right: 10px;
    padding-left: 0px;
    background-color: #94ba1d;
    position: relative;
    left: -10px;
}

.menubar-sub-li {
    line-height: 14px;
    padding-top: 5px;
    padding-left: 10px;
}

.menubar-subsub {
    display: none;
}

1 个答案:

答案 0 :(得分:1)

您可以通过将主菜单按钮设置为position: relative并将(绝对)子菜单放在其中来将子菜单放在主菜单按钮下方。然后,您可以通过切换overflow: hiddenvisible来显示/隐藏子菜单。

以下是演示:http://codepen.io/seraphzz/pen/osGnh