简体   繁体   English

CSS下拉菜单中的调整宽度

[英]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. 我仅使用CSS制作了一个下拉菜单,但我做起来并不容易。 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/ 所有站点文件都位于这里: 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: 在firefox / firebug中玩耍时,我发现这种组合似乎产生了预期的效果:

#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: 首先,将以下内容添加到下拉列表中的链接的CSS:

#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. 我还必须将#menu ul#menu ul li更改为#menu > ul#menu > ul > li ,以便这些CSS样式仅适用于第一级菜单项。

Here's a basic reference about the use of > in CSS selectors. 这是有关在CSS选择器中使用>的基本参考。 I think there are other spots in this example where it would help: 我认为此示例中还有其他方面可以帮助您:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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