简体   繁体   中英

Adapted width in CSS dropdown menu

I'm making a dropdown menu with only CSS, and it's not turning out easy the way I've done it. So far I've got an actual dropdown, but the width is the width of the parent element, which is too small for certain items to be displayed in one line.

菜单宽度太短

I tried setting a manual width, but that just unaligns the whole thing and isn't pratical as the menu item could be much longer. Is there anyway of having a width that adapts to the content, without changing the parent width ?

All the site files are located here : http://dev.cuonic.com/bourree/

Any help is appreciated, thanks :)

Playing around in firefox/firebug I found that this combination seemed to produce the desired effect:

#menu ul ul li {
    display: block;
    float: left;
    left: -34px;
    position: relative;
    width: 200px;
}

在此处输入图片说明

Here's a solution that doesn't use a fixed-width for the drop-downs.

First, add the following to the CSS for the links in the drop-downs:

#menu ul ul li a {
    white-space: nowrap;
}

I also had to change #menu ul and #menu ul li to #menu > ul and #menu > ul > li , respectively, so that those CSS styles would apply only to the first level menu items.

Here's a basic reference about the use of > in CSS selectors. I think there are other spots in this example where it would help:

http://reference.sitepoint.com/css/childselector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM