Bootstrap 5 Card Header with 2 dropdowns and a button not showing one beside the other but on top

Viewed 587

I am using Boostrap 5 with a card and on the left I have a title.

On the right I have 2 dropdown menus and a button.

The issue is that the dropdowns and buttons are one on top of the other instead of side by side.

I need the 2 dropdowns and then finally the button.

Here is the code:

<div class="card">
    <div class="card-header">
      <div class="float-start">
        <h3>title</h3>
      </div>
      <div class="float-end">
        <div class="dropdown">
            <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown">
                <i class="bi bi-alarm"></i>
            </button>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#">Link 1</a></li>
              <li><a class="dropdown-item" href="#">Link 2</a></li>
              <li><a class="dropdown-item" href="#">Link 3</a></li>
            </ul>
          </div>
          <div class="dropdown">
            <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown">
                <i class="bi bi-arrows-move"></i>
            </button>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#">Link 1</a></li>
              <li><a class="dropdown-item" href="#">Link 2</a></li>
              <li><a class="dropdown-item" href="#">Link 3</a></li>
            </ul>
          </div>
        <button class="btn btn-primary"><i class="bi bi-x-circle"></i></button>
      </div>
    </div>
  </div>

How can I fix this?

1 Answers

That happens because your float-end pushes everything to the end. I suggest not using float, but instead using flex. It's better in every sense, and you have better control. The code would be:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>

<body>
  <div class="card">
    <div class="card-header d-flex justify-content-between">
      <h3>title</h3>
      <div class="d-flex justify-content-between gap-4">
        <div class="dropdown">
          <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown">
                <i class="bi bi-alarm"></i>
            </button>
          <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="#">Link 1</a></li>
            <li><a class="dropdown-item" href="#">Link 2</a></li>
            <li><a class="dropdown-item" href="#">Link 3</a></li>
          </ul>
        </div>
        <div class="dropdown">
          <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown">
                <i class="bi bi-arrows-move"></i>
            </button>
          <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="#">Link 1</a></li>
            <li><a class="dropdown-item" href="#">Link 2</a></li>
            <li><a class="dropdown-item" href="#">Link 3</a></li>
          </ul>
        </div>
        <button class="btn btn-primary"><i class="bi bi-x-circle"></i></button>
      </div>
    </div>
  </div>
  <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>
</body>

</html>

You can change the space between the dropdown and buttons by changing the value of gap-4 class (it can range from 0 to 5).

Related