下拉菜单继承其父级的宽度并裁剪其子级(如果它们比父级宽度更宽)

时间:2015-04-18 18:11:04

标签: html css html5 css3 drop-down-menu

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

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

enter image description here

如何让我的下拉菜单适应他们孩子的宽度,而不是如下图所示的父母宽度:

enter image description here

所以下拉菜单会拉伸以适应父母的宽度,如果他们的孩子的长度超过那个宽度,他们的宽度应该增加以适应。

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/

1 个答案:

答案 0 :(得分:0)

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