gpt4 book ai didi

html - 下拉菜单继承其父项的宽度,如果其子项比父项宽度宽,则对其进行裁剪

转载 作者:行者123 更新时间:2023-11-28 08:01:48 25 4
gpt4 key购买 nike

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

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

enter image description here

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

enter image description here

因此下拉菜单将拉伸(stretch)以适应父级的宽度,如果它们的任何子级比该宽度长,它们的宽度应该增加以适应。

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/

最佳答案

我已经从 #header .mainMenu ul ul 中删除了 position: absolute; 并且看到它工作正常...

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com