October 26, 2024
Chicago 12, Melborne City, USA
HTML

Navbar item selection rectangle and dropdown menu positioned with incorrect code


In this navbar when I move the mouse over one of its entries, that little green rectangle should appear exactly in the position of the photo. Also, under the little green rectangle that horizontal drop-down menu should appear exactly in the position of my photo.

enter image description here

Apparently it seems to work well, but there are problems in the css code, so I did not get this result in the best way. Previously I always got the little green rectangle that did not touch the bottom edge of the navbar and also the drop-down menu was previously detached from the navbar and there was a thin white line of empty space between the navbar and the drop-down menu. Now I have apparently solved the problem, but the code is definitely not correct.

How can I get the same result as the photo, but improving the code?

I think the wrong code is in:

.navbar-nav .nav-link:hover::after {
content: '';
display: block;
width: 100%;
height: 4px;
background-color: #459439;
 position: absolute;
 top: 100%;
 left: 0;
 z-index: 1000 } /* Orange rectangle at the bottom edge of the navbar */ .navbar-nav .nav-link { color: #ffffff;
 margin-right: 15px;
 position: relative;
 line-height: 21px;

} @media (min-width: 768px) { .navbar-expand-md .navbar-nav .nav-link { padding-right: .5rem;
 padding-left: .5rem;
 padding-top: 31px;
 padding-bottom: 31px;
 } } .navbar { z-index: 1000;
} .dropdown-menu-horizontal { z-index: 9999;
}

index.html:

<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Example</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="styles.css">
</head>
<body>


<!-- Navbar con logo e link -->
<nav class="navbar navbar-expand-md navbar-dark mb-0">
    <div class="container">
        <a class="navbar-brand d-flex align-items-center" href="#">
            <img src="img/.png" alt="Logo" class="logo">
            <div class="brand-text">
                <span class="aaaaaaa">AAAAAAA</span>
                <span class="xxxx">XXXX</span>
            </div>
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item dropdown">
                    <a class="nav-link" href="#" id="fondazioneDropdown">COMPANY</a>
                    <div class="dropdown-menu dropdown-menu-horizontal" aria-labelledby="fondazioneDropdown">
                        <a class="dropdown-item-horizontal" href="#"><img src="..." alt="Logo 1"> Example 1</a>
                        <a class="dropdown-item-horizontal" href="#"><img src="..." alt="Logo 2"> Example 2</a>
                        <a class="dropdown-item-horizontal" href="#"><img src="..." alt="Logo 3"> Example 3</a>
                    </div>
                </li>
                <li class="nav-item"><a class="nav-link" href="#">Item 2</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Item 3</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Item 4</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Item 5</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Item 6</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Item 7</a></li>
                <li class="nav-item"><a class="nav-link" href="#">Item 8</a></li>
            </ul>
        </div>
    </div>
</nav>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="script.js"></script>
</body>
</html>

styles.css:

* {
    box-sizing: border-box;
}



/* Navbar */
.navbar {
    background-color: #000033;
    padding: 0px;
    position: relative;
    z-index: 1000;
    margin: 0;  /* Elimina eventuali margini */
    
}

/* Stile logo e testo del brand */
.logo {
    height: 80px;
    margin-right: 10px;
}

.brand-text {
    color: #ffffff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    text-align: left;
    line-height: 1;
}

.aaaaaaa {
    font-size: 0.80em;
    font-weight: normal;
    letter-spacing: 0.28em;
}

.xxxx {
    font-size: 2.4rem;
    font-weight: bold;
    letter-spacing: 0.05em;
}

/* Rettangolino arancione sul bordo inferiore della navbar */
.navbar-nav .nav-link {
    color: #ffffff;
    margin-right: 15px;
    position: relative;
    line-height: 21px; /* Aumenta leggermente l'altezza della riga per migliorare l'allineamento */


}



.navbar-nav .nav-link:hover::after {
    content: '';
    display: block;
    width: 100%;
    height: 4px; /* Riduci leggermente l'altezza del rettangolo */
    background-color: #459439;
    position: absolute;
    top: 100%; /* Posiziona il rettangolo esattamente sotto la voce di menu */
    left: 0;
    z-index: 1000
}

/* Rimozione della freccia dropdown */
.nav-link.dropdown-toggle::after {
    display: none;
}

.navbar-dark .navbar-nav .nav-link {
    color: white;
}

/* Dropdown orizzontale */
.dropdown-menu-horizontal {
    display: none;
    background-color: #000033;
    padding: 20px;
    min-width: 100%;
    border-radius: 0; /* Rimosso il bordo arrotondato */
    position: absolute;
    left: 0;
    top: 100%; /* Cambia il valore top per allinearlo perfettamente sotto la navbar */
    z-index: 9999; /* Assicura che venga mostrato sopra tutto il resto */


}



.dropdown-item-horizontal {
    color: #ffffff;
    display: inline-flex;
    align-items: center;
    padding: 10px 20px;
    margin-right: 30px; /* Distanza tra gli elementi */
    white-space: nowrap;
}

.dropdown-item-horizontal img {
    width: 50px; /* Dimensioni delle icone */
    height: 50px;
    margin-right: 15px;
}

.dropdown-item-horizontal:hover {
    color: #d4d4ff;
    text-decoration: none;
}

/* Mostra il menu a tendina al passaggio del mouse */
.nav-item:hover .dropdown-menu-horizontal {
    display: flex;
    justify-content: space-around;
}


@media (min-width: 768px) {
    .navbar-expand-md .navbar-nav .nav-link {
        padding-right: .5rem;
        padding-left: .5rem;
        padding-top: 31px;
        padding-bottom: 31px;
    }
}

script.js:

document.addEventListener("DOMContentLoaded", function () {
    const dropdowns = document.querySelectorAll(".nav-item.dropdown");

    dropdowns.forEach(dropdown => {
        dropdown.addEventListener("mouseenter", function () {
            const menu = this.querySelector(".dropdown-menu-horizontal");
            if (menu) menu.style.display = "flex";
        });

        dropdown.addEventListener("mouseleave", function () {
            const menu = this.querySelector(".dropdown-menu-horizontal");
            if (menu) menu.style.display = "none";
        });
    });
});



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video