What is causing the HTML sub menu to appear in the incorrect position?

I'm working on an HTML and CSS menu.

I'm trying to center the sub-menu relative to the main menu (ul.menu).

Why is there extra space appearing on the left side?

The styling for ul.menu li:hover ul already sets left: 0, and its closest parent is ul.menu. I can't figure out where this extra space is coming from.

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

.menu-main-container { padding-bottom:52px; margin-top:-60px; margin:10px auto; }
ul.menu { z-index: 597; text-align:center; position: relative;background: gray; width: 960px; margin:auto!important;}
ul.menu li { display: inline-block; line-height: 2.1em; vertical-align: middle; zoom: 1; }
ul.menu a { display:block;  letter-spacing:2px; color:#333; font-size:13px; text-decoration: none; padding:0 35px; text-transform:uppercase; font-weight:normal !important;}
ul.menu a:hover {background:#EFEFEF; color:#ad7f12}
ul.menu, ul.menu li, ul.menu ul { list-style: none; margin: 0; padding: 0; color:#333; }

ul.menu li:hover { position: relative; z-index:599; text-decoration:none; background:#EFEFEF; }
ul.menu li:hover ul {}
ul.menu ul { display: none; }
ul.menu ul li { float: none; line-height:34px; margin:0; padding:0; display: inline-block;}
ul.menu ul li a:hover{ }
ul.menu ul li { top: -2px; left: 100%; }
ul.menu li:hover ul { display:inline-block; position: absolute; top: 100%; left: 0; z-index: 598; width:1020px; background:red; }
<div class="" ="menu-main-container">

<ul class="menu">
<li><a href="#">Menu 1</a>
 <ul>
 <li><a href="#">Menu 1 Submenu item 1</a></li>
 <li><a href="#">Menu 1 Submenu item 2</a></li>
 <li><a href="#">Menu 1 Submenu item 3</a></li>
 </ul>
</li>

<li><a href="#">Menu 2</a>
 <ul>
 <li><a href="#">Menu 2 Submenu item 1</a></li>
 <li><a href="#">Menu 2 Submenu item 2</a></li>
 <li><a href="#">Menu 2 Submenu item 3</a></li>
 </ul>
</li>

<li><a href="#">Menu 3</a>
 <ul>
 <li><a href="#">Menu 3 Submenu item 1</a></li>
 <li><a href="#">Menu 3 Submenu item 2</a></li>
 <li><a href="#">Menu 3 Submenu item 3</a></li>
 </ul>
</li> 
</ul>

</div>

Answer №1

The reason it begins at that empty space is because it is aligning based on its parent element. To fix this, delete the following:

position: relative;

Answer №2

The closest ancestor is actually ul.menu li, not ul.menu, which explains why it's not aligned in the center. Its position is being determined by ul.menu li. Simply altering the parent element won't resolve this issue; you'll need to employ an alternative approach along with CSS for it to be effective. Personally, I believe that jQuery or Javascript would offer the most suitable solutions in this scenario.

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

Center JQuery within the current screen

My implementation of Jquery dialog is as follows: <body> <div id="comments_dialog"> Insert a comment <form> <input type="text" id="search" name="q"> </form> </div> .... </body> dialog = $( "#com ...

How to Verify if a WebControl Contains Server-Side Blocks in ASP.NET

I am looking for a way to identify if a Web control contains server blocks in code, without necessarily parsing the entire file or scanning for specific tags. The reason for this requirement is because I have numerous old Web forms that were created witho ...

What could be causing the mousedown event to go unrecognized in JavaScript when using THREE?

I have a basic THREE.js setup and I am trying to click on a spherical object, however, nothing happens when I click anywhere in the browser. I attempted to follow the advice given in this post but it did not work. The full code is provided below: <!DO ...

Optimizing VueJS applications with browser caching features for faster loading

I've encountered an issue with my VueJS app. After running npm run build, a new set of dist/* files are generated. However, when I upload them to the server (replacing the old build) and try to access the page in the browser, it seems to be loading th ...

Steps for adjusting an image in a Bootstrap Carousel

Currently tackling a simple bootstrap Carousel for a school project. I've adjusted the max-height of the images to 400px but it seems like only the top part of the images is visible. I attempted to adjust the margins on the image, and even thought abo ...

Learn how to utilize the getElementByClassName method in JavaScript to insert checkboxes into two lists that share the same class name

I have a situation where I have two lists sharing the same class name. I need to dynamically create checkboxes in both of these lists based on XML data. My current dilemma lies in how to properly utilize getElementByClassName in JavaScript to add checkbox ...

Vue: Implement out-in transition where the incoming element appears before the outgoing element has completely disappeared

Check out my code on Codepen: here In this scenario, I have set up two div elements: Block 1 and Block 2. The functionality I am looking for is when a button is clicked, Block 1 smoothly translates to the left until it goes out of view. Once that happens ...

Steps for applying background color to a chosen element

Using a specific template, I have listed topics in the following way: //Template used for topic list display %li.topic{:topic_slug => "<%=topic.slug%>", :topic_name =>"<%=topic.text%>"} %a{href: "#!/topics/<%=topic.slug%>" } <%= ...

Is it possible to employ flexbox CSS to stack four tables on top of each other, allowing for resizing up to the maximum height of the window?

After experimenting with various setups, I have yet to find a complete solution. My objective is to have 4 tables stacked on top of each other, each starting just below 1/4 of the browser height. I want the ability to resize each table vertically, making t ...

What is the best method for creating a vertical line separator with a hover effect in CSS?

Facing an issue with creating a vertical line separator in the navigation menu. While trying to add a vertical line separator to my navigation, I noticed that it does not get covered by the hover effect when hovering over the menu items. Please refer to t ...

Ways to resolve the issue with TypeError: CSS2Properties lacking an index property setter for '0'

I'm currently working on application development using ReactJs and Material UI. When I try to open the application in Mozilla Firefox, an error message pops up saying "TypeError: CSS2Properties doesn't have an indexed property setter for ' ...

What is the method for creating five columns in a single row using Bootstrap?

How can I make my columns responsive for all screen sizes? Currently, I have set the width to 20% for each column, but I would like it to adjust accordingly. Here is a link to the fiddle. Any help on making the row responsive with five columns would be gre ...

css Issue with the "left" property

Is it possible to center the left side (left border) of a child div within its parent div? I tried using left: 50% in the CSS for the child div, but it doesn't seem to be working. Can anyone explain why? <div id="outher"> <div id="inner ...

Change background according to URL query

My goal is to dynamically change background images based on a URL parameter, specifically the destination code. With numerous background images available, it's crucial to display the correct one depending on the URL string. For instance, if the URL re ...

step-by-step guide to disabling grayscale mode automatically

In recent times, a trend has emerged in Thailand where many websites are appearing in grayscale. This is achieved through the use of -webkit-filter: grayscale(100%), filter: grayscale(100%) and similar filters. Although it is possible to revert these webs ...

CSS: Struggling to Keep Footer at the Bottom of the Page

I've been struggling to get my footer to stick to the bottom of the page, but I just can't seem to make it work. I've searched online for solutions with no success, so I thought I would ask for help here. http://jsfiddle.net/f54eq3w8/ Here ...

What could be causing this table to exceed its dimensions by 2 pixels?

Why is the height of this table 102px instead of 100px? Is there another CSS attribute that needs to be set besides: table { border-spacing:0; border-collapse:collapse; } For example, check out this example link. ...

What could be the reason for the absence of this Javascript function in my attribute?

I have been working on an app using electron, and I have a function that successfully adds tabs to the app. The issue arises when I try to add tabs via JavaScript with the onclick attribute - they show up as expected but do not execute the code to hide and ...

Unable to properly zoom in on an image within an iframe in Internet Explorer and Google Chrome

My image zoom functionality works perfectly on all browsers except for IE and Google Chrome when opened inside an iframe. Strangely, it still functions flawlessly in Firefox. How can I resolve this frustrating issue? The image link was sourced from the i ...

What is the method for adjusting the font size of the label in an Angular mat-checkbox element?

I've been trying to adjust the font size of a mat-checkbox's label, but no matter what I do, the component's default styles keep overriding my changes: Using !important Trying ::ng-deep Applying a global style in styles.scss <mat-checkb ...