skip to Main Content

Using a list-group, the order class works fine but it is not working when I tried using it under the dropdown-menu. Saw the docs in bootstrap 5 about ordering of classes but can’t make it to work inside the dropdown-menu.

See code sample below:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
    Dropdown button
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
    <li><a class="dropdown-item order-5" href="#">Last</a></li>
    <li><a class="dropdown-item" href="#">Another action</a></li>
    <li><a class="dropdown-item order-1" href="#">Should be first</a></li>
  </ul>
</div>

Anyone experienced this problem?

2

Answers


  1. The "order" class is part of Bootstrap’s Flexbox utilities, the list-group class is creating a flexbox from your <ul> element, but dropdown-menu is not, and this is why ordering don`t work with dropdown-menu. But with a little modification you can order the dropdown-menu too.

     <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
            <div class="row flex-column">
                <div class="order-5">
                    <li class=><a class="dropdown-item" href="#">Last</a></li>
                </div>
                <div class="order-2">
                    <li><a class="dropdown-item" href="#">Another action</a></li>
                </div>
                <div class="order-1">
                    <li><a class="dropdown-item" href="#">Should be first</a></li>
                </div>
            </div>
        </ul>
    

    With this modification the <li> elements are inside a flexbox, and "order" class can work now.

    Login or Signup to reply.
  2. If you want to sort order using .order-* class, the parent element should a flexbox. Here is how you can use that sort order

    <div class="dropdown">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
        Dropdown button
      </button>
      <ul class="dropdown-menu flex-column" aria-labelledby="dropdownMenuButton1">
        <li class="order-5"><a class="dropdown-item" href="#">Last</a></li>
        <li class="order-1"><a class="dropdown-item" href="#">Another action A</a></li>
        <li class="order-1"><a class="dropdown-item" href="#">Another action B</a></li>
        <li class="order-0"><a class="dropdown-item" href="#">Should be first</a></li>
      </ul>
    </div>
    
    .dropdown-menu.flex-column.show {
        display: flex;
    }
    

    you need to write additional CSS because when dropdown-menu is showing the class .dropdown-menu.show is display: block

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search