这可能是一个简单的解决方案,但我为此绞尽脑汁。我试图将鼠标悬停在打开另一个 ul 的 li 上,一旦我将鼠标悬停在提到的 ul 中的 li 上,背景就会占据 ul 背景的整个宽度。
ul {
margin: 0;
padding: 0;
list-style: none;
z-index: 1000;
}
li {
position: relative;
cursor: pointer;
}
ul>li {
color: #000000;
float: left;
padding: 10px;
height: 22px;
}
ul>li a {
color: #000000;
text-decoration: none;
display: block;
}
ul>li li {
float: left;
clear: left;
}
ul>li ul {
display: none;
left: 0;
top: 100%;
}
ul>li li>ul {
top: 0;
left: 100%;
}
li>ul {
position: absolute;
min-width: 100%;
display: none;
background: #f0f0f0;
list-style: none;
}
li:hover>ul,
li:focus>ul {
display: block;
}
li:hover {
background: #27B3CF;
outline: 1px dotted red;
}
<ul>
<li style="color: white;">Reports
<ul>
<li>Lvl 1</li>
<li><span class="title">Show a List</span>
<ul>
<li><a href="#">Details</a></li>
</ul>
</li>
<li>Lvl 1</li>
</ul>
</li>
</ul>
我试过使用 display: block;尽我所能,但出于某种原因,我无法使背景宽度跨越下拉菜单的宽度。我仍然是初学者,可以使用一些帮助来引导我朝着正确的方向前进。您看到我可以在哪里添加 CSS 来进行此调整吗?任何帮助将不胜感激。
最佳答案
通过为列表项指定固定宽度,背景颜色将显示在该宽度上,而不是仅显示文本和填充的大小。
这是您的代码段,在 ul>li
类中添加了以下内容:
宽度:50px;
ul {
margin: 0;
padding: 0;
list-style: none;
z-index: 1000;
}
li {
position: relative;
cursor: pointer;
}
ul>li {
color: #000000;
float: left;
padding: 10px;
height: 22px;
width: 50px;
}
ul>li a {
color: #000000;
text-decoration: none;
display: block;
}
ul>li li {
float: left;
clear: left;
}
ul>li ul {
display: none;
left: 0;
top: 100%;
}
ul>li li>ul {
top: 0;
left: 100%;
}
li>ul {
position: absolute;
min-width: 100%;
display: none;
background: #f0f0f0;
list-style: none;
}
li:hover>ul,
li:focus>ul {
display: block;
}
li:hover {
background: #27B3CF;
outline: 1px dotted red;
}
<ul>
<li >Reports
<ul>
<li>Lvl 1</li>
<li><span class="title">Show a List</span>
<ul>
<li><a href="#">Details</a></li>
</ul>
</li>
<li>Lvl 1</li>
</ul>
</li>
</ul>
关于html - 使 li 背景色跨越整个 ul 宽度 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44448030/