按内容下拉菜单宽度

时间:2017-10-30 09:13:51

标签: html css

我有一个下拉菜单,我想要更改,下拉菜单宽度将按内容。我想更改,该下拉列表<li>将仅位于主<li>旁边的一行中。

我在这里有jsfiddle,我使用的是sass。 http://jsfiddle.net/hhkrp71f/3/

这是我的解决方案:

.header__links {
  float: right;
  margin-top: 40px;
}
.header__links nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.header__links nav ul li {
  font-weight: 100;
  font-style: italic;
  color: red;
  float: left;
  margin-left: 25px;
}
.header__links nav ul li a {
  color: red;
  text-decoration: none;
}
.header__links nav ul li a:hover {
  color: red;
  transition: color 0.7s ease;
}
.header__links nav ul li:hover ul {
  visibility: visible;
  opacity: 1;
  filter: alpha(opacity=100);
}
.header__links nav ul li ul {
  visibility: hidden;
  opacity: 0;
  filter: alpha(opacity=0);
  -webkit-transition: 500ms ease;
  -moz-transition: 500ms ease;
  -o-transition: 500ms ease;
  transition: 500ms ease;
  position: absolute;
  margin: 0;
  padding: 20px 0 0;
}
.header__links nav ul li ul li {
  margin: 0;
  padding: 10px;
  background-color: blue;
}
<div class="header__links">
  <nav>
    <ul>
      <li><a href="#">Test</a>
        <ul>
          <li><a href="#">Test Test Test</a></li>
          <li><a href="#">Test Test</a></li>
          <li><a href="#">Test Test Test</a></li>
          <li><a href="#">Test Test</a></li>
        </ul>
      </li>
      <li><a href="#">Test</a></li>
      <li><a href="#">Test</a></li>
      <li><a href="#">Test</a></li>
    </ul>
  </nav>
</div>

1 个答案:

答案 0 :(得分:0)

right: 0添加到内部ul

.header__links {
  float: right;
  margin-top: 40px;
}
.header__links nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.header__links nav ul li {
  font-weight: 100;
  font-style: italic;
  color: red;
  float: left;
  margin-left: 25px;
}
.header__links nav ul li a {
  color: red;
  text-decoration: none;
}
.header__links nav ul li a:hover {
  color: red;
  transition: color 0.7s ease;
}
.header__links nav ul li:hover ul {
  visibility: visible;
  opacity: 1;
  filter: alpha(opacity=100);
}
.header__links nav ul li ul {
  visibility: hidden;
  opacity: 0;
  filter: alpha(opacity=0);
  -webkit-transition: 500ms ease;
  -moz-transition: 500ms ease;
  -o-transition: 500ms ease;
  transition: 500ms ease;
  position: absolute;
  margin: 0;
  padding: 20px 0 0;
  right: 0;
}
.header__links nav ul li ul li {
  margin: 0;
  padding: 10px;
  background-color: blue;
}
<div class="header__links">
  <nav>
    <ul>
      <li><a href="#">Test</a>
        <ul>
          <li><a href="#">Test Test Test</a></li>
          <li><a href="#">Test Test</a></li>
          <li><a href="#">Test Test Test</a></li>
          <li><a href="#">Test Test</a></li>
        </ul>
      </li>
      <li><a href="#">Test</a></li>
      <li><a href="#">Test</a></li>
      <li><a href="#">Test</a></li>
    </ul>
  </nav>
</div>