哪个CSS属性将下拉菜单保留在其父li下?

时间:2019-03-17 05:06:49

标签: html css css-position

我创建了一个导航栏。它包含li元素,并且某些li元素包含下拉菜单。将鼠标悬停在特定的li元素上后,下拉菜单将变为可见。问题在于下拉菜单没有出现在其父li元素下。取而代之的是,它们全部出现在远离父元素的相同位置(在徽标下方)。我想念什么?

#navigation {
  height: 50px;
  width: 95%;
  margin: auto;
  box-shadow: 0px 0 1px 1px #ddd;
  padding: 10px;
  background-color: #fff;
}

#navigation #nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

#navigation #nav li.nav-block,
#navigation #nav li#logo-container {
  display: inline;
  margin-left: 10px;
  padding: 20px 0px;
  width: 50px;
  text-align: center;
  height: 40px;
  line-height: 50px;
  font-family: sans-serif;
  margin: 2px 0px;
}

#navigation #nav li a.nav-button,
#navigation #nav li a#logo {
  text-decoration: none;
  color: #333;
  padding: 15px 40px;
}

#navigation #nav li.nav-block:hover {
  background-color: skyblue;
}

#navigation #nav li.nav-block:hover a.nav-button {
  color: #fff;
}

a.category {
  cursor: default;
}

#logo-container {
  margin-right: 10px;
}

#logo {
  font-family: sans-serif;
  font-weight: bold;
  color: #333;
}

#logo-span {
  color: skyblue;
}

.nav-dropdown-menu {
  display: none;
  z-index: 11;
  position: absolute;
  width: 200px;
  min-height: 50px;
}

.nav-dropdown-menu>ul {
  display: block;
}

.nav-dropdown-menu>ul>.dropdown-li {
  display: block !important;
  background-color: #fff;
  border: 1px solid #fff;
  box-shadow: 0 0 1px 1px #ddd;
  width: 100%;
}

#navigation #nav li.nav-block:hover>.nav-dropdown-menu {
  display: block;
}

.nav-dropdown-menu>ul>.dropdown-li a {
  text-decoration: none;
  padding: 10px 50px;
  color: #333;
}

.nav-dropdown-menu>ul>.dropdown-li:hover {
  background-color: #333;
}

.nav-dropdown-menu>ul>.dropdown-li:hover a {
  color: #fff;
}
<div id="navigation">
  <ul id="nav">
    <li id="logo-container"><a id="logo">My&nbsp;<span id="logo-span">Logo</span></a></li>
    <li class='nav-block'><a class='nav-button category'>Handy</a>
      <div class='nav-dropdown-menu'>
        <ul>
          <li class='dropdown-li'><a href='home.php?brand_id=1&cat_id=2'>Dell</a></li>
          <li class='dropdown-li'><a href='home.php?brand_id=4&cat_id=2'>Samsung</a></li>
        </ul>
      </div>
      <li class='nav-block'><a class='nav-button category'>Tablet</a>
        <div class='nav-dropdown-menu'>
          <ul>
            <li class='dropdown-li'><a href='home.php?brand_id=2&cat_id=9'>Sony</a></li>
          </ul>
        </div>
        <li class="nav-block"><a class="nav-button" href="registration.php">Sign Up</a></li>

        <li class="nav-block"><a class="nav-button" href="main_login.php">Login</a></li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:4)

看看这个。将position;relative赋予您的li,并设置left中的rightnav-dropdown-menu

#navigation {
  height: 50px;
  width: 95%;
  margin: auto;
  box-shadow: 0px 0 1px 1px #ddd;
  padding: 10px;
  background-color: #fff;
}

#navigation #nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

#navigation #nav li.nav-block,
#navigation #nav li#logo-container {
  display: inline;
  margin-left: 10px;
  padding: 20px 0px;
  width: 50px;
  text-align: center;
  height: 40px;
  line-height: 50px;
  font-family: sans-serif;
  margin: 2px 0px;
}

#navigation #nav li a.nav-button,
#navigation #nav li a#logo {
  text-decoration: none;
  color: #333;
  padding: 15px 40px;
}

#navigation #nav li.nav-block:hover {
  background-color: skyblue;
}

#navigation #nav li.nav-block:hover a.nav-button {
  color: #fff;
}

a.category {
  cursor: default;
}

#logo-container {
  margin-right: 10px;
}

#logo {
  font-family: sans-serif;
  font-weight: bold;
  color: #333;
}

#logo-span {
  color: skyblue;
}

.nav-block {
  position: relative;
}

.nav-dropdown-menu {
  display: none;
  z-index: 11;
  position: absolute;
  left: -39px;
  right: 0;
  width: 200px;
  min-height: 50px;
}

.nav-dropdown-menu>ul {
  display: block;
}

.nav-dropdown-menu>ul>.dropdown-li {
  display: block !important;
  background-color: #fff;
  border: 1px solid #fff;
  box-shadow: 0 0 1px 1px #ddd;
  width: 100%;
}

#navigation #nav li.nav-block:hover>.nav-dropdown-menu {
  display: block;
}

.nav-dropdown-menu>ul>.dropdown-li a {
  text-decoration: none;
  padding: 10px 50px;
  color: #333;
}

.nav-dropdown-menu>ul>.dropdown-li:hover {
  background-color: #333;
}

.nav-dropdown-menu>ul>.dropdown-li:hover a {
  color: #fff;
}
<div id="navigation">
  <ul id="nav">
    <li id="logo-container"><a id="logo">My&nbsp;<span id="logo-span">Logo</span></a></li>
    <li class='nav-block'><a class='nav-button category'>Handy</a>
      <div class='nav-dropdown-menu'>
        <ul>
          <li class='dropdown-li'><a href='home.php?brand_id=1&cat_id=2'>Dell</a></li>
          <li class='dropdown-li'><a href='home.php?brand_id=4&cat_id=2'>Samsung</a></li>
        </ul>
      </div>
      <li class='nav-block'><a class='nav-button category'>Tablet</a>
        <div class='nav-dropdown-menu'>
          <ul>
            <li class='dropdown-li'><a href='home.php?brand_id=2&cat_id=9'>Sony</a></li>
          </ul>
        </div>
        <li class="nav-block"><a class="nav-button" href="registration.php">Sign Up</a></li>

        <li class="nav-block"><a class="nav-button" href="main_login.php">Login</a></li>
  </ul>
</div>