下拉菜单的位置很奇怪

时间:2019-02-27 13:23:28

标签: html css drop-down-menu position

这是我的工具栏的代码:

.toolbar {
  list-style-type: none;
  height: 90px;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #252525;
  box-shadow: 1px 1px 5px #0d0d0d;
}

.centered_toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
}

.toolbar ul {
  list-style-type: none;
  list-style: none;
}

.toolbar li {
  float: left;
}

.toolbar a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-family: 'Poppins', sans-serif;
}

.toolbar a:hover {
  color: #cccccc;
}

.dropdown a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="toolbar">
  <div class="centered_toolbar">
    <img src="./logo.png" width="200px" height="auto" style="position: absolute; left: 0px; margin-left: 30px;">
    <ul>
      <li class="active"><a a href='#' onclick="goToMainPage()"><i class="dashboard"></i>Dashboard</a></li>
      <li><a a href='#' onclick="goToNoteslistPage()"><i class="notenliste"></i>Notenliste</a></li>
    </ul>
    <a href='#' onclick="myFunction()" style="position: absolute; right: 0px; margin-right: 120px;">

      <div class="dropdown">
        <button class="dropbtn">Dropdown
                          <i class="fa fa-caret-down"></i>
                        </button>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </div>

    </a>
    <a href='#' onclick="logout()" style="position: absolute; right: 0px; margin-right: 20px; font-family: 'Poppins', sans-serif;"><i
                            class="logout"></i>Logout</a>
  </div>
</div>

最好通过以下图片描述问题:https://www.dropbox.com/s/wt47iz5r5fhjrss/bug.PNG?dl=0
下拉菜单不在下拉按钮下方。不仅如此。使用该代码,下拉按钮本身将低于工具栏中的其他元素。可以请帮助我的任何人。

4 个答案:

答案 0 :(得分:1)

这是您要找的东西吗?

您的HTML布局有点混乱,您需要将下拉列表与其他项一起添加到ul li列表中。我在下面修改了您的HTML和CSS。

.toolbar {
  list-style-type: none;
  height: 90px;
  margin: 0;
  padding: 0;
  background-color: #252525;
  box-shadow: 1px 1px 5px #0d0d0d;
}

.centered_toolbar {
  display: flex;
  align-items: center;
  height: 100%;
  margin: 0 30px;
  justify-content: space-between;
}

.toolbar ul {
  list-style-type: none;
  list-style: none;
}

.toolbar li {
  float: left;
}

.toolbar a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-family: 'Poppins', sans-serif;
}

.toolbar a:hover {
  color: #cccccc;
}

.dropdown a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown:hover {
  background-color: red;
}

.dropdown-content {
  display: none;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  flex-direction: column;
  padding: 0;
  position: absolute;
  top: 75px;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: flex;
}
<div class="toolbar">
  <div class="centered_toolbar">
    <img src="./logo.png" width="200px" height="auto">
    <ul>
      <li class="active"><a href='#' onclick="goToMainPage()"><i class="dashboard"></i>Dashboard</a></li>
      <li><a href='#' onclick="goToNoteslistPage()"><i class="notenliste"></i>Notenliste</a></li>
      <li class="dropdown"><a href='#' onclick="myFunction()">Dropdown<i class="fa fa-caret-down"></i></a>
        <ul class="dropdown-content">
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
          <li><a href="#">Link 3</a></li>
        </ul>
      </li>
    </ul>
    <a href='#' onclick="logout()"><i class="logout"></i>Logout</a>
  </div>
</div>

经过进一步检查,我将答案修改为:

我从徽标图片和注销按钮中删除了position:absolute,并在容器上使用了justify-content:space-between。这是使菜单项均匀分布的一种更清洁的方法。

答案 1 :(得分:1)

如果这是您的要求,则可以删除position属性。位置属性可移动您的按钮,但下拉值不会根据位置属性来移动位置。

enter image description here

答案 2 :(得分:0)

这是由于设置CSS时发生错误。您可以使用引导程序模板来获取帮助。 See if this helps

请帮忙投票。

答案 3 :(得分:0)

你在这里。

除非您必须这样做,否则我将避免放置任何内联CSS,这会使代码随着项目的增长而变得难以维护。就您而言,position: absolute也没有必要。

.toolbar {
  list-style-type: none;
  height: 90px;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #252525;
  box-shadow: 1px 1px 5px #0d0d0d;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.toolbar ul {
  list-style-type: none;
  list-style: none;
}

.toolbar li {
  display: inline-block;
}

.toolbar a,
.dropbtn {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-family: 'Poppins', sans-serif;
}

.toolbar a:hover {
  color: #cccccc;
}

.dropdown a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  margin: 0;
}

.navbar a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="toolbar">
  <div class="logo">
    <img src="./logo.png" width="100px" height="auto">
  </div>
  <ul>
    <li class="active">
      <a a href='#' onclick="goToMainPage()"><i class="dashboard"></i>Dashboard</a>
    </li>
    <li><a a href='#' onclick="goToNoteslistPage()"><i class="notenliste"></i>Notenliste</a></li>
    <li>
      <div class="dropdown">
        <button class="dropbtn">Dropdown<i class="fa fa-caret-down"></i></button>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </div>
    </li>
  </ul>
  <ul>
    <li>
      <a href='#' onclick="logout()">
        <i class="logout"></i>Logout
      </a>
    </li>
  </ul>
</div>