HTML 下拉菜单无法正确定位

标签 html css navigation dropdown

我制作了一个下拉菜单,用于我网站导航栏中的三个不同链接。下拉菜单本身工作正常但是我遇到的问题是我无法让下拉菜单将自己定位在每个导航链接的中间,它针对其中一个链接正确定位但是另一个不是因为它们具有不同的特征长度。下拉菜单完全用 HTML 和 CSS 完成,网站的其余部分也使用 Django,但它没有在下拉菜单中使用。

问题图片-

(这是下拉列表未正确定位的链接之一,您可以看到框顶部的三 Angular 形未与链接的中间对齐。)

This is one of the links where the dropdown is not positioned correctly

(在这个中你可以看到三 Angular 形或多或少以链接为中心,但我相信这只是偶然)

This is one of the links where the dropdown is correctly positioned

下拉 HTML(其中一个下拉列表与其他下拉列表相同)

      <li class="link-drop"><a href="">Employer</a>
          <ul class="link-drop-ul">
              <div class="triangle"></div>
              <div class="link-drop-box">
                  <li><a class="link-drop-a" href="{% url 'createjoblisting' %}">Add Listing</a></li>
                  <li><a class="link-drop-a" href="{% url 'featurenotimplemented' %}">How it works</a></li>
              </div>
          </ul>
      </li>

下拉 CSS(这在所有地方都一样)

.link-drop {
    list-style-type: none;
    display: inline;
    font-size: 16px;
    cursor: pointer;
    list-style-type: none;
    display: inline-block;
    float: right;
    margin: auto;
    text-align: center;
}
.link-drop li {
    list-style-type: none;
}
.link-drop a {
    text-decoration: none;
    color: #ebebee;
    float: right;
    margin-top: 18px;
    margin-right: 9px;
    margin-left: 9px;
    list-style-type: none;
}
.link-drop-ul {
    display: none;
    position: absolute;
    text-align: center;
    padding-top: 18px;
    -webkit-transition: all .1s ease-out;
    -moz-transition: all .1s ease-out;
    -ms-transition: all .1s ease-out;
    -o-transition: all .1s ease-out;
    transition: all .1s ease-out;
}
.link-drop-box {
    padding-bottom: 1px;
    padding-top: 1px;
    background-color: white;
    box-shadow: 0px 0px 38px -10px black;
    border-radius: 5px;
    width: 122px;
    margin: auto;
}
.triangle {
    width: 0;
    height: 0;
    position: relative;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-bottom: 8px solid white;
    left: 50%;
    margin-left: -8px;
}
.link-drop-ul li {
    margin-top: 15px;
    margin-bottom: 15px;
}
.link-drop-ul li a {
    color: black;
    float: none;
}
.link-drop-ul li a:hover {
    color: #d1d1d1;
}
.link-drop:hover > .link-drop-ul {
    display: block;
    position: absolute;
    padding-top: 55px;
    padding-left: 0px;
    margin-top: 10px;
}

感谢任何帮助,谢谢!

最佳答案

这是您所面临问题的解决方案。我更改/移动/添加了一些 CSS。

.link-drop {
    position:relative;
    list-style-type: none;
    display: inline;
    font-size: 16px;
    cursor: pointer;
    list-style-type: none;
    display: inline-block;
    float: right;
    margin: auto;
    text-align: center;
}
.link-drop li {
    list-style-type: none;

}
.link-drop a {
    text-decoration: none;
    color: #ebebee;
    float: right;
    margin-top: 18px;
    margin-right: 9px;
    margin-left: 9px;
    list-style-type: none;
}
.link-drop-ul {
    display: none;
    position: absolute;
    width: 122px;
    text-align: center;
    -webkit-transition: all .1s ease-out;
    -moz-transition: all .1s ease-out;
    -ms-transition: all .1s ease-out;
    -o-transition: all .1s ease-out;
    transition: all .1s ease-out;
    padding: 0;
    margin:0;
    left: 50%;
    -moz-transform: translatex(-50%);
    -ms-transform: translatex(-50%);
    -o-transform: translatex(-50%);
    -webkit-transform: translatex(-50%);
    transform: translatex(-50%);
    top:100%;
    padding-top:20px;
}
.link-drop-box {
    padding-bottom: 1px;
    padding-top: 1px;
    background-color: white;
    box-shadow: 0px 0px 38px -10px black;
    border-radius: 5px;
    margin: auto;
}
.triangle {
    width: 0;
    height: 0;
    position: relative;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-bottom: 8px solid white;
    left: 50%;
    margin-left: -8px;
}
.link-drop-ul li {
    margin-top: 15px;
    margin-bottom: 15px;
}
.link-drop-ul li a {
    color: black;
    float: none;
}
.link-drop-ul li a:hover {
    color: #d1d1d1;
}
.link-drop:hover > .link-drop-ul {
    display: block;
    position: absolute;

}
<li class="link-drop"><a href="">Employer</a>
          <ul class="link-drop-ul">
              <div class="triangle"></div>
              <div class="link-drop-box">
                  <li><a class="link-drop-a" href="{% url 'createjoblisting' %}">Add Listing</a></li>
                  <li><a class="link-drop-a" href="{% url 'featurenotimplemented' %}">How it works</a></li>
              </div>
          </ul>
      </li>

关于HTML 下拉菜单无法正确定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32944150/

相关文章:

css - 使用平板电脑的导航下拉菜单

javascript - 两个按钮分别打开 2 个 iframe

CSS 检查不聚焦

html - Div 未能对内容产生任何影响

html - 避免在 "disabled"表行上进行用户交互

android - ActionBar 后退按钮 StackTrace 错误

javascript - React Native 导航(未定义不是一个对象.. this.props.navigation.navigate

css - Wordpress - (theme=Unsigned) 禁用响应?

html - CSS3 圆 Angular 图像在 Safari/Chrome 中不起作用?

php - 根据数据库输出在单选按钮上加载具有特定背景颜色的页面