我使用CSS display:表创建了主导航,如下所示:

下拉菜单的with不超过父级的宽度,这意味着如果下拉菜单中有一个长文本,则会被裁剪,如下图所示:



我如何使下拉菜单适应其孩子的宽度,而不是适应其父项的宽度,如下图所示:



因此,下拉菜单将拉伸以适合其父对象的宽度,如果其子对象的任何一个大于该宽度,则它们的宽度应增加以适应。

HTML:

<nav class="mainMenu">
    <ul>
        <li> <a <?php href( 'index.php');?>>Index</a>
        </li>
        <li> <a <?php href( 'modules.php');?>>Modules</a>
        </li>
        <li class="subMenu"> <a <?php href( 'page.php');?>>Dropdown</a>

            <ul>
                <li> <a <?php href( '');?>>Short Text</a>
                </li>
                <li> <a <?php href( '');?>>Dropdown Longer Text</a>
                </li>
            </ul>
        </li>
        <li class="subMenu"> <a <?php href( 'page.php');?>>Dropdown</a>

            <ul>
                <li> <a <?php href( '');?>>Sub menu item which is longer than its parent</a>
                </li>
                <li> <a <?php href( '');?>>Short Text</a>
                </li>
            </ul>
        </li>
    </ul>
</nav>


CSS:

/*Header*/
 #header {
    background-color: #fff;
    width: 100%;
    display: inline-block;
    position: relative;
    z-index: 10000;
}
/*Header > Content Container*/
 #header .content {
    width: 1024px;
    padding: 0 20px;
    margin: 0 auto;
    position: relative;
}
/*Header > Main Menu*/
 #header .mainMenu {
    display: table;
    float: right;
}
#header .mainMenu ul {
    display: table-row;
    position: relative;
}
#header .mainMenu ul li {
    display: table-cell;
    position: relative;
}
/*Header > Main Menu > Menu Links*/
 #header .mainMenu ul li a {
    padding: 10px 15px;
}
#header .mainMenu ul li a:hover {
    color: #2B508F;
}
#header .mainMenu ul li a.active, #header .mainMenu ul li a.active:hover {
    color: #fff;
    background-color: #2B508F;
    cursor: default !important;
}
/*Header > Main Menu > Dropdown Menus*/
 #header .mainMenu ul ul {
    background-color: #fff;
    padding: 1px;
    min-width: 100%;
    position: absolute;
    top: 100%;
    left: 0;
    text-align: left;
    overflow: hidden;
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    -webkit-transition: all .1s linear;
    -moz-transition: all .1s linear;
    -ms-transition: all .1s linear;
    -o-transition: all .1s linear;
    transition: all .1s linear;
}
#header .mainMenu ul li:hover > ul {
    pointer-events: auto;
    visibility: visible;
    opacity: 1;
}
#header .mainMenu ul ul li {
    width: 100%;
    display: block;
}
/*Header > Main Menu > Dropdown Menu Links*/
 #header .mainMenu ul ul li a {
    display: block;
}
#header .mainMenu ul ul li a:hover {
}
#header .mainMenu ul ul li a.active, #header .mainMenu ul ul li a.active:hover {
}
/*Header > Main Menu > Dropdown Menu Parent Link*/
 #header .mainMenu .subMenu:hover > a {
    color: #2B508F;
}
#header .mainMenu .subMenu:hover > a.active {
    color: #2B508F;
}


JSFiddle:http://jsfiddle.net/t2vp03ox/

最佳答案

我已经从position: absolute;中删除​​了#header .mainMenu ul ul,发现它工作正常...

关于html - 下拉菜单继承了其父级的宽度,如果子级的宽度大于父级的宽度,则会裁剪其子级,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29720975/

10-12 12:59
查看更多