Sub Nav不会水平显示在下面

时间:2016-05-26 20:41:36

标签: html css

我正在创建一个带有更多子列表的导航栏。第一个子列表显示我想要的方式,但下面的那个没有。它不是在初始子导航栏下方水平显示,而是垂直显示在其右侧。

有没有人知道为什么会这样,我相信我错过了一些东西但是因为我的生活无法找到它。

我添加了一张照片来解释我想要的内容。红色应出现在蓝色的位置。

enter image description here

感谢您的时间!

<nav>
  <ul>
    <li><a href="/p/">Players</a>
      <ul>
        <li><a href="#">Wonderkids</a>
        </li>
        <li><a href="#">Cheap Players</a>
        </li>
        <li><a href="#">Player Comparisons</a>
        </li>
      </ul>
    </li>
    <li><a href="/c/">Clubs</a>
      <ul>
        <li><a href="#">Club Info</a>
        </li>
        <li><a href="#">Transfer Budgets</a>
        </li>
        <li><a href="#">Sugar Daddys</a>
        </li>
      </ul>
    </li>
    <li><a href="/s/">Downloads</a>
      <ul>
        <li><a href="#">Tactics</a>
        </li>
        <li><a href="#">Shortlists</a>
          <ul>
            <li><a href="#">Various Shortlists</a>
            </li>
            <li><a href="#">Positional Shortlists</a>
            </li>
            <li><a href="#">Staff Shortlists</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="/g/">Write-Ups</a>
    </li>
    <li><a href="/p/">Social</a>
      <ul class="ul-right">
        <li><a href="#">Facebook</a>
        </li>
        <li><a href="#">Twitter</a>
        </li>
        <li><a href="#">Youtube</a>
        </li>
        <li><a href="#">Affiliates</a>
        </li>
      </ul>
    </li>
    <li><a href="/aboutus/">About Us</a>
    </li>
  </ul>
</nav>
{{1}}

1 个答案:

答案 0 :(得分:0)

如果设置ul的宽度没有问题,这可能是一个解决方案:

nav {
  margin: 0 auto;
  text-align: center;
}
nav ul ul {
  display: none;
}
nav ul li:hover > ul {
  display: block;
}
nav ul {
  color: white;
  background: #787878;
  background: linear-gradient(top, #787878 0%, #272727 100%);
  background: -moz-linear-gradient(top, #787878 0%, #272727 100%);
  background: -webkit-linear-gradient(top, #787878 0%, #272727 100%);
  box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
  padding: 0 20px;
  border-radius: 25px;
  list-style: none;
  position: relative;
  display: inline-table;
}
nav ul:after {
  content: "";
  clear: both;
  display: block;
}
nav ul li {
  float: left;
}
nav ul li:hover {
  background: #A8A8A8;
}
nav ul li:hover a {
  color: #fff;
}
nav ul li a {
  display: block;
  padding: 15px 50px;
  color: white;
  text-decoration: none;
}
nav ul ul {
  background: #505050;
  border-radius: 0px;
  padding: 0;
  position: absolute;
  top: 100%;
}
nav ul ul li {
  float: left;
  border-top: 1px solid #6b727c;
  border-bottom: 1px solid #575f6a;
  position: relative;
}
nav ul ul li a {
  padding: 8px 40px;
  color: #fff;
}
nav ul ul li a:hover {
  background: #A8A8A8;
}
nav ul ul ul {
    position: absolute;
top: 100%;
width: 578px;
}
nav ul ul.ul-right {
  right: 0;
}
<nav>
  <ul>
    <li><a href="/p/">Players</a>
      <ul>
        <li><a href="#">Wonderkids</a>
        </li>
        <li><a href="#">Cheap Players</a>
        </li>
        <li><a href="#">Player Comparisons</a>
        </li>
      </ul>
    </li>
    <li><a href="/c/">Clubs</a>
      <ul>
        <li><a href="#">Club Info</a>
        </li>
        <li><a href="#">Transfer Budgets</a>
        </li>
        <li><a href="#">Sugar Daddys</a>
        </li>
      </ul>
    </li>
    <li><a href="/s/">Downloads</a>
      <ul>
        <li><a href="#">Tactics</a>
        </li>
        <li><a href="#">Shortlists</a>
          <ul>
            <li><a href="#">Various Shortlists</a>
            </li>
            <li><a href="#">Positional Shortlists</a>
            </li>
            <li><a href="#">Staff Shortlists</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="/g/">Write-Ups</a>
    </li>
    <li><a href="/p/">Social</a>
      <ul class="ul-right">
        <li><a href="#">Facebook</a>
        </li>
        <li><a href="#">Twitter</a>
        </li>
        <li><a href="#">Youtube</a>
        </li>
        <li><a href="#">Affiliates</a>
        </li>
      </ul>
    </li>
    <li><a href="/aboutus/">About Us</a>
    </li>
  </ul>
</nav>