Navigationbar 下拉菜单奇怪的位置

Navigationbar dropdown menu weird placement

我正在尝试为我的导航栏制作一个下拉菜单,但是当我将鼠标悬停在“伦敦”选项卡上时,下拉菜单略微位于右侧。我不知道为什么会这样,我尝试了一些解决方案,但都没有成功。

我不知道如何使下拉菜单居中,所以它就在“伦敦”选项卡下方。如有任何帮助或建议,我们将不胜感激!

body {
    font-family: Arial;
}
.navbar {
    display: flex;
    margin-bottom: 0px;
    justify-content: center;
    gap: 50px;
    padding: 1em;
    padding-bottom: 4em;
    padding-top: 1.5em;
}
.navbar a {
    color: white;
    padding: 14px 20px;
    text-decoration: none;
    padding: 2em;
    padding-top: 1em;
    padding-bottom: 1em;
    background-color: #333;
    border-radius: 5px;
    transition: 0.2s;
}
.navbar a:hover {
    background-color: whitesmoke;
    color: black;
}
.active-dropdown {
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
    position: relative;
}
.active-dropdown {
    color: white;
    padding: 14px 20px;
    text-decoration: none;
    padding: 2em;
    padding-top: 1em;
    padding-bottom: 1em;
    background-color: #04AA6D !important;
    border-radius: 5px;
    transition: 0.2s;
}
.active-dropdown a{
    display: block;
    background-color: #11c784 !important;
    color: white;
    text-decoration: none;
    padding: 2em;
    padding-top: 1em;
    padding-bottom: 1em;
    border-radius: 5px;
    transition: 0.2s;
}
.active-dropdown > .dropdown-sub{
    display: none;
    z-index: 1;
    flex-direction: column;
    position: absolute;
    top: 50px;
    background: rgb(4, 199, 129);
    color: white;
    border-radius: 5px;
    transition: 0.2s;
    width: 100%;
}
.active-dropdown:hover > .dropdown-sub {
    display: flex;
}
.dropdown-option a:hover{
    background-color: #abcdef !important;
}
.active-dropdown a:hover {
    color: white;
    transform: scale(1);
}
<!DOCTYPE html>
<html>
<head>
    <title>City</title>
    <link href="london.css" rel="stylesheet" type="text/css">
</head>
<body>
        <div class="navbar">
            <div class="active-dropdown">
                <div class="dropdown-title">London</div>
                <div class="dropdown-sub">
                    <div class="dropdown-option"><a href="">Overview</a></div>
                    <div class="dropdown-option"><a href="">Wikipedia</a></div>
                    <div class="dropdown-option"><a href="">Pictures</a></div>
                </div>
            </div>
            <a style="align-self: flex-start" href="#">Paris</a>
            <a style="align-self: flex-start" href="#">Tokyo</a>
        </div>

一个非常简单的解决方案是将此添加到您的代码中:

 .active-dropdown > .dropdown-sub{
     display: none;
     z-index: 1;
     flex-direction: column;
     position: absolute;
     top: 50px;
     background: rgb(4, 199, 129);
     color: white;
     border-radius: 5px;
     transition: 0.2s;
     width: 100%; 
     left: 0px;  /* add this */
 }

问题是,当使用 position:absolute 时,“右”的默认值不是 0。

来源: What are the default top, left, botton or right values when position:absolute is used?

这应该可以解决问题:

.active-dropdown > .dropdown-sub {
  right: 0px;
}

首先,您应该添加left:0px。我建议改用百分比。以后如果size或者padding有变化就不需要再编辑了。


 .active-dropdown > .dropdown-sub{
     display: none;
     z-index: 1;
     flex-direction: column;
     position: absolute;

     top: 100%; /* Changed */
     left: 0%; /* Changed */

     background: rgb(4, 199, 129);
     color: white;
     border-radius: 5px;
     transition: 0.2s;
     width: 100%; 
     
 }