【问题标题】:Centering Drop Down Button居中下拉按钮
【发布时间】:2021-07-27 02:56:31
【问题描述】:

我是新手,刚刚学习编码。我猜想我被困在一个非常简单的事情上。如何在页面中居中下拉按钮?这是 HTML 代码。

.dropbtn {
  background-color: #04AA6D;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  min-width: 110px;
  font-weight: bold;
  vertical-align: middle;
}

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

.dropdown-content {
  display: none;
  position: center;
  background-color: #f1f1f1;
  min-width: 110px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  text-align: center;
}

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

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

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

.dropdown:hover .dropbtn {background-color: #3e8e41;}
<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

我没有为这个下拉按钮使用任何 CSS 或 JavaScript。

我还应该使用 CSS 和 JavaScript 来让这段代码运行良好吗?

请多多指教。

谢谢。

【问题讨论】:

    标签: html css dropdown dropdownbox


    【解决方案1】:

    您可以将下拉菜单的位置更改为绝对并设置左侧和顶部。

    .dropdown {
      position: absolute;
      top: 50%;
      left: 50%;
      display: inline-block;
      transform: translate(-50%,-50%)
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用:

      body {
        display: flex;
        justify-content: center;
      }
      

      ( 在现实世界的场景中,您将把它应用到容器元素而不是整个主体)

      body {
        display: flex;
        justify-content: center;
      }
      
      .dropbtn {
        background-color: #04AA6D;
        color: white;
        padding: 16px;
        font-size: 16px;
        border: none;
        min-width: 110px;
        font-weight: bold;
        vertical-align: middle;
        margin: 0 auto;
      }
      
      .dropdown {
        position: relative;
        display: inline-block;
      }
      
      .dropdown-content {
        display: none;
        position: center;
        background-color: #f1f1f1;
        min-width: 110px;
        box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
        z-index: 1;
        text-align: center;
      }
      
      .dropdown-content a {
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
      }
      
      .dropdown-content a:hover {
        background-color: #ddd;
      }
      
      .dropdown:hover .dropdown-content {
        display: block;
      }
      
      .dropdown:hover .dropbtn {
        background-color: #3e8e41;
      }
      <div class="dropdown">
        <button class="dropbtn">Dropdown</button>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </div>

      【讨论】:

      • 谢谢。有用。是的,我应该将这些代码应用于容器元素。
      • 谢谢。有用。是的,我应该将这些代码应用于容器元素。
      【解决方案3】:

      你也可以使用这个简单的方法:

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

      【讨论】:

      • 这需要在父级 (body)
      【解决方案4】:

      .dropbtn {
        background-color: #04AA6D;
        color: white;
        padding: 16px;
        font-size: 16px;
        border: none;
        min-width: 110px;
        font-weight: bold;
        vertical-align: middle;
      }
      
      .dropdown {
        position: relative;
        display: inline-block;
      }
      
      .dropdown-content {
        display: none;
        position: center;
        background-color: #f1f1f1;
        min-width: 110px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
        text-align: center;
      }
      
      .dropdown-content a {
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
      }
      
      .dropdown-content a:hover {background-color: #ddd;}
      
      .dropdown:hover .dropdown-content {display: block;}
      
      .dropdown:hover .dropbtn {background-color: #3e8e41;}
      
      .dropdown{
         display:flex;
         flex-direction:column;
         justify-content:center;
         align-items:center;
         border:solid 2px green;
         height:100vh;
         }
      <!DOCTYPE html>
      <html>
      
      
      
      <div class="dropdown">
        <button class="dropbtn">Dropdown</button>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </div>
      
      </html>

      【讨论】:

        猜你喜欢
        • 2015-12-26
        • 2021-12-11
        • 1970-01-01
        • 2011-01-29
        • 1970-01-01
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        相关资源
        最近更新 更多