The drop-down menu is misaligned from its designated position underneath the title

Trying to incorporate a dropdown menu into my website, I am structuring the HTML as follows:

<ul class="topnav">

  <li>
       SECTION TITLE
       <ul class="subnav">
         <li>One</li>
         <li>Two</li>
       </ul>
  </li>

</ul>

The CSS code for this setup is as follows:

header ul.topnav li ul.subnav {
    position: absolute;
    left: 0; 
    top: 35px;
    background: blue;
    margin: 0;
    padding: 0;
    display: none;
    float: left;
    width: 16%;
    border: 1px solid black;
}

Although successful in getting the jQuery function to animate the submenu, there is an issue with its styling. The dropdown menu emerges at the far left of the screen instead of sliding down from beneath the SECTION TITLE. For further reference and visualization, visit my website: link here (the menu is positioned at the top; hover over elements to view the sub-menu).

Due to the use of position: absolute, the placement of the ul.subnav should be relative to its parent element, specifically the li enclosing SECTION TITLE. Unfortunately, it appears that the positioning of the ul.subnav is connected to the top left corner.

Answer №1

The issue arises from floating all topnavs li elements to the left, including unnecessary float left on children like sections with ids such as whithacking. This causes the topnav li's to be pulled far left. Removing these floats and widths is necessary. Once floats are removed, anchors will automatically adjust width.

ul.topnav > li{
position:relative;
float:left;
width:15%
}

After applying this, customize your styling accordingly.

Answer №2

the element is placed absolutely, in relation to the initial parent element with a position of: absolute, relative, or fixed. Implement this:

header ul.topnav > li {
    position: relative;
}

and it should function properly.

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

Trouble with Selenium Webdriver executing Custom Ribbon Button

I encountered an issue while trying to click on a Custom Ribbon MSCRM2011 Button in an automation script I'm writing. Below is the HTML structure: <li id="contact|NoRelationship|Form|Mscrm.Form.contact.MainTab.ExportData" class="ms-cui-group" unse ...

An unanticipated SyntaxError was encountered while attempting to utilize an Ajax post, specifically in relation

I can't seem to figure out how to troubleshoot an ajax/jquery error. Here is the function I'm working with: var LogIn = { Email: $("#Name").val(), MobileNo: $("#txtMobileNumber").val(), PinCode: '', ...

Is your CSS not reflecting changes in Node.js?

I am having trouble getting the CSS to update in the browser using Browser Sync via Gulp on Node.js. What could be the issue? Below is my Gulp.js file: var themename = 'playtest haha 1'; var gulp = require('gulp'), // Prepare ...

What's with the lack of acknowledgment when I triumph in rock, paper, scissors, lizard, spock?

My game is running smoothly, except for when lizard or spock are involved and I win. For some reason, the outcome does not display correctly, even though it works fine when I lose. I've double-checked for typos but couldn't find any. If someone c ...

Using TailwindCSS with Vite in a vanilla JavaScript project: A step-by-step guide

As a beginner, I feel like I'm losing my mind trying to figure this out. I've watched countless tutorials and read through numerous documents, but still can't seem to get it working. Does anyone have any advice on how to successfully integra ...

I am currently engaged in a Portfolio project, where my objective is to ensure that all images are optimized for responsiveness

While Bootstrap ensures that relative images are responsive, absolute images may not be. This can cause alignment issues when the browser window is resized on mobile devices. .title-img { position: relative; width: 100%; height: 100%; } .skill-b ...

Create a pop-up window within a single controller using Angular.js

I followed a tutorial to create this code. I am interested in learning how to utilize just one controller for generating the dialog box. Currently, I am using two controllers for this project. Any guidance or tips would be greatly appreciated. View my cod ...

Encountering an "undefined is not a function" error across all libraries

While working on ASP.Net MVC4, I have encountered an issue where I consistently receive the error message "undefined is not a function" when using jQuery functions with different libraries. Despite ensuring that every ID is correct and everything has bee ...

Refreshing the Dropdown Menu with Jquery

Looking for a way to create a reusable dropdown menu using css/jquery? Check out this codepen: https://codepen.io/anon/pen/NxXXPg Is there a method to reset the 'active' status when clicking on a blank space or another html element? Here's ...

How can I troubleshoot the unresponsive remove div function on my website?

My code is running fine on CodePen (link provided below), but for some reason, it's not working properly in the web browser. I am executing the code from localhost and the button isn't responding as expected. CODE Here is my Visual Studio code ...

Is it possible to trace the data flow in the console to see the destination when you submit a form on a website?

Can you track the destination of data when submitting a form on a website through the console? ...

receive a JSON response containing [object object]

Every time I run my page with JSON, I always get the response: [object object]. I have read in other posts that this is a problem with a non-existent field, but this error also appears too often $.each(result, function(i, row) { console.log(JSON.stri ...

Ineffectiveness of Less Guard feature

I have a mixin with a guard clause that I've implemented following the provided guidelines. According to the syntax below, @palette should be selected from a list of custom colors and @color should be assigned a value from a specific set. The curren ...

I'm attempting to use Bootstrap to align my divs, but unfortunately, it doesn't seem to be working as I had hoped

I've been attempting to align divs using bootstrap, but I'm encountering issues. I prefer not to use a table for this purpose and have experimented with the table tag, but would rather achieve alignment with divs and bootstrap. My familiarity ...

Chrome has a tendency to cut off page contents in print preview, unlike other browsers that do not have this issue

My current version of Chrome browser (54.0.2840.59) is exhibiting a strange behavior when attempting to print preview an HTML page I have developed. The print preview truncates the page content, displaying less than what should be shown. The content is not ...

Once a value is selected from the datalist input autocomplete, it loses focus

My issue involves using an input with a dynamic datalist for auto completion. To achieve this, I've implemented the following approach: A key function in my process is as follows: $(function () { $("#to").on("input", function (e) { ...

Incorporate a delay when using jQuery's mouseenter function

I'm trying to implement a tooltip that appears 5 seconds after the mouse enters. Here's the code I've been working with: $('thead').mouseenter( function() { var tooltip = $('<div id="tooltip" class="tooltip-container ...

When the flex-grow property is set to 1, the height of the div extends beyond the space that

Here is an example of some HTML code: .container { height: 100%; width: 100%; display: flex; flex-direction: column; padding: 10px; background-color: aqua; } .box { width: 100%; height: 100%; flex-grow: 1; background-color: cadetb ...

Submitting a Complex JSON Object through a URL with Javascript

I am attempting to send a complex JSON Object in a URL input = { "id": 1, "sd": "123", "filter": { "h": 1,"road": true }, "legs": [{ "id": 1, "x1": -0.001, "y1": 51.122 }, { "id": 2, "x1": -12, "y1": 12 }] }; I have experimented with these methods data ...

Prevent content overlap in Tumblr's sidebars by using CSS positioning techniques

I'm facing an issue with my blog on Tumblr where the sidebar keeps overlapping the main content. I have modified a premade template and am trying to position the sidebar in the top right corner using absolute or fixed positioning: #sidebar{ position: ...