Make sure to inherit the width from the parent element

I am facing an issue with my main menu #navi, where the items have different widths but the submenus should match the width of their parent items. To illustrate this problem, I have created a JsBin: http://jsbin.com/yusunohage/1

The text A very very very long submenu should not exceed the width of its parent A longer menu.

Below is the HTML code snippet:

  <ul id="navi">
    <li class="menu1">Menu </li>
    <li class="menu2">A longer menu 
      <ul class="children">
        <li>A very very very long submenu</li>
        <li>Submenu</li>
      </ul>
    </li>
    <li class="menu3">Menu item</li>
  </ul>

And here is the corresponding CSS:

ol, ul {
  list-style: none;
  padding: 0;
}
#navi > li {
  padding: 0 10px;
}

li {
  float: left;
}
 ul.children{
   background: gold;


 }

#navi li ul li {
  float:none;
}

Answer №1

ISSUE:

  • Difficulty in constraining child elements within a parent element without directly setting the width using CSS.

RESOLUTION:

  • Initial step: Set the width of child elements to 0px to measure the natural width of the parent.
  • Next, adjust the width of child elements based on the measured parent width.

jQuery:

$('.children li').css('width', 0);
$('.children li').css('width', $('.menu2').width());

Find the complete solution here : http://jsfiddle.net/urahara/4m5vkkk2/2/

Answer №2

To ensure proper sizing, start by setting the height of parent elements before focusing on the width of child elements.

Simply include this snippet in your CSS:

.menu2{
max-width:140px;
}

For a live example, visit the Jsfiddle http://jsfiddle.net/nwL1hku4/

Revised Version

If you're unsure about the width of the parent element, consider assigning a max-width to the child elements to control their width.

Check out this updated version here http://jsfiddle.net/nwL1hku4/1/

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

divide a JSON list

Within this JSON array, there is a plethora of person data, each with their own specified type such as "hr" or "accounting". The structure of the JSON array is depicted below: { "0": { "id": "2", "name": "blabla", "type": "hr" ...

` How can I activate a dropdown menu based on the selection of another dropdown menu in SENCHA/ext js?`

I need to make a change in my Sencha application. I want to add a second combobox that starts off disabled, which is not an issue. However, I also need the first combobox to enable the second one when certain items (not all) are selected from the first com ...

Why does my HTML page keep triggering a redirect loop when embedded?

Trying to create an HTML document that auto-refreshes every 5 seconds while embedding a web page poses a challenge. Various attempts with embed tags, object tags, and iframe tags led to automatic redirection to the embedded page upon each refresh. The curr ...

Is there a way to keep a section of an animated class in place?

My website features an animated retractable menu bar with two separate menus. Clicking on one icon activates a sliding motion to reveal that particular menu while causing the second menu to retract in a similar fashion. To achieve a smooth transition effec ...

What causes the 'pegman' display to malfunction on Google Maps API v3 when using Internet Explorer?

If you visit my website, you'll notice that the StreetView control, "Pegman", functions perfectly on Firefox. However, when I tested it on IE (specifically 7 and 8), it caused issues with displaying the Pegman control. Below is the code I am using fo ...

Why are my buttons displaying uneven spacing?

I'm stumped trying to figure out what's causing the strange spacing issue you can see in this image https://i.sstatic.net/11Fj5.png (unfortunately, I can't embed images yet...sorry) https://i.sstatic.net/z0P5Q.png taken from . Here is the ...

Custom-designed background featuring unique styles

I have implemented the following code to create a continuous running banner: <style> #myimage { position: fixed; left: 0%; width: 100%; bottom: 0%; background:url("http://static.giga.de/wp-content/uploads/2014/08/tastatur-bild ...

FormView does not have a defined namespace prefix asp - How should I specify my Page Directive?

After creating a basic web page using Microsoft Expression Web 4 with an ASP Connection to a local Access DB, I encountered an error when navigating to the page from another webpage. The browser displayed the following errors: Error: Namespace p ...

The functionality of the CSS transform property seems to be malfunctioning

My HTML and CSS code displays texts in two blocks within a dl element. I attempted to change the ▶ symbol to a different shape using the css transform scale property. However, some browsers only show the triangle in the first column. Why is this happenin ...

How can you customize gutter widths for specific rows in Bootstrap 3?

Currently, I am constructing my own website using Bootstrap 3 and have come across a challenge that I would like to address: My goal is to assign distinctive gutter widths to certain rows in my design. The remaining sections of the site will adhere to th ...

The function insertAdjacentHTML does not seem to be functioning properly when used with a

I am working with a parent element that contains some child elements. I create a DocumentFragment and clone certain nodes, adding them to the fragment. Then, I attempt to insert the fragment into the DOM using the insertAdjacentHTML method. Here is an ex ...

Getting two <div> elements to float below one another can be achieved by utilizing the CSS property "float

Can someone please tell me the trick to floating one element below another on the same side? I want them both to float but be positioned under each other. Thank you in advance. ...

The document type is not compatible with the "div" element in this section. You must include one of the following start-tags: "object", "ins", "del", "map", or "button"

When I run W3C validation, it indicates an error on the line below: <div id='main' style='border:1px solid #999;'> This error appears in the following code snippet: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "h ...

Python: Fails to reach the second iteration in the For loop

I need Python to iterate through multiple pages by incrementing the page number in the URL. # Using get request on the site from bs4 import BeautifulSoup import requests from warnings import warn response1 = requests.get('https://lasvegas.craigslist ...

The functionality of Scriptaculous Ajax.InPlaceEditor is not compatible with HTML

Using the Scriptaculous Ajax.InPlaceEditor has been great for editing data from my database. However, I have encountered a strange issue. Initially, everything works perfectly when there is no data in the database. The source code in my browser looks like ...

Download a complete website using PHP or HTML and save it for offline access

Is there a way to create a website with a textbox and save button, where entering a link saves the entire website like the 'save page' feature in Google Chrome? Can this be done using PHP or HTML? And is it possible to zip the site before downloa ...

CSS has detected an invalid value for the property across all components. Please

Need help reverting the style of a child element back to default and receiving an error message saying "Invalid Property Value". HTML: <div class="dropdown locale-selector"> <button class="btn btn-secondary " type="button" id="dropdownMenuBut ...

What are the steps for editing and preserving a logo with an 8-bit indexed color palette?

After purchasing a RE/MAX franchise, I now need to make edits to the official logo found on remax.com using Adobe Photoshop. However, I have encountered a problem when trying to edit their logo in Photoshop. When I open their PNG logo, the layer appears d ...

Is the styling of the image tag's height different on the iPhone browser compared to other browsers?

I have encountered a strange issue while testing my website on various devices, all of which are using Chrome. Everything displays correctly except when viewed on an iPhone, where I notice an unexplained extra height in the img tag. Below is the HTML code ...

Tap and conceal feature on a mobile website

I seem to be facing a JavaScript challenge. On the desktop version of my website, I've managed to create a functionality where hovering over a button reveals some text, and clicking anywhere else on the site hides the text. However, I'm now stru ...