"Using CSS to ensure that the submenu is properly aligned with its parent menu

I am encountering an issue with the vertical positioning of a Submenu and I'm looking to address it using CSS, without involving JS.

Here is an image showing the current setup:

https://i.sstatic.net/B37qT.jpg

I have highlighted in red how the Submenu should be aligned.

When hovering over the parent element on the left, the corresponding submenu opens as expected.

My challenge lies in dynamically aligning the submenu with its parent menu element.

While applying the "top:" CSS property, I can only use static values like "48px" that are based on the entire div. However, I need a dynamic solution for each parent menu element.

Thanks in advance.

HTML Code:

.dropdown {
  float: left;
  text-align: center;
  margin-right: 25px;
  position: relative;
  display: inline-block;
  z-index: 1;
}

.menu-button {
  background-color: transparent;
  background-repeat: no-repeat;
  border: none;
  cursor: pointer;
  overflow: hidden;
  outline: none;
  z-index: 1;
}

.first-level-dropdown-content {
  position: relative;
  left: 0;
  background-color: white;
  box-shadow: 0 6px 14px 0 rgba(0,0,0,0.2);
  z-index: 1;
  text-align: left;
  white-space: nowrap;
  width: auto;
}

.first-level-dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.first-level-dropdown-content a:hover {
  background-color: #f1f1f1;
}

.dropdown:hover .first-level-dropdown-content {
  display: block;
}

.second-level-dropdown-content {
  position: relative;
  left: 100%;
  background-color: white;
  box-shadow: 0 2px 8px 0 rgba(0,0,0,0.2);
  z-index: 1;
  //top: 48px;
}

nav div div {
  display: none;
  position: absolute;
}

nav div div a:hover > div {
  display: inherit;
}
<nav id="menu-items" aria-label="menu">
      <div class="dropdown">
        <button class="menu-button">Services <i class="arrow-down"></i></button>
        <div class="first-level-dropdown-content">
          <a href="#">Services <i class="arrow-right"></i></a>
          <a href="#">Services <i class="arrow-right"></i></a>
          <a href="#">Services <i class="arrow-right"></i>
          <div class="second-level-dropdown-content">
            <a href="#"> Test Service </a>
            <a href="#"> Test Service </a>
            <a href="#"> Test Service Test </a>
            <a href="#"> Test Service Test </a>
            <a href="#"> Test Service Test </a>
          </div>
          </a>
          <a href="#">Services <i class="arrow-right"></i></a>
          <a href="#">Services <i class="arrow-right"></i>
            <div class="second-level-dropdown-content">
              <a href="#"> Test Service </a>
              <a href="#"> Test Service </a>
              <a href="#"> Test Service Test </a>
              <a href="#"> Test Service Test </a>
              <a href="#"> Test Service Test </a>
            </div>
          </a>
          <a href="#">Services <i class="arrow-right"></i></a>
          <a href="#">Help</a>
        </div>
      </div>
    </nav>

Answer №1

One commenter pointed out that your code currently only works up to the first level, but there is potential for it to work beyond that.

If your second-level-dropdown-content always relates to the previous <a>, a simple solution could be applying top: -40px; to align it.

If your second-level-dropdown-content needs to dynamically adjust to different <a>, you have two options:

  1. Utilize some Javascript to handle positioning
  2. Experiment with the CSS "anchor" property, as demonstrated in this video: https://youtu.be/B4Y9Ed4lLAI?si=0wJevHi3FF2Vv82C

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

Creating a new tab within a window that is already open

I have an interesting challenge - I need to open a new tab in a window that was previously opened using window.open(). So, if the window is already open, I want to open a tab in that same window. I have attempted to reference the existing window to open a ...

Angular: Oops! Encountered some template parsing issues: Surprising closing tag encountered

Recently, I created a brand new project with Angular CLI on 05/25/17. I decided to copy and paste some HTML code into a new component in the project. Surprisingly, this HTML code, which worked perfectly fine as its own standalone page, is now causing compi ...

What steps can be taken to ensure that an element is perfectly centered within a div?

I have a div containing some images: <div> <img src="1.png"> </img> <img src="1.png"> </img> </div> Currently, the layout in browsers looks like this: ----------------- |XX | ----------------- H ...

Tips for showcasing two pieces of content next to each other across the entire webpage using a combination of Bootstrap, CSS, and Laravel

I am facing a challenge with displaying two contents side by side on a full page and stacking them vertically when the screen size is small (mobile). I have managed to achieve the side by side display on a full page, but they stack on top of each other on ...

Creating an array of form input names using JavaScript within the HTML input tag

I have a two part question that I'm hoping someone can help me with. There was a similar question asked recently that included information about this particular type of array in PHP, but unfortunately, I can't seem to locate it at the moment. 1. ...

Struggle with the accordion collapse animation in Bootstrap4?

My implementation of accordion collapse in Bootstrap 4 is functional, but lacks smoothness. Can anyone offer suggestions on how to improve this? I have attempted to nest divs inside each other, but it has not yielded the desired outcome. Below is the code ...

Problem: Challenge in Iterating and Assigning Variables to HTML Table Elements

I am currently working on a project that involves tracking the order statuses of individual shipments from a list of URLs stored in an Excel sheet. The code I have written is capable of looping through the URLs and extracting information from them successf ...

Creating a CSS grid layout with a scrollbar positioned on the left side and content filling from left to right

How can I move the scrollbar to the left while keeping ng-repeat population from left to right? I'm using an ng-repeat to fill a grid that is 3 by X (Width by height). By default, the scroll bar appears on the right side. I want to position it on the ...

The internal style and script specified within the <head> section are not being rendered

Within my Joomla website using the T3 template, I inserted the following "Custom Code" just before the closing </head> tag: <style type="text/stylesheet"> div.t3-sidebar.t3-sidebar-right{ background: #F8F8F8 none repeat scroll 0% 0%; ...

Incorporating a variety of functions into an external javascript file

I am currently working on enhancing the functionality of a basic HTML page by incorporating JavaScript with multiple buttons. The problem arises when I attempt to introduce another function in my external JS file, as it causes the existing code to stop wor ...

How to vertically center a card between a search bar and the bottom of the viewport using Bootstrap?

I'm struggling to center a card vertically between the search bar and the bottom of the viewport. Even after setting the body height to 100%, the content extends beyond the viewport, leaving white space that users can scroll into. I've attempted ...

Issues with Safari text shadow not rendering properly when extending beyond the element border

I've encountered a situation where I have a text shadow applied with the following CSS: body { background: white; } h1 { color: black; text-shadow: 100px 100px 10px black; } https://i.sstatic.net/j8OTS.png When viewing in Safari browser, t ...

Is it possible for a draggable position:absolute div to shrink once it reaches the edge of a position:relative div

I am facing an issue with draggable divs that have position:absolute set inside a position:relative parent div. The problem occurs when I drag the divs to the edge of the parent container, causing them to shrink in size. I need the draggable divs to mainta ...

What is the best way to customize the MenuProps of Material UI Select component using createTheme?

I'm trying to update the dropdown menu style of my Material UI Select component using createTheme, but I'm having trouble getting it to work. https://i.sstatic.net/zpVjW.png Here is the code I have so far. Can you spot what might be causing the ...

I am puzzled as to why my jQuery height and width functions are returning NaN as the result

I am attempting to calculate the ratio of height and width for each image on a webpage. Unfortunately, I am consistently receiving a NaN result and I cannot figure out why. I have been using get() and find() <div class='image-wrapper'> ...

Tips for optimizing character count for mobile web pages with varying screen sizes and font settings

As I embark on developing an eBook mobile app, I am faced with various considerations such as screen size, font size, and determining the number of paragraphs to include on a single page based on the current screen and font settings. My aim is to adjust t ...

Duplicate the appearance of a webpage exactly as it is displayed, without regard to the underlying source code

Looking to transfer content from a webpage to a Word document? Specifically, an installation guide, with additional comments about our own installation process. Seems easy enough - just copy and paste, right? Not quite. The issue lies in the structure of ...

Struggling with getting the Bootstrap Carousel to switch between slides?

I'm facing a challenge in creating a functioning carousel with Bootstrap. I've utilized a template for this purpose, but the issue lies in the slides not transitioning correctly when I click the arrow buttons to move to the next or previous slide ...

Adjust the size of the div menu according to the window's dimensions

Is there a way to make a menu that will resize both the width and height of the window? I've managed to resize the width using %, but for some reason, the height isn't cooperating. I've experimented with max-width/height settings and tried ...

"Learn how to extract the image URL from the configuration file (config.json) within the assets folder, and then seamlessly display it within

In my Angular project, I have a configuration file located in the assets folder: { "brandConfig": "brand1", "brand1": {"urlPath": "http://192.168.168.60:8081/mantle-services", " ...