减少下拉菜单的宽度

时间:2018-02-13 03:51:31

标签: html css css3

我有两页代码。我希望第一页的下拉菜单看起来像第二页的下拉菜单。第二页是我从W3学校复制并粘贴的一些代码。

问题出在第一页,下拉菜单的宽度与导航栏相同。我希望导航栏的宽度更小,我无法弄清楚为什么导航条的宽度相同。

第一页

ul {
  margin: 0;
  padding: 0;
  background-color: green;
  overflow: hidden;
  list-style-type: none;
}

li {
  float: left;
}

li a,
.dropbtn {
  display: inline-block;
  padding: 50px 100px;
  text-decoration: none;
  color: white;
}

li a:hover {
  color: white;
  background-color: #333;
}

.dropdown {
  display: inline-block;
}

.dropcont {
  display: none;
  position: absolute;
  background-color: #333 min-width:200px;
  z-index: 1;
}

.dropcont a {
  color: white;
  padding: 12px 16px;
  display: block;
  text-align: left;
}

.dropdown:hover .dropcont {
  display: block;
}
<ul>
  <li><a href="#home"> Home</a></li>
  <li><a href="#home1"> Your Home</a></li>
  <li><a href="#home2">Home Sales</a></li>
  <li class="dropdown">
    <a href="#home3" class="dropbtn">Home profile</a>
    <div class="dropcont">
      <a href="#home2">Home2</a>
      <a href="#home3">Home3</a>
      <a href="#home4">Home4</a>
    </div>
  </li>
</ul>

第二页

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}

li {
  float: left;
}

li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

li.dropdown {
  display: inline-block;
}

.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;
}

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

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

.dropdown:hover .dropdown-content {
  display: block;
}
<ul>
  <li><a href="#home">Home</a></li>
  <li><a href="#news">News</a></li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>
</ul>

1 个答案:

答案 0 :(得分:0)

这是更新后的css:

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color:green;
}

li {
  float: left;
}

li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover,
.dropdown:hover .dropbtn {
  background-color:#333;
}

li.dropdown {
  display: inline-block;
}

.dropcont {
  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;
}

.dropcont a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropcont a:hover {
  background-color: #f1f1f1
}

.dropdown:hover .dropcont {
  display: block;
}
<ul>
  <li><a href="#home"> Home</a></li>
  <li><a href="#home1"> Your Home</a></li>
  <li><a href="#home2">Home Sales</a></li>
  <li class="dropdown">
    <a href="#home3" class="dropbtn">Home profile</a>
    <div class="dropcont">
      <a href="#home2">Home2</a>
      <a href="#home3">Home3</a>
      <a href="#home4">Home4</a>
    </div>
  </li>
</ul>