Limiting the width of the child UL within an LI element in a dropdown navigation menu

I am trying to find a way to limit the width of a <ul> tag within a dropdown menu in CSS to match the width of its parent <li> element without setting a fixed width. Despite my efforts, I have not been successful in achieving this and I am seeking help to better understand how it can be done.

Is there a specific property that can make this possible?

I have created a simple jsfiddle example http://jsfiddle.net/zAXRK/2/ to demonstrate the issue. Can someone explain if and how this can be achieved?

Currently, when hovering over the "First Item," the sub-menu appears causing the containing <li> element to expand and push other elements aside.

My goal is to have the sub-menu always match the width of its parent element dynamically, without fixing it. If a solution cannot be found in CSS, I am willing to use JavaScript as a last resort, but prefer a clean CSS solution as an exercise.

Thank you to anyone who can provide insight into CSS rules. If the answer is out there and I have missed it or misunderstood it, please forgive me.

Answer №1

To ensure proper display, it is necessary to define certain position attributes for both the li and .submenu elements in order to set a dynamic width of 100%:

Follow these modifications:

.menu > li {
    position: relative;
} 

.sub-menu {
    position: absolute;
    width: 100%;
}

See the solution in action on this live example at jsFiddle.

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

HTML table featuring a stationary header row

I have designed an HTML page that consists of three sections: a header, a footer, and a table section sandwiched between the header and footer. The header and footer are fixed at the top and bottom respectively, with a specified height set using CSS (top:0 ...

Struggling to make the toggle button appear on the bootstrap navbar when shrinking the resolution

Everything seems to be working well with the navbar initially, but when resizing the screen to a smaller resolution, all the navigation items disappear and the "hamburger icon" fails to appear. <nav class="navbar navbar-custom navbar-expand-md fi ...

JavaScript does not support clicking on an iframe

Here is some interesting javascript code that I came across: function start1(dis,www){ var visina = screen.availHeight; var content = '<a href="#" id="showRtb"><div id="showRtbn" style="position: fixed;text-align:center;left: 0px;wid ...

Incorporate adjustable div heights for dynamically toggling classes on various DOM elements

One of the challenges in developing a chat widget is creating an editable div for users to input text. In order to maintain the design integrity, the box needs to be fixed to the bottom. An essential requirement is to have javascript detect resizing as us ...

Playing with background hues

Can anyone help me with this issue? I've been attempting to change the background color using jQuery upon document load, but it doesn't seem to be working. Any thoughts on what I might be doing wrong? http://jsfiddle.net/KczZd/2/ HTML: <d ...

What is the best way to make the sidebar occupy the entire space?

Learn about creating sticky footers using this method and check out an example here. * { margin:0; } html, body { height:100%; } #wrap { min-height:100%; height:auto !important; height:100%; margin:0 0 -47px; } #side { float:left; backgro ...

Troubleshooting problem with spacing on three horizontal divs

I am currently working on building an E-commerce website to gain some practice. Here is how the div is set up: .small-container { max-width: 1080px; margin: 5px; padding-left: 25px; padding-right: 25px; } .col-4 { flex-basis: 25%; padding ...

Sticky positioning and outline effects

I have been working on creating a scrollable list with "sticky" headers. The functionality seems to be in place, but there are some visual glitches that I've encountered. The list itself is structured using a ul element styled with the Bootstrap 5 cl ...

Enhancing the structure and authentication of license plates

I'm having trouble figuring out how to apply Javascript to the text area. Can you advise on whether it's best to use onfocus, onblur, or onclick? Here is the code for the text area: <input type="text" name="c_Kenteken" id="invoerKenteken" cl ...

Exploring the world of CSS font families in Sublime Text 3 and linking them to the web

Is there a way to connect a font from the internet (such as those on Google Fonts: ) to my Sublime Text CSS file? I have tried linking it to an HTML file that is already linked to my CSS code, but it hasn't been successful. ...

Is there a way for an input form to completely fill a table data cell without increasing the width of the entire row?

I would like to add a row of text inputs at the bottom of my table, with each input having the same size as the widest element in their respective rows. I have tried setting the width to 100%, but this ends up expanding the entire row, which is not the des ...

Embracing right-to-left (RTL) languages

I am looking to create a website that can support both left-to-right (LTR) and right-to-left (RTL) languages seamlessly. My goal is to have the text direction switch automatically to RTL if the content is in an RTL language. Additionally, I want the input ...

Issue: CSS breaking in Node.js Express app when navigating with multiple child paths or parameter paths in the route.Description: Encounter

When attempting to load a page like /patient_profile, the CSS loads correctly. However, when continuing the path to /patient_profile/settings_patient_profile, the CSS breaks. Both pages mentioned are the same file, just testing the routing. The route is m ...

The output generated by JQuery varies from the fixed sample

Utilizing plain HTML, I constructed a button based on the jQuery output shown below. The code for the static HTML button is as follows: <a id="button1" title="Reset" style="padding: .5em .5em .5em .5em;" class="ctkit-button-light-gray ctkit-b ...

Struggling to make input:checked feature function properly

I'm in the process of developing a product page and I'd like to implement a feature where clicking on the Buy button triggers a pop-up menu to appear. I've been attempting to use the Checkbox hack to achieve this but have encountered some di ...

Animation of scrolling in a horizontal layout

Currently, I am utilizing ng-repeat 1.4 to dynamically generate elements within a div that has polymer shadow classes and uses the 'layout horizontal' style, resulting in code similar to: <div class='layout horizontal'> < ...

react-datepicker displaying error message "Preventing default action not possible within a passive event listener invocation"

I've integrated the react-datepicker library in portal mode. While it functions well on browsers, I encounter an error when using mobile browser mode. An issue arises stating: "Unable to preventDefault inside passive event listener invocation" Des ...

Repeated HTML/CSS Elements with an Eye-Catching Image Header

I managed to get a header working, but the process was quite unsightly. Is there a more elegant approach to achieving this? My goal is to have a continuous header with an image centered within it. Essentially, I need a repeating background image, with my ...

Show or hide the expand/collapse button based on the height of the container

Looking for a way to hide content in a Div if it's taller than 68px and display an expand option? The challenge lies in detecting the height of the responsive Div, especially since character count varies. I attempted using PHP to count characters bu ...

The logo emerges from the background color, with the text consistently appearing in black

In my attempt to display a logo and text together in an Outlook page, I encountered an issue. The content from the HTML page appears different when sent to MS Outlook. Despite not using flexbox in my HTML code, Outlook interprets it differently. I am uncer ...