【问题标题】:Dropdown menu is inheriting the width of its parent and cropping its children if they were wider than the parent widths'下拉菜单继承其父级的宽度并裁剪其子级,如果它们比父级宽度更宽
【发布时间】:2015-04-19 02:35:02
【问题描述】:

我使用 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/

【问题讨论】:

    标签: html css drop-down-menu


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2013-08-10
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2015-03-25
      相关资源
      最近更新 更多