Mega Dropdown Menu using CSS

I am currently working on a dynamic mega drop-down menu using only HTML and CSS. The issue I'm facing is that when I select one of the list items (LIs), it expands in size at the expense of the other LIs.

I'm not sure where the problem lies, so I was hoping to get some suggestions on how to fix this.

HTML

<div id="wrapper">
  <div id="menu2">
    <ul id="menu2_ul"> 
      <li class="list"><a href="#">Home</a>

        <div class="sub">
          <div class="submenu">
            <ul class="submenu_left_menu">
              <li><a href="#">Home</a></li>
              <li><a href="#">Insurance</a></li>
              <li><a href="#">Loans</a></li>
              <li><a href="#">Mortgages</a></li>
              <li><a href="#">Accounts & Savings</a></li>
              <li><a href="#">Energy</a></li>
              <li><a href="#">Investments</a></li>
              <li><a href="#">Discounts</a></li>
            </ul>
          </div>

          <div class="submenu_content">
            <h2>Lorem ipsum</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
          </div>

          <div class="submenu_poll">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
          </div>
        </div>
      </li>
      <li class="list"><a href="#">Insurance</a>

        <div class="sub">
          <div class="submenu">
            <ul class="submenu_left_menu">
              <li><a href="#">Home</a></li>
              <li><a href="#">Insurance</a></li>
              <li><a href="#">Loans</a></li>
              <li><a href="#">Mortgages</a></li>
              <li><a href="#">Accounts & Savings</a></li>
              <li><a href="#">Energy</a></li>
              <li><a href="#">Investments</a></li>
              <li><a href="#">Discounts</a></li>
            </ul>
          </div>
          <div class="submenu_content">
          </div>

          <div class="submenu_poll">
          </div>
        </div>
      </li>
    </div>
  </div>

CSS

body {
  background-color: black;
  padding: 0px;
  margin: 0px;        
}

#wrapper {
  margin: 0px;
  padding: 0px;
  height: 40px;         
  background-color: white;
}

#menu2 {
  width: 981px;
  margin: 0 auto;
  padding: 0px;      
}

#menu2 ul {
  list-style: none;
  display: table;
  margin: 0px;
  padding: 0px;
  height: 40px;
}

#menu2 li.list {
  display: table-cell;
  line-height: 40px;
  border-left: 1px solid grey;
  position: relative;
}          

#menu2 div.sub {
  margin:0;
  display:none;

  background-color: white;
}

#menu2 li.list:hover div.sub {
  display: block !important;
  width: 981px !important;
}

/******************************************************************************\
\******************************************************************************/

#menu2 div.sub .submenu {
  float: left;
  width: 220px;                        
}

#menu2 div.sub .submenu_content {
  width: 550px;
  float: left;
}

#menu2 div.sub .submenu_poll {
}

Answer №1

1. Change div.sub to absolute positioning

When using absolute positioning, each div will not start at the same position by default. To solve this issue, manually adjust the margin-left for each div. This can be done by assigning unique IDs to each div or utilizing CSS3 selectors like

#menu2:nth-child(1) {margin-left: -...}
.

Drawback: Requires manual ID assignment and margin adjustment for each div.sub, potentially necessitating uniform width for menu tabs.

2. Opt for fixed positioning for div.sub

With fixed positioning, all divs will start at an identical position. However, be cautious of their dimensions surpassing other elements, as documents do not stretch for fixed position items.

Drawback: Fixed divs may not align properly if additional elements follow them in the document flow, especially if no other elements are larger than divs.sub.

3. Explore the CSS3 :target selector

The :target selector can alter the link specified in an anchor's href attribute. Check out an example here. Each div.sub needs an assigned ID for this approach, allowing direct placement within the content section instead of nesting for each menu item. Note that it lacks compatibility with IE8 and earlier versions.

Drawback: Not supported on IE8 and older browsers.

4. Implement js/jQuery as an alternative

Using JavaScript or jQuery may be perceived as a simpler solution for some, but it comes with added weight.

Drawback: Adds more complexity and may not function in browsers with JavaScript disabled.

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

Revisiting the display of input placeholders

Enabling the placeholder property on an input field allows the text to disappear when something is written and reappear when everything is deleted. Here's the question: Is it feasible to delay the reappearance of the placeholder text until after the t ...

The CSS animation initially encounters a glitch before ultimately running smoothly as planned

I am currently working on a basic webpage that will showcase a list of disciplines. When a discipline is selected, relevant information will be displayed below. The code snippet available here demonstrates the functionality I am aiming for. However, I hav ...

What is the importance of ensuring that user registration is done consistently?

While working on a user registration feature, I noticed that the character restrictions for the 'username' field in some sample codes are quite limited, only allowing '.', ''', and '-'. No spaces or other specia ...

How to display percentage value on ReactJS Material UI progress bar

For displaying the progress completed in numbers, I utilize the Linear Determinate component. Take a look at the image below to see how it appears. ...

If a user types in Korean characters in the input field and then clicks away to any other part of the screen, the input field will automatically regain focus

jsfiddle instructions for testing: type Korean characters in the input field scroll down until the input field is no longer visible Click anywhere on the screen Expected result: the input field will come into focus with (Korean text) If the user enters K ...

What is the best way to position an image using css?

I am looking to position an image on the left side of my homepage using CSS instead of HTML. However, I am having trouble figuring out how to make it work. The image I want to use is called Nobullying.jpg. HTML: <html> <head> <link re ...

Instructions on removing an HTML element from a div that has the attribute contentEditable

Here is an example of HTML code: <div id="editable" contentEditable="true"> <span contentEditable="false">Text to remove</span> </div> I want to be able to delete the entire span element (along with its text) with just one bac ...

How come the background and border of the link are shown behind the image, while the link itself is displayed in front?

There seems to be an issue with the "Give Now" and "How You Help" links on the next page. The background color applied to them is showing up behind an image, making the link appear in front of it. Any suggestions on how to resolve this? ...

What is the best way to assign an HTML string to the DOM and connect it with an object?

In my angular HTML component template, I have the following markup: <div *ngIf="htmlTemplate && jsonObj" [innerHTML]="htmlTemplate"></div> The jsonObj variable is retrieved from an endpoint and looks like this: { fi ...

SQL Sorting Investigation

I am working with a SQL table that contains a column called reference_number. The values in the reference_number column follow the format of x-MMYY-xxxx. (MM represents the month and YY stands for year. -xxxx indicates auto-increment.) My current challen ...

Images in assets folder are not showing up in Vue Bootstrap

I'm attempting to showcase an image similar to the example provided in this guide Below is the code I am using: <template> <b-container fluid class="p-4 bg-dark"> <b-row> <b-col> <b-img rounded="ci ...

Is it possible to verify .0 with regular expressions?

In my project, I have a field that requires whole numbers only. To validate this, I used a regex validation /^\d{1,3}$/ which successfully validates whole number entry and rejects decimal points starting from .1. However, I encountered an issue where ...

Utilizing CSS to create a repeating background image within a specified container

I'm currently working on setting up a container with a background image that repeats only when the contained div elements are long enough. However, I seem to be encountering issues getting the image to repeat properly within the #container code. This ...

What is the best way to dynamically swap out an image within an element?

Is there a way to dynamically replace this image without deleting the HTML SVG element and re-rendering it? xlink:href="http://missosology.info/forum/download/file.php?avatar=11666_1307312313.jpg" CODE: https://jsfiddle.net/bfv17f0e/ <svg class="clip ...

Photo captured by camera is not stored in photo gallery

I am currently working on a basic image upload form that allows users to take photos using their phone camera and upload them. However, I have noticed that the pictures taken this way are not being saved to the gallery. Is there something missing in the H ...

What is the method for aligning inline-block text to the right after a line break?

I am currently encountering a challenge in developing a directory tree using <ul> element, particularly when the text of an item exceeds the width of the container. As I am utilizing the antd React library for this project, my options are somewhat l ...

Finding out the specific row that was clicked in a Jquery Mobile listview

I've searched everywhere and can't find a solution. How can I retrieve the value of a row tapped in a listview? This could be anything from the name to the index within the object. Currently, I have a function handling the tap event. I need to pa ...

Tips on increasing the height of an element that is overflowing

When populating my timeline component with dynamically mapped data from an array, I encountered an issue where if I added more data causing the maximum height to be reached, the overflow-y element didn't display all content. Despite trying various sol ...

When adjusting the zoom level, a fixed position element seems to shift up and down in relation to another element. Is there a way to stop this from happening?

I'm attempting to vertically center some text within a speech bubble, and I've achieved this using the position: absolute method with a specified top value on the text wrapper element. However, upon zooming in and out in the browser window, the t ...

Javascript file containing prohibited characters disrupting form functionality when linked with Jquery

Having some trouble with these files - HTML form, PHP processor, and JavaScript. The browser is showing errors like U+12AF for illegal characters in the JavaScript file. Spent 1.5 hours on this already and feeling frustrated. --submit.js-- $(docume ...