Responsive menu with nested submenu

Hi, I'm currently facing some challenges with my mega menu. My goal is to replicate the style of Newegg's mega menu where a small sub-menu appears upon hovering over a menu item. You can view my Codepen example here: https://codepen.io/iamgonge/pen/vxEEeN

The issue I'm encountering is aligning the sub-menu with the caret icon in each menu link. Currently, the sub-menu stays in the same position and when I change it from Absolute positioning, it causes the content to shift down on hover.

/* Body  */

body {
  margin: 0px;
  padding: 0px;
  font-family: Helvetica;
  background-color: #fff;
}
header {
  min-height: 450px;
}
header.dd-blue {
  background-color: #fff;
}

/* Nav */

nav {
  position: relative;
}
header.dd-blue nav {
  background-color: #4F96BA;
}
ul.main-nav {
  list-style-type: none;
  padding: 0px;
  font-size: 0px;
  max-width: 1000px;
  margin: 0 auto;
}
ul.main-nav h2 {
  font-size: .9em;
  font-weight: 300;
}
ul.main-nav>li {
  display: inline-block;
  padding: 0;
}
ul.main-nav>li>a {
  display: block;
  padding: 20px 30px;
  position: relative;
  color: #fff;
  font-size: 15px;
  font-weight: 400;
  box-sizing: border-box;
}
ul.main-nav>li:hover {
  background-color: #f9f9f9;
}
ul.main-nav>li:hover>a {
  color: #333;
  font-weight: 400;
}
ul.main-nav>li ul.sub-menu-lists {
  margin: 0px;
  padding: 1px;
  list-style-type: none;
  display: block;
}
ul.main-nav>li ul.sub-menu-lists>li {
  padding: 0 0px;
  margin-top: 2px;
}
ul.main-nav>li ul.sub-menu-lists>li>a {
  font-size: .84em;
  font-weight: 500;
}
.sub-menu-head {
  margin: 10px 0;
  border-bottom: 1px solid #4F96BA;
  width: 100%;
} 

@media only screen and (min-width: 769px) {
  /* Desktop */
  ul.main-nav {
    display: block;
    position: relative;
  }
  .sub-menu-block {
    padding: 15px;
  }
  /* Sub-menu */
  ul.main-nav>li>div.sub-menu-block {
    visibility: hidden;
    background-color: #f9f9f9;
    position: absolute;
    margin-top: 0px;
    width: 100%;
    color: #333;
    left: 0;
    box-sizing: border-box;
    z-index: 3;
    font-size: 16px;
    border-left: 1px solid #ccc;
    border-right: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    opacity: 0;
    /*CSS animation applied for sub menu : Slide from Top */
    -webkit-transition: all 0.4s ease 0s;
    -o-transition: all 0.4s ease 0s;
    transition: all 0.4s ease 0s;
    -webkit-transform: rotateX(90deg);
    -moz-transform: rotateX(90deg);
    -ms-transform: rotateX(90deg);
    transform: rotateX(90deg);
    -webkit-transform-origin: top center;
    -ms-transform-origin: top center;

     transform-origin: top center;
  }
  ul.main-nav>li:hover>div.sub-menu-block {
    background-color: #fff;
    visibility: visible;
    opacity: 1;
    -webkit-transform: rotateX(0deg);
    -moz-transform: rotateX(0deg);
    -ms-transform: rotateX(0deg);
    transform: rotateX(0deg);
  }
  ul.main-nav>li>div.sub-menu-block>* {
    -webkit-transition-property: opacity;
    -moz-transition-property: opacity;
    -o-transition-property: opacity;
    transition-property: opacity;
    -webkit-transition-duration: 0.4s;
    -moz-transition-duration: 0.4s;
    -o-transition-duration: 0.4s;
    transition-duration: 0.4s;
    opacity: 0;
  }
  ul.main-nav>li:hover>div.sub-menu-block>* {
    opacity: 1;
  }
  .sub-menu-head {
    font-size: 20px;
  } 
  
 
 [...]
       
  </li>      
</ul>
              </div>
            </div> 
          </div> <!--end of sub-menu-block -->
        </li>
     
      </ul>
[...]

Answer №1

Adjust the CSS properties to

position: absolute; display: inline-block;
while removing any existing left and top values, ensuring that the element is displayed at its original position in the DOM.

/* Body  */

body {
  margin: 0px;
  padding: 0px;
  font-family: Helvetica;
  background-color: #fff;
}
header {
  min-height: 450px;
}
header.dd-blue {
  background-color: #fff;
}

/* Nav */

nav {
  position: relative;
}
header.dd-blue nav {
  background-color: #4F96BA;
}
ul.main-nav {
  list-style-type: none;
  padding: 0px;
  font-size: 0px;
  max-width: 1000px;
  margin: 0 auto;
}
ul.main-nav h2 {
  font-size: .9em;
  font-weight: 300;
}
ul.main-nav>li {
  display: inline-block;
  padding: 0;
}
ul.main-nav>li>a {
  display: block;
  padding: 20px 30px;
  position: relative;
  color: #fff;
  font-size: 15px;
  font-weight: 400;
  box-sizing: border-box;
}
ul.main-nav>li:hover {
  background-color: #f9f9f9;
}
ul.main-nav>li:hover>a {
  color: #333;
  font-weight: 400;
}
ul.main-nav>li ul.sub-menu-lists {
  margin: 0px;
  padding: 1px;
  list-style-type: none;
  display: block;
}
ul.main-nav>li ul.sub-menu-lists>li {
  padding: 0 0px;
  margin-top: 2px;
}
ul.main-nav>li ul.sub-menu-lists>li>a {
  font-size: .84em;
  font-weight: 500;
}
.sub-menu-head {
  margin: 10px 0;
  border-bottom: 1px solid #4F96BA;
  width: 100%;
}

@media only screen and (min-width: 769px) {
  /* Desktop */
  ul.main-nav {
    display: block;
    position: relative;
  }
  .sub-menu-block {
    padding: 15px;
  }
  /* Sub-menu */
  ul.main-nav>li>div.sub-menu-block {
    visibility: hidden;
    background-color: ...

#rds-font {
  font-size: 15px;
}
a, a:active, a:visited, a:link {
  color: #599ab9;
  text-decoration: none;
}
a:hover {
  color: black;
}
#left-border {
  border-left: 1px solid #666;
}

/* Hover dropdown */

.dropdown ul.dropdown-menu {
  margin-top: 0;
}


.hover_drop_down:hover ul.dropdown-menu {
  position: absolute;
  display: inline-block;
  z-index: 1;
  left: auto;
  top: auto;
}

.caret-right {
  display: inline-block;
  width: 0;
  height: 0;
  margin-left: 2px;
  vertical-align: middle;
  border-left: 5px solid;
  border-left-color:#1c2b36;
  border-bottom: 5px solid transparent;
  border-top: 5px solid transparent;
}
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>

  <header class="dd-blue">
    <nav role="navigation">
      <ul class="main-nav">
        <li class="top-level-link">
          <a class="mega-menu" id="left-border"><span>Main 1</span></a>
          <div class="sub-menu-block">
            <div class="row">
              <div class="col-xs-15">
                <h2 class="sub-menu-head">Header 1</h2>
                <ul class="sub-menu-lists">
                  <li class="hover_drop_down">
                    <a href="#">Item with sub-menu<span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">                     
                      <li><a href="#">Item X</a></li>
                    
                    </ul>
                  </li>        
                   <li class="hover_drop_down">
                    <a href="#">Item with sub-menu<span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">                     
                      <li><a href="#">Item X</a></li>
                    
                    </ul>
                  </li>      
                </ul>
              </div>
              
              
              
            </div> 
          </div> <!--end of sub-menu-block -->
        </li>
     
        
        
      </ul>
    </nav>
  </header>

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

Arrangement of items in a grid with various sizes

I have encountered a challenge that I cannot seem to overcome. In my grid system, there are 18 identical items/boxes. I am looking to remove 4 of those items/boxes and combine them into one large item/box. Refer to the wireframe below for guidance on how ...

Is it possible to nest an HTML <span> inside a label tag in a Rails form_for?

Is it possible to nest a span element inside a form_for label tag? I am trying to achieve this in order to style a specific part of the label using CSS, such as making the text red. While it seems like valid HTML based on my research, it is causing some is ...

How can I change the z-index specifically for one of the select2.js elements?

Within my setup, there are 4 distinct elements: element 1 resides in a fixed header while elements 2, 3, and 4 are part of my form. All of these elements have been customized with select2 (a select box that can be tailored to allow searching). The issue ...

Broken responsive carousel using bootstrap

Looking for some help with a responsive bootstrap carousel issue. The carousel items adjust themselves below each other when the screen size is reduced. I want them to display as single items in the carousel. Here are the screenshots and the code. Please ...

Synchronizing a webpage on various devices

I've encountered a dilemma with my code where the alignment of elements on my website looks different when viewed on a different computer. The pages were developed on my MacBook Pro and display correctly, but when I view them on my iMac, everything is ...

The functionality of the angularjs class seems to be malfunctioning

I'm a new learner of angularjs and I've created a simple page below. I have a style called "item" that I'm trying to apply to a div, but it's not working. However, if I use inline style, then it works fine. Can someone please help me f ...

Trim the data in the following table cell using CSS

I'm trying to create a list using a table structure: https://i.sstatic.net/ucICV.png However, the lengthy URL is causing the table to expand wider than desired. I have attempted solutions like: table { table-layout: fixed; width: 100%; } and td.t ...

Leveraging HTML div elements for forms can greatly enhance the user

I am in the process of developing a website where users can upload images, name them, and then submit the data to PHP code. Currently, I am using plain HTML and PHP for this project, but I intend to integrate the Bulma CSS library. The current HTML code l ...

Gradually make the table row and its contents disappear as you scroll, based on how hidden the row is within the containing div

Uncertainty clouds my mind as to the practicality of this approach, the potential performance implications, and the frequency of scroll events firing to make this technique viable. Nevertheless, within a fixed height div (overflow-y: auto) lies a table. M ...

Searching for elements in Python selenium by their background gradients is a useful way

Before, I used to locate elements in the following way: element = fox.find_element_by_xpath("//div[contains(@class, 'well')]/p[2]/a[@style='background:#0373F1;']") This method was effective! However, now the element has a gradient: ...

Personalized navigation bar using the Bootstrap 5 framework

I am seeking to create a unique custom navbar layout with specific requirements: A centered logo 3 collapsible menu buttons on the left 2 icons (account and Instagram) on the right, always visible When the left menu is collapsed, I want to display a hamb ...

Problem Encountered with Position Absolute in Bootstrap 3

Could you please review this demonstration and advise me on why I am encountering issues when trying to set up an absolute position on the first row? Thank you. html, body { height: 100%; } .element{ position:absolute; top:0; } .one{height:500px; backgro ...

Issue with animation transition not triggering on initial click

I am currently working on creating an animation for a form using JS and CSS. Here is the code snippet I have: var userInput = document.getElementById("login-user"); var userLabel = document.getElementById("user-label"); // User Label Functions funct ...

The image source is not functioning properly to display the image, even though it is working perfectly on

I'm currently attempting to retrieve an image from Instagram. Everything seems to be in order with the image link on Instagram. However, when I try to use it within an img tag, it fails to fetch. One thing worth noting is that this particular image d ...

Ways to position two buttons in each corner with flexbox in a react js application

I'm fairly new to React JS and have just started building a simple React app. I'm trying to place two buttons on each side using CSS flex, but I'm having trouble achieving it. Can anyone help point out where I might be going wrong? im ...

The correct order for the "float:right" property is as it appears in the HTML code

I am dealing with a container div containing two child divs, each with the float: right property. The structure of my code is as follows: <div class="container"> .container {width: 50%} <div class="a"></div> ...

Newly designed Bootstrap 4 input field with search feature positioned off-center on smaller screens

I have successfully positioned an input text next to a button as shown below: While it displays perfectly on a regular computer screen, the alignment gets skewed when viewed on a phone device: This is the functional code I am using: <!-- Add Filer Fo ...

Looking to include a toggle button in the menu for both desktop and mobile versions using Bootstrap

When viewing the website on a mobile device, the menu appears as a toggle button. However, on desktop, the menu is displayed without the toggle. Is there a way to show the menu in a toggle button on the desktop version using Bootstrap? ...

ReactJs: difficulty in resetting input field to empty string

I have an application using React v 0.13.1 (Old version). I am struggling to update my input field to "" after retrieving the updated value from the database. Scenario: I am updating the input fields by clicking on the button named "Pull&qu ...

Image Width in Firefox Parent Element Exceeds Limits

When using a responsive picture element, the parent element does not receive the correct width in Firefox on OS-X. Are there any solutions or workarounds for this issue? .picture-wrapper { float: left; } .next-element { float: left; } < ...