在CSS中的按钮元素旁边放置和输入元素

ejk8hzay  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(201)

我试图在下拉菜单旁边添加一个input元素,这样当下拉菜单打开时,它就不会覆盖输入。即使在设置输入框的显示属性为inline-block后,它仍然显示在下拉按钮元素的下面。我可以请一些帮助来找出为什么会发生这种情况,以及什么可能是解决方案吗?我尝试使用display属性并将其设置为inline-block,但它不起作用。

div.metric.two {
    display:block;
    position: relative;
    align-items: center;
    background-color: var(--blue);
    flex: 1 1 0%;
    padding: 2rem 1rem;
    max-width: 450px;
    width:100%;
}

.workout-header {
    display: block;
    align-items: center;
}

.dropdown {
    position: relative;
}

.dropbtn {
    display:flex;
    margin-right: 10px;
    background-color: var(--darker);
    border-radius: 1rem;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
  }

  .dropbtn::after {
    content: "";
    display: inline-block;
    margin-left: 5px;
    width: 0;
    height: 0;
    border-top: 5px solid white;
    border-right: 5px solid transparent;
    border-left: 5px solid transparent;
    position: relative;
    top: -2px;
  }
  
  /* Style the dropdown content */
  .dropdown-content {
    display: none;
    background-color: var(--dark);
    align-items: flex-start;
    justify-content: flex-start;
    font-size: 20px;
    position: absolute;
    z-index: 1;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    border-radius: 1rem;
    padding: 8px 0;
    font-size: 14px;
  }

  .dropdown-content a {
    color: white;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    border-radius: 1rem;
  }
  
  .dropdown-content a:hover {
    background-color: var(--darker);
  }
  
  /* Show the dropdown menu on hover */
  .dropdown:hover .dropdown-content {
    display: inline-block;
  }
  
  /* Change the background color of the dropdown button when the dropdown content is shown */
  .dropdown:hover .dropbtn {
    background-color: var(--dark);
  }

  .exercise-input {
    display:inline-block;
    /* top:0; */
    /* left: calc(100% +10px); */
    background-color: var(--dark);
    margin-left: 4px;
    padding: 1rem;
    border-radius: 1rem;
    color:var(--light-gray)
  }
<div class="metric two">
  <div class="workout-header">
    <h2 class="title_workouts">Workouts</h2>
    <div class="dropdown">
      <button class="dropbtn">Workout Type</button>
      <div id="workoutsDropdown" class="dropdown-content">
        <a href="#">Weight Training 🏋️</a>
        <a href="#">Cardio 🏃</a>
        <a href="#">Calisthenics 💪</a>
      </div>
      <input type="text" name="exercise-input" class="exercise-input" placeholder="Exercise"/>
    </div>
  </div>
</div>
z31licg0

z31licg01#

我假设您希望在输入字段旁边有一个工作下拉列表。
这个答案只是为了解决重叠的问题。
将下拉按钮和输入字段都更改为inline-block,使其并排。将内容设置为block,使其位于下方。

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

.dropbtn {
  display: inline-block;
  ......
}
.exercise-input {
  display: inline-block;
  ......
}
yjghlzjz

yjghlzjz2#

您可以使用列表组来完成此操作。

ul{
        padding: 0;
        list-style: none;
       
    }
    ul li{
        display: inline-block;
        position: relative;
        line-height: 21px;
        text-align: left;
    }
    ul li a{
        display: block;
        padding: 8px 25px;
        min-width: 200px;
        text-decoration: none;
        background-color: var(--dark);
        margin-left: 4px;
        padding: 1rem;
        border-radius: 1rem;
        color:var(--light-gray)
    }
    ul li a:hover{
        color: #fff;
        background: #939393;
    }
    ul li ul.dropdown{
        min-width: 200px; /* Set width of the dropdown */
       
        display: none;
        position: absolute;
        z-index: 999;
        left: 0;
    }
    ul li:hover ul.dropdown{
        display: block; /* Display the dropdown */
    }
    ul li ul.dropdown li{
        display: block;
    }
    
    .exercise-input {
    display:inline-block;
    /* top:0; */
    /* left: calc(100% +10px); */
    background-color: var(--dark);
    margin-left: 4px;
    padding: 1rem;
    border-radius: 1rem;
    color:var(--light-gray)
  }
<div class="metric two">
  <div class="workout-header">
    <h2 class="title_workouts">Workouts</h2>
    <div class="dropdown">
      <ul>
     <li> <input type="text" name="exercise-input" class="exercise-input" placeholder="Exercise"/>
        </li>
            <li>
            <a href="#">Workout Type &#9662;</a>
            <ul class="dropdown">
              <li><a href="#">Weight Training 🏋️</a></li>
                <li><a href="#">Cardio 🏃</a></li>
                <li><a href="#">Calisthenics 💪</a></li>
            </ul>
          </li>
       </ul>
    </div>
  </div>
</div>

相关问题