我有一个悬停菜单,需要定位某些元素,但我无法将文本扩展到最大宽度。它只是将自己设置为最小宽度。
我将max-width设置为一个非常大的例子,但你可以看到它在Pages子菜单下被忽略了。
好像我删除li
上的位置相对,文字换行,但这会影响我的菜单。
ul {
padding-left: 0;
}
.toggle {
font-weight: bold;
cursor: pointer;
}
#shop-navigation .sub-menu,
#shop-navigation,
#shop-navigation [type="checkbox"]:checked~.sub-menu-toggle span,
.hidden-xs-up {
display: none;
}
#shop-navigation .menu li {
position: relative;
}
#shop-navigation a {
display: block;
padding: 0.5rem 4rem 0.5rem 15px;
margin: 0 -15px;
}
#shop-navigation .menu .sub-menu-toggle {
padding: 0.5rem;
width: 3rem;
position: absolute;
right: 0;
top: 0;
border-left: 1px solid rgba(0, 0, 0, 0.1);
text-align: center;
height: 100%;
}
#shop-navigation .sub-menu a {
background: #e8e8e8;
padding-left: 30px;
}
#shop-navigation .sub-menu ul a {
padding-left: 45px;
}
#shop-navigation [type="checkbox"]:checked~.sub-menu {
display: block;
}
#shop-navigation [type="checkbox"]:checked~.sub-menu-toggle:before {
content: "-";
}
@media (min-width: 768px) {
#shop-navigation a {
margin-left: -30px;
margin-right: -30px;
}
#shop-navigation a {
padding-left: 30px;
padding-right: 4rem;
}
#shop-navigation .menu .sub-menu-toggle {
width: 4rem;
}
#shop-navigation .sub-menu a {
padding-left: 45px;
}
#shop-navigation .sub-menu ul a {
padding-left: 60px;
}
}
#shop-navigation .sub-menu-toggle,
#shop-navigation [type="checkbox"]:checked~.sub-menu {
display: none;
}
#shop-navigation {
position: relative;
width: 100%;
min-height: 1px;
padding-right: 30px;
padding-left: 30px;
}
#shop-navigation {
-webkit-box-flex: 0;
-webkit-flex: 0 0 100%;
-ms-flex: 0 0 100%;
flex: 0 0 100%;
max-width: 100%;
}
#shop-navigation li {
display: inline-block;
}
#shop-navigation {
display: block;
text-align: center;
padding-top: 1.5rem;
z-index: 2020;
}
#shop-navigation .menu {
margin-bottom: 0.5rem;
}
#shop-navigation li {
margin: 0 0.5rem 1rem;
}
#shop-navigation a {
padding: 0;
margin: 0;
}
#shop-navigation .menu-item-has-children>a:first-child::after {
content: " \f107";
color: #4e9197;
}
#shop-navigation .sub-menu .menu-item-has-children>a:first-child::after {
content: " \f105";
position: absolute;
right: 1rem;
line-height: 1.5;
}
#shop-navigation .sub-menu .sub-menu .menu-item-has-children>a:first-child::after {
content: "";
}
#shop-navigation .menu-item-has-children:hover>.sub-menu {
display: block;
position: absolute;
z-index: 2030;
}
#shop-navigation .sub-menu {
background: #ffffff;
border: 1px solid rgba(0, 0, 0, 0.15);
-webkit-background-clip: padding-box;
background-clip: padding-box;
-webkit-box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.175);
box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.175);
cursor: pointer;
border-radius: 0.25rem;
}
#shop-navigation .sub-menu .sub-menu {
left: 100%;
top: -1px;
margin: -0.25rem 0 0 -0.25rem;
}
#shop-navigation .sub-menu>li:first-child>.sub-menu {
margin-top: 0;
}
#shop-navigation .sub-menu li {
background: #ffffff;
min-width: 10rem;
max-width: 200rem;
margin: 0 0 1px;
text-align: left;
display: block;
border-radius: 0.25rem;
}
#shop-navigation .sub-menu a {
background: transparent;
padding: 0.25rem 1rem;
}
#shop-navigation .sub-menu li:first-child>a {
padding-top: 0.5rem;
}
#shop-navigation .sub-menu li:last-child>a {
padding-bottom: 0.5rem;
}
#shop-navigation .sub-menu .menu-item-has-children a {
padding-right: 2.25rem;
padding-left: 1rem;
position: relative;
}
<nav id="shop-navigation" class="main-navigation text-nohyphen" role="navigation">
<ul id="shop-menu" class="menu">
<li id="menu-item-2131" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home menu-item-2131 menu-item-home"><a href="#">Home</a></li>
<li id="menu-item-1051" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1051 menu-item-depth"><a href="#">Depth</a><input id="expand-1051" class="hidden-xs-up" type="checkbox"><a class="toggle sub-menu-toggle" href="#" data-for="expand-1051" aria-label="Expand sub-menu"><span>+</span></a>
<ul class="sub-menu">
<li id="menu-item-1052" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1052 menu-item-level-01"><a href="#">Level 01</a><input id="expand-1052" class="hidden-xs-up" type="checkbox"><a class="toggle sub-menu-toggle" href="#" data-for="expand-1052" aria-label="Expand sub-menu"><span>+</span></a>
<ul class="sub-menu">
<li id="menu-item-1053" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1053 menu-item-level-02"><a href="#">Level 02</a><input id="expand-1053" class="hidden-xs-up" type="checkbox"><a class="toggle sub-menu-toggle" href="#" data-for="expand-1053" aria-label="Expand sub-menu"><span>+</span></a></li>
</ul>
</li>
<li id="menu-item-1937" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1937 menu-item-level-01a"><a href="#">Level 01a</a><input id="expand-1937" class="hidden-xs-up" type="checkbox"><a class="toggle sub-menu-toggle" href="#" data-for="expand-1937" aria-label="Expand sub-menu"><span>+</span></a>
<ul class="sub-menu">
<li id="menu-item-1938" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1938 menu-item-level-02"><a href="#">Level 02</a></li>
<li id="menu-item-1940" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1940 menu-item-level-02a"><a href="#">Level 02a</a></li>
</ul>
</li>
<li id="menu-item-1939" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1939 menu-item-level-01b"><a href="#">Level 01b</a></li>
</ul>
</li>
<li id="menu-item-1046" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-1046 menu-item-pages"><a href="#">Pages</a><input id="expand-1046" class="hidden-xs-up" type="checkbox"><a class="toggle sub-menu-toggle" href="#" data-for="expand-1046" aria-label="Expand sub-menu"><span>+</span></a>
<ul class="sub-menu" style="right: auto;">
<li id="menu-item-1695" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1695 menu-item-page-image-alignment"><a href="#">Page Image Alignment</a></li>
<li id="menu-item-1693" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1693 menu-item-page-image-alignment"><a href="#">Page Image Alignment</a></li>
<li id="menu-item-1696" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1696 menu-item-page-markup-and-formatting"><a href="#">Page Markup And Formatting</a></li>
<li id="menu-item-1694" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-1694 menu-item-page-markup-and-formatting"><a href="#">Page Markup And Formatting</a></li>
</ul>
</li>
</ul>
</nav>
答案 0 :(得分:1)
您正在将max-width
应用于相对定位的元素(li
s)。
这些元素存在于绝对定位的容器(ul
s)中。
在CSS定位中,容器为已定位的子项设置边界,而不是width
,height
,max-width
或其他内容。定位元素被限制在其父元素的边界。
所以你的max-width: 200rem
被容器的宽度覆盖了。
这篇文章提供了更多细节,可能有助于进一步解释这个问题:
如果您只想将li
文字换行,请添加white-space: nowrap
:
#shop-navigation .sub-menu li {
background: #ffffff;
min-width: 10rem;
max-width: 200rem;
margin: 0 0 1px;
text-align: left;
display: block;
border-radius: 0.25rem;
white-space: nowrap; /* NEW */
}