使下拉宽度调整为其内容

时间:2017-03-19 09:35:39

标签: html css

我有以下CSS和HTML:



.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  right: 0;
  background-color: #f9f9f9;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: inline-block;
  width: auto;
}

.dropdown-content a:hover {
  background-color: #f1f1f1
}

.dropdown:hover .dropdown-content {
  display: inline-block;
}

.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}

<body>
  <div class="dropdown" style="float:left;">
    <button class="dropbtn">Left</button>
    <div class="dropdown-content" style="left:0;">
      <a href="#">Make this phrase appear on one line.</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>

</body>
&#13;
&#13;
&#13;

我希望下拉列表中的所有菜单项都显示在线 - 无论它们有多长。人们说display: inline-block让这种情况发生。但仍然没有成功。你能提一下吗?

3 个答案:

答案 0 :(得分:2)

一个好老式的white-space: nowrap;怎么样?

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  position: absolute;
  left: 0;
  display: none;
  background-color: #f9f9f9;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  white-space: nowrap;
}

.dropdown-content a:hover {
  background-color: #f1f1f1
}

.dropdown:hover .dropdown-content {
  display: inline-block;
}

.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
<body>
  <div class="dropdown" style="float:left;">
    <button class="dropbtn">Left</button>
    <div class="dropdown-content" style="left:0;">
      <a href="#">Make this phrase appear on one line.</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>

</body>

答案 1 :(得分:1)

width:100%;添加到 .dropdown ,然后从 .dropdown-content 中移除right:0;,使其根据下拉列显示进行扩展

&#13;
&#13;
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropdown {
  position: relative;
  display: inline-block;
  width: 100%;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  top: 100%;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown-content a:hover {
  background-color: #f1f1f1
}

.dropdown:hover .dropdown-content {
  display: inline-block;
}

.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
&#13;
<body>
  <div class="dropdown" style="float:left;">
    <button class="dropbtn">Left</button>
    <div class="dropdown-content" style="left:0;">
      <a href="#">Make this phrase appear on one line.</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>

</body>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您的下拉容器不够大,无法容纳如此大的内容。 因此,将容器的宽度从160px增加到300px:

.dropdown-content {
min-width: 300px;
}

这将解决问题。

由于