html - 下拉菜单 <ul> 100% 宽度,内联子菜单项

标签 html css drop-down-menu

我发现这个 SO 讨论 ( Dropdown Menu - Make the <ul> submenu 100% width) 非常接近我正在寻找的内容,但子菜单项是堆叠的。我想让子菜单项内联,而不是堆叠。我试过在子菜单上 float 、内联 block ,但没有任何东西可以使它们内联。

最佳答案

在该示例中,子菜单项的最大宽度受容器宽度的限制。您可以使它们保持内联,如下所示:

.nav > ul > li > ul {
    ...
    white-space: nowrap;
}
.nav > ul > li > ul > li {
    ...
    display: inline-block;
}

Updated jsFiddle

/* not very relevant styling */
h1         { font-size: 20px; padding-top: 20px; }
.container { position: relative; margin: 20px auto 0 auto; width: 75%; }
.header    { background: #eee; }
.nav       { background: #ccc; }


/* relevant styling */

body { overflow-x: hidden; } /* trick from css-tricks comments */

/* FIRST LEVEL */

.nav > ul > li { 
    display: inline-block; 
    position: relative; 
    padding: 3px 10px 3px 0;
    z-index: 100;
}

/* SECOND LEVEL */

.nav > ul > li > ul {
    position: absolute;
    left: 0;
    top: 100%;
    padding: 0 1000em; /* trick from css-tricks comments */
    margin: 0 -1000em; /* trick from css-tricks comments */
    z-index: 101;
    visibility: hidden;
    opacity: 0;
    background: rgba(255, 240, 240, 0.8);
    white-space: nowrap; /*new*/
}

.nav > ul > li:hover > ul {
    visibility: visible;
    opacity: 1;
}

.nav > ul > li > ul > li {
    padding: 3px 0;
    display: inline-block; /*new*/
}
<div class="header">
    
    <div class="container">
        <h1>Hank's Big Leauge Widgets</h1>
        <span>You want a widget? we got 'em!</span>
    </div>
    
    <!-- NAVIGATION -->    
    <div class="nav">
        <ul class="container">
            <li>
                <a href="#">Products</a>
                <ul>
                    <li><a href="#">Widget A</a></li>
                    <li><a href="#">Widget B</a></li>
                    <li><a href="#">Widget C</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Locations</a>
                <ul>
                    <li><a href="#">Location A</a></li>
                    <li><a href="#">Location B</a></li>
                    <li><a href="#">Location C</a></li>
                </ul>
            </li>
            <li>
                <a href="#">Staff</a>
                <ul>
                    <li><a href="#">President</a></li>
                    <li><a href="#">VP</a></li>
                    <li><a href="#">Manager</a></li>
                </ul>
            </li>
        </ul>
    </div>
    
</div>

<div class="content container">
    <p>All sorts of content</p>
    <p>All sorts of content</p>
    <p>All sorts of content</p>
    <p>All sorts of content</p>
    <p>All sorts of content</p>
    <p>All sorts of content</p>
</div>

关于html - 下拉菜单 <ul> 100% 宽度,内联子菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40754032/

相关文章:

html - 使用显示 : table? 时如何使内部 DIV 填充到外部 DIV 的高度

html - 如何将下拉项添加到我的水平菜单栏?

jquery - 根据列表大小调整 Bootstrap 菜单多列的大小

html - 页脚重叠内容 - HTML 和 CSS

css - 基于设备方向和 CSS 的动画?

html - 如何链接到浏览器无法查看的文件进行下载?

html - 为什么 min-height 在我的 Safari 和 Firefox 中的表格上不起作用?

html - 与父导航宽度相同的下拉导航

jquery - 隐藏/淡入效果 - 跨浏览器?

javascript - Node server.js - 无法获取/