html - 如何保持下拉列表中子标题的宽度与父标题相同?

标签 html css

我想保持下拉内容的宽度与父标题相同,但我没有这样做,因为与父标题相比,一些子标题的长度更长。我应该如何处理下拉菜单中较长的标题?

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-wrapbreak-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/

相关文章:

javascript - 尝试为 div 创建点击方法,在 IE6 中不起作用?

javascript - 如何使用 jquery 为 heapbox 应用 onChange 事件?

javascript - Web 应用程序不适用于 iPad,但适用于桌面浏览器

html - 使可滚动的 div 成为父容器的 100% 高度

css - 如何限制 Sharepoint 2010 WebPart 中 HtmlTable 中单元格的宽度?

html - 通过 gulp 将脚本的 src 从 Bower_components 更改为 CDN

css - 如何创建一个由栏分隔但第一项和最后一项没有栏的菜单?

html - 边距和图像问题

html - 列出与字体和 "content:'\value' 一起使用的图标/字形

html - 为什么我的标签与输入组不一致 - Bootstrap