skip to Main Content

I am attempting to add an input element next to a drop down menu so that when the drop down menu open it does not cover the input. However, even after setting the display property of the input box to inline-block it still appears below the dropdown button element. May i please have some assistance in figuring out why this is happening and what may be a solution? I tried to use the display property and set it to inline-block but it does not work.

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>

2

Answers


  1. I assume you would like to have a working dropdown list next to an input field.
    This answer just aimed to solve the overlapping problem.

    Change both the dropdown button and input field to be inline-block, such to be side by side. The content set to block such that placed below.

    .dropdown:hover .dropdown-content {
      display: block;
    }
    
    .dropbtn {
      display: inline-block;
      ......
    }
    .exercise-input {
      display: inline-block;
      ......
    }
    
    Login or Signup to reply.
  2. You can do it using list group.

        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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search