Pure CSS Drop-Down Menu Misalignment

Struggling with aligning the drop-down menu to the "Services" link. Every time I hover over the "services" link, it shifts off to the left. I tried using the "left" function on nav ul ul, and although it somewhat worked, it doesn't respond when I minimize the window. It only functions at the specific window size I adjusted it to. This issue has me puzzled. https://i.sstatic.net/ByKa3.png

/* Basic */
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
     body{  
      font-family: 'Acme', sans-serif;
      background: white;  
      line-height: 1.6;
     }
     ul{   
       list-style: none;
     }
    
     a{
       text-decoration: none;
     }
    
     /* Navbar */
     .navbar{
         display: flex;
         background: #004C66;
         align-items: center;
         justify-content: space-between;
         width: 100%;
         position: fixed;
    
         padding: 0 30px;
     }
    /* To Keep Control of the Logo */
     .navbar .logo img{
         width: 100%;
         margin-top: 10px;
     }
    
     /* To Make the Desktop Nav Buttons Inline */
     .navbar ul li{
        display: inline;
     }
    
     .navbar ul li .nav-link{
         padding: 10px 25px;
         color: #fff;
         font-size: 30px;
         transition-duration: 500ms;
     }
    
     .navbar .nav-link:hover{
         color: black;
     }
    
     /* NAVBAR DROP DOWNS */
    
        /* Hide Dropdowns By Default */
        nav ul ul {
            display: none;
            position: absolute; 
            top: auto; /* the height of the main nav */
            text-align: center;
            justify-items: center;
            border-radius: 10%;
        }
        
        /* Display Dropdowns on Hover */
        nav ul li:hover > ul {
            display:flex;
            background-color: teal;
            flex-direction: column;
        }
    
        /* First Tier Dropdown */
        nav ul ul li {
            width:auto;
            top: 0px;
            float:none;
            display:list-item;
            position: relative;
        }
    
        /* Second, Third and more Tiers */
        nav ul ul ul li {
            position: relative; 
        }
<header class="navbar">
    <a href="" class="logo"><img src="/img/logoS.png" alt=""></a>

        <nav class="desktop-view">
            <ul>
                <li><a class="nav-link" href="#">Home</a></li>
                
                <li><a class="nav-link services" href="#">Services</a>
                <!-- First Tier Drop Down -->
                    <ul>
                        <li><a class="nav-link" href="#">Tile</a></li>
                        <li><a class="nav-link"href="#">Mexican Tile</a></li>
                        <li><a class="nav-link"href="#">Marble Floor</a></li>
                        <li><a class="nav-link"href="#">Shower Regrout</a></li>
                    </ul>
                </li>

                <li><a class="nav-link" href="#">Contact</a></li>
            </ul>
        </nav>
</header>


 

Answer №1

You need to adjust the position of .navbar ul li to relative

Afterward, center your dropdown menu using left: 50% and transform: translateX(-50%)

/* Essential Styles */
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
     body{  
      font-family: 'Acme', sans-serif;
      background: white;  
      line-height: 1.6;
     }
     ul{   
       list-style: none;
     }
    
     a{
       text-decoration: none;
     }
    
     /* Navigation Bar Styling */
     .navbar{
         display: flex;
         background: #004C66;
         align-items: center;
         justify-content: space-between;
         width: 100%;
         position: fixed;
    
         padding: 0 30px;
     }
    /* Logo Adjustment */
     .navbar .logo img{
         width: 100%;
         margin-top: 10px;
     }
    
     /* Inline Desktop Nav Buttons */
     .navbar ul li{
        display: inline;
        position: relative;
     }
    
     .navbar ul li .nav-link{
         padding: 10px 25px;
         color: #fff;
         font-size: 30px;
         transition-duration: 500ms;
     }
    
     .navbar .nav-link:hover{
         color: black;
     }
    
     /* NAVBAR DROP DOWNS */
    
        /* Default Hide for Dropdowns */
        nav ul ul {
            display: none;
            position: absolute; 
            text-align: center;
            border-radius: 10%;
            left: 50%;
            width: 300px;
            transform: translateX(-50%);
        }
        
        /* Display on Hover */
        nav ul li:hover > ul {
            display:flex;
            background-color: teal;
            flex-direction: column;
        }
    
        /* First Tier Dropdown */
        nav ul ul li {
            width:auto;
            top: 0px;
            float:none;
            display:list-item;
            position: relative;
        }
    
        /* Subsequent Tiers */
        nav ul ul ul li {
            position: relative; 
        }
<header class="navbar">
    <a href="" class="logo"><img src="/img/logoS.png" alt=""></a>

        <nav class="desktop-view">
            <ul>
                <li><a class="nav-link" href="#">Home</a></li>
                
                <li><a class="nav-link services" href="#">Services</a>
                <!-- First Tier Drop Down -->
                    <ul>
                        <li><a class="nav-link" href="#">Tile</a></li>
                        <li><a class="nav-link"href="#">Mexican Tile</a></li>
                        <li><a class="nav-link"href="#">Marble Floor</a></li>
                        <li><a class="nav-link"href="#">Shower Regrout</a></li>
                    </ul>
                </li>

                <li><a class="nav-link" href="#">Contact</a></li>
            </ul>
        </nav>
</header>

Answer №2

Elements that are absolutely positioned are placed in relation to the nearest parent element with a set position. Therefore, when the navigation ul ul has a position: absolute, it will be aligned relative to the first ancestor element with a defined position.

To ensure that the drop-down menu aligns properly with its parent li, apply position: relative to that specific element.

Answer №3

view screenshot of navigation bar

To achieve a similar dropdown menu appearance, modify the following in your CSS code:

nav ul{   
       list-style-type: none;
       display: flex;
       position: relative;
     }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pricing determined by location on a website created with HTML

We are looking to customize our HTML5/CSS3 website by displaying different pricing tables based on the location of each visitor. Our goal is to have a fixed price for users from Singapore while showing a different price for visitors from other parts of th ...

Ensure the footer remains at the bottom of the page without utilizing a minimum height of 100

When trying to address the common issue of making the footer stay at the bottom of a page, one popular solution is to enclose everything in a div with position:relative and min-height:100%, as explained in this helpful tutorial here. The challenge arises ...

Changing the description doesn't affect the fixed height of the box - here's how to do it with CSS

Here are the references I used: GetBootstrap Jumbotron CoreUI Base Jumbotron This is how my script looks like: <template> <div class="wrapper"> <div class="animated fadeIn"> <b-card> <b-row v-for="row in ...

There seems to be a syntax error in the Textillate Plugin, where an IntegerLiteral was expected but ""

I am currently incorporating the Textillate plugin for text animation in my web application. However, I am encountering a Syntax error in Eclipse when defining the JavaScript for the animation, particularly with the 'in' definition. How can I res ...

The vertical height scrollbar will automatically show up when a margin-top is added

My HTML: div.container My CSS: html, body { height: 100%; background-color: red; padding-left: 0px; padding-right: 0px; } .container { height: 100%; background-color: blue; margin-top: 5px; *zoom: 1; } I am looking to achieve a spec ...

The process of downloading a webpage onto a computer is not completely successful

I recently created a webpage on WIX and attempted to download it using CTRL+S. However, when I loaded the site from my computer, certain elements were not functioning properly. Is there a specific piece of code that is missing when saving the webpage in th ...

Implementing Angular JS to Fill a Drop-down Menu with JSON Data

Here is my HTML code: <body ng-app="mainApp"> <div ng-controller="myController"> <select> <option ng-repeat="person in persons">{{ person.data }}</option> </select> </div> </body> This is my JavaScript ...

My Horizontal Drop Down Menu won't stay still, how can I make it stop moving?

I'm a beginner with dropdown menus and I'm struggling to prevent my dropdown menu from expanding and pushing the main buttons. I think it's related to a positioning adjustment, but I can't pinpoint where or what it is. Here is the link ...

Put the code inside a function. I'm new to this

My goal is to encapsulate this code: if($(window).width() > 980) { $(window).on("scroll", function() { if($(window).scrollTop() > 20) { //add black background $(".x-navbar").addClass("active"); $(".x-navbar .desktop ...

What is the best way to select a random set of n elements from an array using angularjs

My array contains a list of elements. app.controller("MainController", function($scope){ $scope.names= [ { value: "q1" }, { value: "q2" }, { value: "q3" } ]; }); ...

AngularJS: Solving the issue of controllers not exchanging data by utilizing a service

I am working with the following controllers and service: angular.module('myApp', []); angular.module('myApp').factory('ShareService', function() { var information = {}; information.data = ""; information.setD ...

Disabling a text area or mat-form-field within the same HTML file in Angular 9

Currently, I am working with Angular 9 and angular Material Reactive Forms. My goal is to disable certain fields without relying on the disabled status within formControl in the TypeScript file. Instead, I want to achieve this solely through HTML. Here is ...

Optimal technique for implementing up and down arrows in a select dropdown

What's the most effective way to implement a percentage arrow up/down slider/selector within a select element on a webpage like this: https://i.sstatic.net/ygu55.png Should we use a select list with a dedicated background? If so, how do we detect i ...

Changing the file format from FLV to MP4

After using the mencoder software, I successfully converted the flv files to mp4 format by executing the given command: mencoder input.flv -o outout.mp4 -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vbitrate=800:mbd=2:mv0:trell:v4mv:cb ...

Problems encountered when applying box-shadow for text background across multiple lines

Lately, I've become accustomed to using box-shadow to add backgrounds to text headings. I've been employing a little trick of applying a box-shadow to a span element within h1 tags in order to create a unique "background with padding" highlight e ...

Removing a modal div element in React after navigating

import React, { useState } from "react"; import { useNavigate } from "react-router-dom"; import axios from "axios"; import Cookies from "js-cookie"; const LoginPage = () => { const [email, setEmail] = useState( ...

Ensure that the button becomes clickable only after text has been inputted into the field

How can I make my button active after entering text into the text box? I would appreciate any help or guidance. Here is the code I have been working on: <body> <script> function ActivateButton() { if (input1.value != null) { send ...

Reorganize Material UI Grid Items with varying lengths

I'm currently working with a Material UI grid system that appears as shown below: <> <Typography gutterBottom variant='h6'> Add New User{' '} </Typography> <Grid container spacing={3} ...

Performing functions in a sequence with JQuery

I've encountered an issue with executing functions in sequence. In my HTML file, I have the following code: <body onload="start();"> <div id="title"><div id="welcome">WELCOME</div></div> Within my JS code, I have a ...

Sending a form using an AngularJS dropdown menu

I have recently started using angularjs and decided to switch out a traditional html <select> box for an angular modal select box. The select box successfully populates with data from a $http ajax request, but I am facing issues with form submission ...