我想保持下拉内容的宽度与父标题相同,但我没有这样做,因为与父标题相比,一些子标题的长度更长。我应该如何处理下拉菜单中较长的标题?
ul {
list-style-type: none;
margin: 0;
position: absolute;
}
li {
display: inline-block;
float: left;
margin-right: 1px;
}
li a {
display: block;
min-width: 100px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none;
}
li:hover a {
background: #97455f;
}
li:hover ul a {
background: grey;
color: #2f3036;
height: 40px;
line-height: 40px;
text-align: center;
}
li:hover ul a:hover {
background: #97455f;
color: #fff;
}
li ul {
display: none;
}
li ul li {
display: block;
float: none;
}
.hidden {
padding: 0;
}
ul li a:hover+.hidden,
.hidden:hover {
display: block;
width: auto;
}
@media screen and (max-width: 460px) {
ul {
position: static;
display: none;
}
li {
margin-bottom: 1px;
}
ul li,
li a {
width: 100%;
}
.show-menu {
display: block;
}
}
<ul class="menu">
<li><a href="#">Somthing</a></li>
<li> <a href="#">something</a>
<ul class="hidden">
<li><a href="#">somethoingaaaaaaaaaaaaaaaaaaaaa</a></li>
<li><a href="#">something</a></li>
</ul>
</li>
</ul>
最佳答案
您可以在多行设置中打断单词 word-wrap
至 break-word
.
ul {
list-style-type: none;
margin: 0;
position: absolute;
}
li {
display: inline-block;
float: left;
margin-right: 1px;
}
li a {
display: block;
width: 100px;
min-height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #fff;
background: #2f3036;
text-decoration: none;
word-wrap: break-word;
}
li:hover a {
background: #97455f;
}
li:hover ul a {
background: grey;
color: #2f3036;
min-height: 40px;
line-height: 40px;
text-align: center;
}
li:hover ul a:hover {
background: #97455f;
color: #fff;
}
li ul {
display: none;
}
li ul li {
display: block;
float: none;
}
.hidden {
padding: 0;
}
ul li a:hover+.hidden,
.hidden:hover {
display: block;
width: auto;
}
@media screen and (max-width: 460px) {
ul {
position: static;
display: none;
}
li {
margin-bottom: 1px;
}
ul li,
li a {
width: 100%;
}
.show-menu {
display: block;
}
}
<ul class="menu">
<li><a href="#">Somthing</a></li>
<li> <a href="#">something</a>
<ul class="hidden">
<li><a class href="#">somethoingaaaaaaaaaaaaaaaaaaaaa</a></li>
<li><a class href="#">something</a></li>
</ul>
</li>
</ul>
关于html - 如何保持下拉列表中子标题的宽度与父标题相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45625059/