Search code examples
htmltwitter-bootstrapinputlabelfloating-labels

How to add 2nd floating label to input when using 1 div only in bootstrap 5


How can i add a 2nd floating label to the right input field? the problem now is that the 2nd label overlaps the first label in the 1st input field:

<div class="form-floating input-group">                         
    <input type="text" name="" class="form-control delete-from-date" value="" />
    <label for="floatingDateToDelete">startdate</label>
    <span class="input-group-text">till</span>                          
    <input type="text" name="" class="form-control delete-till-date" value="" />
    <label for="floatingDateToDelete">Enddate</label>   
    <button type="submit" class="btn btn-danger delete-multiple-events" name="submit">Delete</button>               
</div>

here is a fiddle you can see the issue: http://jsfiddle.net/1uf0zv2d/6/

The floating label "Enddate" should occur in the right input field


Solution

  • Each input+label combo needs to be within its own .form-floating class. Having multiple inputs in the same div was causing that issue. Also, a placeholder is required on each input field.

    I've moved the entire combo into a container that has .input-group on it. You can look at the changes here.

    <html>
    
    <head>
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    </head>
    
    <body>
      <div class="input-group">
        <div class="form-floating">
          <input type="text" class="form-control delete-from-date" id="floatingDateToDelete1" placeholder="startdate" />
          <label for="floatingDateToDelete1">startdate</label>
        </div>
        <span class="input-group-text">till</span>
        <div class="form-floating">
          <input type="text" class="form-control delete-till-date" id="floatingDateToDelete2" placeholder="Enddate" />
          <label for="floatingDateToDelete2">Enddate</label>
        </div>
        <button type="submit" class="btn btn-danger delete-multiple-events" name="submit">Delete</button>
      </div>
    </body>
    
    </html>