gpt4 book ai didi

html - CSS - 绝对定位问题

转载 作者:行者123 更新时间:2023-11-28 03:27:54 24 4
gpt4 key购买 nike

enter image description here

我想了解为什么下拉菜单列表没有正确调整到导航项的左下边缘。

(我已经尝试将 left: 0px 添加到 .dropdown-menu 中,这对水平间隙工作正常,但我不知道如何处理垂直。)

.navbar {
padding-left: 0;
list-style-type: none;
background-color: #252525;
font-size: 0;
/* to remove white-space gap between child blocks */
}

.navitem {
font-size: 1.0rem;
/* root eM*/
margin: 0;
padding: 10px;
text-align: center;
display: inline-block;
color: white;
cursor: pointer;
}

.navitem:hover {
background-color: black;
}

.dropdown {
position: relative;
border: 1px solid red;
}

.dropdown-menu {
padding: 0;
/*left:0px;*/
position: absolute;
min-width: 100%;
text-align: left;
list-style-type: none;
background-color: #252525;
}
<ul class="navbar">
<li class="navitem link">Link1</li>
<li class="navitem link">Link2</li>
<li class="navitem link">Link3</li>
<li class="navitem dropdown">
<div>Dropdown</div>
<ul class="dropdown-menu">
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
</ul>

</li>
</ul>

最佳答案

添加 top: 100%; 这将使 dropdown-menudropdown 的底部开始。

并添加 right: 0;left:0; 使其从 dropdown 的任一侧开始。

查看代码片段:

.navbar {
padding-left: 0;
list-style-type: none;
background-color: #252525;
font-size: 0;
/* to remove white-space gap between child blocks */
}

.navitem {
font-size: 1.0rem;
/* root eM*/
margin: 0;
padding: 10px;
text-align: center;
display: inline-block;
color: white;
cursor: pointer;
}

.navitem:hover {
background-color: black;
}

.dropdown {
position: relative;
border: 1px solid red;
}

.dropdown-menu {
padding: 0;
/*left:0px;*/
position: absolute;
min-width: 100%;
text-align: left;
list-style-type: none;
background-color: #252525;
top: 100%;
right: 0;
}
<ul class="navbar">
<li class="navitem link">Link1</li>
<li class="navitem link">Link2</li>
<li class="navitem link">Link3</li>
<li class="navitem dropdown">
<div>Dropdown</div>
<ul class="dropdown-menu">
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
</ul>

</li>
</ul>

关于html - CSS - 绝对定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44887257/

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