我想保持下拉内容的宽度与父级相同,但我没有这样做,因为有些子标题的长度与父级相比更长。我应该怎样处理下拉菜单中较长的标题?
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>
&#13;
答案 0 :(得分:1)
您可以在多行中将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>