Search code examples
htmlcssnavbar

Navigationbar dropdown menu weird placement


I'm trying to make a dropdown menu to my navigation bar but when I hover over the London tab, the dropdown menu is slightly placed to the right. I have no idea as to why this is happening, and I've tried some solutions but they have not been successful.

I dont know how to make the dropdown menu centered so its just below the London tab. Any help or advice would be appriciated!

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>


Solution

  • The problem is, when position:absolute is used, the default for "right" is not 0.

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

    This should fix the problem:

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