【问题标题】:Nav UL items stretch across page导航 UL 项目横跨页面
【发布时间】:2015-11-21 05:38:19
【问题描述】:

我正在处理一个简单的静态页面,并且我的导航栏几乎是正确的。现在,菜单项正在整个导航栏的宽度上延伸,而不是在有意义的情况下保留在它们的“列表区域”中。我觉得我已经尝试了所有方法,并且我认为这与我在导航和照片轮播上的 z-index (以便导航菜单项显示在轮播顶部)和定位有关,但是我想不通。

nav ul {
  background: #A6CE4F;
  box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
  padding: 0px;
  list-style: none;
  position: relative;
  display: inline-table;
  width: 100%;
  display: table;
}
nav ul ul {
  display: none;
  width: 100%;
  background: #f37b35;
  border-radius: 0px;
  padding: 0;
  position: absolute;
}
nav ul:after {
  content: "";
  clear: both;
  display: block;
}
nav ul li {
  float: left;
}
nav ul li:hover > ul {
  display: block;
}
nav ul li:hover a {
  color: #fff;
}
nav ul li a {
  display: block;
  padding: 25px 40px;
  color: #1f354b;
}
nav ul ul li {
  float: none;
  border-top: 1px solid #fff;
  border-bottom: 1px solid #fff;
  position: relative;
}
nav ul ul li a {
  padding: 15px 40px;
  color: #fff;
}
nav ul ul li a:hover {
  background: #e2550e;
}
nav ul ul ul {
  position: absolute;
  left: 100%;
  top: 0;
}
<nav style="position: relative; z-index: 2;">
  <ul>
    <li>
      <a href="">ICAB Leadership Group</a>
      <ul>
        <li><a href="">Requirements</a>
        </li>
        <li><a href="">Roles & Responsibilities</a>
        </li>
      </ul>
    </li>
    <li>
      <a href="">Site Cabs</a>
      <ul>
        <li><a href="">Community Input</a>
        </li>
        <li><a href="">Cross Network Collaborations</a>
        </li>
      </ul>
    </li>
    <li>
      <a href="">Community Toolbox</a>
      <ul>
        <li><a href="">Community engagement templates and documents</a>
        </li>
        <li><a href="">Network and Community fact sheets</a>
        </li>
        <li><a href="">Training materials</a>
        </li>
        <li><a href="">FAQs</a>
        </li>
        <li><a href="">Community Research Resources</a>
        </li>
        <li><a href="">Acronyms</a>
        </li>
        <li><a href="">Email Alias Lists </a>
        </li>
      </ul>
    </li>
    <li>
      <a href="">Contacts & Email Lists</a>
    </li>
  </ul>
</nav>

【问题讨论】:

标签: html css navigation


【解决方案1】:

这是因为您将 width: 100% 设置为 .nav ul ul。如果您希望宽度保持可变,请将.nav ul ul 中的width: 100% 设置为width: auto

此处示例:http://codepen.io/anon/pen/MaRJXZ

【讨论】:

    【解决方案2】:

    这是因为您将下拉列表 ul 的宽度设置为 100%。

    像这样添加宽度:

    width: auto;
    

    见小提琴here

    【讨论】:

    • 谢谢!那解决了它!我认为这是我错过的超级简单的东西:)
    • 还有,为什么导航栏右侧的所有空间?
    • @LAS87,看看我更新的小提琴。现在还有空位吗?
    • @lonut 是的,还在 :(
    猜你喜欢
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多