The sub-navigation element goes beyond the traditional navigation bar and causes misalignment

I'm currently in the process of developing sub navigation bars that expand upon hover. However, I've encountered an issue where hovering over "Manage" triggers the regular box to expand. This is happening because of the position: relative within .bujar:hover .bujar-content.

When I change the position to absolute, everything works as expected.

However, when resizing the screen, it performs perfectly fine with position: relative, but not with position: absolute on mobile.

If you'd like to see the code in action, here's the link to the CodePen version: https://codepen.io/bujars/pen/BgErzp

.bujar:hover .bujar-content {
  position: relative;
  display: block;
  float: left;
  left: 100%;
  top: -35px;
}
<div class="topnav responsive" id="myTopnav">
  <a href="/">Home</a>
  <div class="dropdown">
    <button class="dropbtn">India<i class="fa fa-caret-down"></i></button>
    <div class="dropdown-content">
      <a href="#">Dashboard</a>

      <div class="bujar">
        <button class="bujar-dropbtn" style="
">Manage<i class="fa fa-caret-right"></i></button>
        <div class="bujar-content" style="
    /* display: block; */
">


          <a href="#">Manage 1</a>
          <a href="#">Manage 2</a>
          <a href="#">Manage 3</a>
          <a href="#">Manage 4</a>
        </div>
      </div>

      <a href="#">x</a>
      <a href="#">y</a>
      <a href="#">z</a>

      <div class="bujar">
        <button class="bujar-dropbtn" style="
    /* color: red; */
">Manage<i class="fa fa-caret-right"></i></button>
        <div class="bujar-content" style="
    /* display: block; */
">


          <a href="#">Manage 1</a>
          <a href="#">Manage 2</a>
          <a href="#">Manage 3</a>
          <a href="#">Manage 4</a>
        </div>
      </div>


    </div>
  </div>
  <div class="dropdown">
    <button class="dropbtn">G<i class="fa fa-caret-down"></i></button>
    <div class="dropdown-content">
      <a href="#">D</a>
      <a href="#">S2</a>

    </div>
  </div>

  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a>
</div>

Answer №1

Solving this issue involves adjusting the placement of .bujar:hover .bujar-content to absolute.

To ensure that position:absolute functions correctly on smaller screens, some modifications to specific CSS classes in your code are necessary. Below are the classes that require editing or inclusion.

.bujar:hover .bujar-content 
 {
   position: absolute;
   display: block;
   float: left;
   left: 100%;
   top:-35px;
 }

@media screen and (max-width:600px)
{

 .topnav.responsive .bujar:hover .bujar-content
 {
   display: block;
   width: 100%;
   text-align: left;
   /* top: 100%;*/  
   left: 5em;  
 }


  .topnav.responsive .bujar-content 
 {
    display: none;
    width: 100%;
    text-align: left;
    /* top: 100%;*/ 
    left: 0;

  }

 }

In addition, I have implemented CSS adjustments for better positioning of the second nested dropdown.

  .bujar:hover .bujar-content.lower-me 
  {
     margin-top:8em;
  }

For the second nested dropdown div with the bujar-content class, you must also add the lower-me class.

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

I am looking for a way to distinguish between mandatory and non-mandatory input fields in React

I've encountered an issue with the borders of the text fields in my React project. Here's how I want the text fields to look: https://i.stack.imgur.com/MP6DG.png Currently, the borders appear straight in the browser even though I want them rou ...

Is there a CSS Blend Mode alternative for Internet Explorer?

I've implemented the background-blend-mode on this element: <a href="#" class="blend"> <h3>Title</h3> <p>Content goes here</p> </a> It's all set up with a background-image url. When the element with c ...

What is the best way to ensure that a list-group scrolls within a div instead of adjusting its height?

I have a div on my webpage that resembles a receipt. This div dynamically adds X items, with a full height of 95% for graphic design purposes. The problem arises when the div reaches 95% of the screen height and a new item is added, causing the entire page ...

How to utilize variables in Bootstrap 4?

After migrating from Bootstrap 3 to Bootstrap 4 and utilizing sass sources that I compile on the go, I encountered an issue with media switches. Specifically, for @media screen and (min-width: $screen-md-min) { padding: 40px 50px; } An error message ...

Selecting items in an HTML table using a drag-and-drop feature for selecting square or rectangular sections with colspan and rowspan

I am currently working on an implementation for selecting cells in a table. The challenge I am facing is that the cells in question can have a colspan or rowspan, which means that the selection is not restricted to a square or rectangular shape. For exampl ...

What is the best way to divide MySQL content (text and HTML) into multiple columns?

Before diving in, I want to acknowledge the hesitation around asking questions without first trying some trial and error. However, in this case, the need for specific knowledge is essential, so I am going to proceed with my query. I have been grappling wi ...

The CSS file stubbornly refuses to connect, opting instead to redirect back to the HTML file

Something strange has occurred. I was working on a React app and everything was functioning properly until today when I returned to it, and my CSS file wasn't linking for some reason. The only actions I took were running npm run build and npm run depl ...

Instructions on triggering a pop-up modal from a separate HTML document to the index page

I am trying to open a form in a Modal using a button. However, the Modal is located in a separate .html file. How can I use JavaScript to call the form from that external html file into my index.html file? Currently, I am able to call the Modal within the ...

What steps can I take to ensure that the full tingle modal, which includes an image, is visible when the

Upon loading the page, I noticed that if the browser width is greater than 540px, the modal displaying an image gets cut off (see figure below). What steps should I take to ensure that the vertical scroll bar appears immediately? https://i.sstatic.net/cJv ...

Error encountered on Facebook Like button: The large button is failing to show the total number of likes received

Despite functioning flawlessly for months, the large Facebook Like button has suddenly stopped showing the number of "Likes". Strangely, the compact version is still working fine, but the larger button is concealing the count. I am using a Mac and have obs ...

Adjusting the size of a bug while hovering over a specific div

Having recently started learning HTML & CSS, I have a question regarding creating a box that scales from 1.0 to 0.8 when hovered over with the mouse. The scaling works perfectly when hovering in the middle (0.0 to 0.8) of the box. However, when hover ...

Is `html.fa-events-icons-ready` causing clutter and disrupting the layout of the body?

I am seeking some clarification on my code and the resulting issues it is causing. Check out this image for reference: https://i.stack.imgur.com/jBQYd.png Why is the body height not taking up 100% of the space? The background image appears to be affect ...

Place div element on top

In my table cell, I have a link that reveals a hidden div when clicked. At the moment, I am using position: absolute and z-index:10 for this setup. It functions properly, but I want to adjust it slightly towards the top and left. However, when I include t ...

CSS @page does not render content within @top-center, @bottom-right, and similar positions

Currently, I am working on generating a HTML file that is print-ready and have been using CSS @page for this purpose. However, I have encountered a major issue with displaying page numbers utilizing @bottom-right and other similar solutions. Could there be ...

What is the best way to navigate to the top of a form panel?

I'm currently developing a Sencha Touch mobile app. I have a form panel with multiple fields, so I made it scrollable. However, every time I open the screen (panel), scroll down, navigate to other screens, and then return to the form panel, it stays s ...

How to set a canvas as the background of a div element

Check out my code snippet below: <!DOCTYPE html> <html> <body> <div id='rect' style='width:200px;height:200px;border:1px solid red'> </div> <canvas id="myCanvas" style="borde ...

The text color of the tabs turns black when a tab is selected - I receive my results after clicking anywhere on the screen

I am currently using HTML, CSS, and Bootstrap to design some tabs. Here is the current result I am achieving: https://i.sstatic.net/qbUck.png When clicking anywhere on the screen, I see the original results based on my code. The outcome after clicking a ...

Achieve a CSS design similar to the Cinemax logo with angled background ends

Is there a way to customize the angle at which the ends of a background are cut off when using -webkit-transform: rotate(-#deg); and background selectors for tilted text with a background? I'm looking to achieve a design similar to the Cinemax logo. ...

Is there a workaround utilizing calc() and vh specifically for Chrome browsers?

Is it possible to find a CSS-only solution for incorporating vh in calc() functions specifically for Chrome browsers? I am trying to implement calc(100vh - 25px) as the top margin in order to make a 25px bar stick to the bottom of the page, but I am facin ...

Is there a way to properly position an element in line with the element directly above it using html, css, and

I need help with aligning two <input type="text"> boxes on my form. Currently, I have a dropdown button and one text input in one row, and another text input in the second row (refer to the screenshot below). How can I ensure that the "Choi ...