gpt4 book ai didi

html - 绝对定位的div不能根据内容自动调整

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:06 25 4
gpt4 key购买 nike

我正在使用悬停下拉菜单进行导航,一切正常,但问题在于下拉菜单,下拉菜单不会根据内容/文本增加。我希望下拉菜单根据其内容采用宽度。

.navigation {
width: 100%;
float: left;
background: #008271;
min-height: 50px;
font-family: segoe ui;
padding: 0 30px;
}
.navigation,
.navigation * {
box-sizing: border-box;
}
.navigation ul {
padding: 0;
margin: 0;
list-style: none;
}
.navigation .nav-right {
float: right;
}
.navigation > ul > li {
display: block;
float: left;
position: relative;
}
.navigation > ul > li > a {
display: block;
color: #fff;
height: 50px;
line-height: 50px;
text-align: center;
padding: 0 15px;
font-size: 15px;
}
.navigation > ul > li.drop-menu:hover > a,
.navigation > ul > li.drop-menu > a:hover {
background: #01675a;
}
.navigation .sub-menu {
display: none
}
.navigation > ul > li:hover .sub-menu {
width: 100%;
display: block;
position: absolute;
top: 50px;
background: #fff;
min-width: 160px;
right: 0;
border: 1px solid rgba(0, 0, 0, .15);
box-shadow: 0 6px 12px rgba(0, 0, 0, .17);
padding: 5px 0;
-webkit-background-clip: padding-box;
background-clip: padding-box;
}
.sub-menu > li > a {
display: block;
padding: 5px 15px;
color: #333;
white-space: nowrap;
font-size: 14px;
}
.sub-menu > li > a:hover {
background: #f5f5f5;
}
.caret {
display: inline-block;
width: 0;
height: 0;
margin-left: 2px;
vertical-align: middle;
border-top: 4px dashed;
border-top: 4px solid\9;
border-right: 4px solid transparent;
border-left: 4px solid transparent;
}
.nav-left > li > a {
font-size: 24px!important;
font-weight: 300;
}
<div class="navigation">
<ul class="nav-right">
<li class="drop-menu">
<a href="#">Teams <span class="caret"></span></a>
<ul class="sub-menu">
<li><a href="#">Team 1</a>
</li>
</ul>
</li>
<li class="drop-menu">
<a href="#">Communities <span class="caret"></span></a>
<ul class="sub-menu">
<li><a href="#">Communities 1</a>
</li>
<li><a href="#">Communities 2 and much more content</a>
</li>
</ul>
</li>
</ul>

</div>

最佳答案

我已将宽度更改为自动
.navigation > ul > li:hover .sub-menu {
宽度:自动;
}

.navigation {
width: 100%;
float: left;
background: #008271;
min-height: 50px;
font-family: segoe ui;
padding: 0 30px;
}
.navigation,
.navigation * {
box-sizing: border-box;
}
.navigation ul {
padding: 0;
margin: 0;
list-style: none;
}
.navigation .nav-right {
float: right;
}
.navigation > ul > li {
display: block;
float: left;
position: relative;
}
.navigation > ul > li > a {
display: block;
color: #fff;
height: 50px;
line-height: 50px;
text-align: center;
padding: 0 15px;
font-size: 15px;
}
.navigation > ul > li.drop-menu:hover > a,
.navigation > ul > li.drop-menu > a:hover {
background: #01675a;
}
.navigation .sub-menu {
display: none
}
.navigation > ul > li:hover .sub-menu {
width: auto;
display: block;
position: absolute;
top: 50px;
background: #fff;
min-width: 160px;
right: 0;
border: 1px solid rgba(0, 0, 0, .15);
box-shadow: 0 6px 12px rgba(0, 0, 0, .17);
padding: 5px 0;
-webkit-background-clip: padding-box;
background-clip: padding-box;
}
.sub-menu > li > a {
display: block;
padding: 5px 15px;
color: #333;
white-space: nowrap;
font-size: 14px;
}
.sub-menu > li > a:hover {
background: #f5f5f5;
}
.caret {
display: inline-block;
width: 0;
height: 0;
margin-left: 2px;
vertical-align: middle;
border-top: 4px dashed;
border-top: 4px solid\9;
border-right: 4px solid transparent;
border-left: 4px solid transparent;
}
.nav-left > li > a {
font-size: 24px!important;
font-weight: 300;
}
<div class="navigation">
<ul class="nav-right">
<li class="drop-menu">
<a href="#">Teams <span class="caret"></span></a>
<ul class="sub-menu">
<li><a href="#">Team 1</a>
</li>
</ul>
</li>
<li class="drop-menu">
<a href="#">Communities <span class="caret"></span></a>
<ul class="sub-menu">
<li><a href="#">Communities 1</a>
</li>
<li><a href="#">Communities 2 and much more content</a>
</li>
</ul>
</li>
</ul>

</div>

关于html - 绝对定位的div不能根据内容自动调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39328218/

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