Doug Fir
Doug Fir

Reputation: 21204

is it possible to make one element the same width as another withour js?

I'm building a navigation with has a drop down unordered list within an unordered list. I'd like to make the list items as wide as at least the "head" list item so that the drop down menu looks right. Just now the drop down menu is thinner as the line items have a lower character count than the line item that contains the unordered list.

I'd like to make somehting of the order:

ul li ul {
{width: *as wide as ul li*;
}

Is it possible to do this without javascript and just within css alone?

Upvotes: 2

Views: 1022

Answers (3)

0b10011
0b10011

Reputation: 18785

Yes, it is. Technically, the ul is already the same width, but it doesn't appear to be due to margins and padding. To change this, use this CSS:

ul li ul {
 width:100%;
 margin-left:0;
 margin-right:0;
 padding-left:0;
 padding-right:0;
}

You may also want to include the following CSS to change the padding/margin of the li child as well:

ul li ul > li {
 width:100%;
 margin-left:0;
 margin-right:0;
 padding-left:0;
 padding-right:0;
}

Update

In your specific example (posted in comments), the issue is you've set ul li ul's position value to absolute. This takes it out of it's parent (in regards to handling width), but can be fixed with the following CSS being added to your existing CSS:

ul li {
 position:relative;
}
ul li ul {
 width:100%;
}

While this allows you to set the submenu to be the same width as the parent, it comes with the side effect of making the menu items wrap (see sui generis menu). To overcome this, use the following CSS instead (in addition to your jsFiddle):

ul li {
 position:relative;
}
/* Due to 'min-width', this may not be compatible with all browsers */
ul li ul {
 min-width:100%;
 white-space:nowrap;
}

Upvotes: 3

Sven Bieder
Sven Bieder

Reputation: 5681

Try this.

ul li, ul li ul {
  width:100px;
}

Upvotes: 0

Robert
Robert

Reputation: 21

width:100%

that should make the child as wide as the parent :)

Upvotes: 0

Related Questions