html - `<nav>` 和 `<center>` 中的下拉菜单未居中

标签 html css drop-down-menu navigation

感谢这里的优秀人员,我已经解决了下拉菜单的一个问题。现在我有另一个小问题。我的导航栏中的下拉菜单没有居中。它应该直接在“Works”下,但它被移到了左边。

Image

这是我的代码:

nav {
  list-style-type: none;
  margin: 0;
  padding: 0px;
  overflow: hidden;
  margin-bottom: 30px;
  line-height: 1.5em;
  text-decoration: none;
}

nav a,
.dropbtn {
  display: inline;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 25px;
  font-family: "mrs-eaves";
}

.dropbtn {
  background: none;
  border: none;
}

.dropdown {
  overflow: hidden;
}

.dropdown {
  cursor: pointer;
  font-size: 25px;
  border: none;
  outline: none;
  color: black;
  padding: 14px 16px;
  background-color: inherit;
  font-family: "mrs-eaves";
  margin: 0;
  text-decoration: none;
}

nav a:hover,
.dropbtn:hover,
.dropdown:hover,
.dropdown-content a:hover,
.dropbtn:focus {
  background-color: rgb(247, 219, 255);
  cursor: pointer;
}

.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: center;
  position: relative;
}

.show {
  display: block;
}
<nav>
  <center>
    <a href="index.html">Home</a>
    <button class="dropbtn" onclick="myFunction()">Works
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content" id="myDropdown">
      <a href="drawing.html">Drawing</a>
      <a href="animation.html">Animation</a>
      <a href="design.html">Design</a>
    </div>
    <a href="about.html">About</a>
    <a href="contact.html">Contact</a>
    <a href="links.html">Links</a>
  </center>
</nav>

最佳答案

这是我的解决方案,我用 span 标签包装了 buttondropdown-content 类名为 holder,并使其 position: relative。这将确保 dropdown-content 将定位到其 span 父级。

我还删除了 nav 标签上的 overflow:hidden 属性,因为下拉菜单不可见

我已经添加了

 left: -26px;
 top: 30px;

为了将菜单定位在按钮的正下方

nav {
  list-style-type: none;
  margin: 0;
  padding: 0px;
  /*overflow: hidden;*/
  margin-bottom: 30px;
  line-height: 1.5em;
  text-decoration: none;
}

nav a,
.dropbtn {
  display: inline;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 25px;
  font-family: "mrs-eaves";
}

.dropbtn {
  background: none;
  border: none;
}

.dropdown {
  overflow: hidden;
}

.dropdown {
  cursor: pointer;
  font-size: 25px;
  border: none;
  outline: none;
  color: black;
  padding: 14px 16px;
  background-color: inherit;
  font-family: "mrs-eaves";
  margin: 0;
  text-decoration: none;
}

nav a:hover,
.dropbtn:hover,
.dropdown:hover,
.dropdown-content a:hover,
.dropbtn:focus {
  background-color: rgb(247, 219, 255);
  cursor: pointer;
}

.dropdown-content {
  display: block; /*Added for testing purpose*/
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  
   left: -26px;
   top: 30px;
}

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

.show {
  display: block;
}

.holder{
position:relative;
}

.center{text-align:center;}
<nav>
  <div class="center">
    <a href="index.html">Home</a>
    <span class="holder">
    <button class="dropbtn" onclick="myFunction()">Works
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content" id="myDropdown">
      <a href="drawing.html">Drawing</a>
      <a href="animation.html">Animation</a>
      <a href="design.html">Design</a>
    </div>
    </span>
    <a href="about.html">About</a>
    <a href="contact.html">Contact</a>
    <a href="links.html">Links</a>
  </div>
</nav>

关于html - `<nav>` 和 `<center>` 中的下拉菜单未居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51902223/

相关文章:

html - 通过 Sandbox Manifest 2 的 Chrome 打包应用程序本地根 html 链接

jquery - 如何构建像 Evernote 这样的界面。一页,列

html - 订阅者电子邮件 : GMail is converting height to min-height

css - 在视频缩略图上制作一个CSS播放按钮

javascript - 在 bootstrap-select 上显示/隐藏选项?

php - 从数据库中检索数据到下拉菜单,但它没有显示在那里

html - 将html转换为pdf时出现wkhtmltopdf分页问题

javascript - 如何使用 Phaser 加载图像

css - Primefaces 5 旋转轴刻度标签

javascript - jQuery/javascript - 根据下拉选择更改图像