Search code examples
cssbootstrap-4

Is it possible to add an icon besides arrow in bootstrap accordion?


I'm using a bootstrap 4 accordion and I was trying to add an icon besides the arrow up/down icon. I've tried to add a row and both icon place inside a row but still can't figure out how to make these two inline.

Here's what I have tried so far.

<div class="accordion" id="accordionExample">

<div class="card mb-3">
    <div class="card-header" id="heading-1">
         <h5 class="mb-0 card-header-title"> 
            <a class="btn btn-link text-left collapsed" role="button" data-toggle="collapse" data-target="#collapse-1" aria-expanded="false" aria-controls="collapse-1"> 
              Item 3 
            </a> 
         </h5>
    </div>
    <div id="collapse-1" class="collapse" data-parent="#accordionExample" aria-labelledby="heading-1">
        <div class="card-body"> 
            Description 1 
        </div>
    </div>
</div>

</div>

CSS:

    .card-header-title > a {`
        display: block;
        position: relative;
    }
    .card-header-title > a:hover {
        cursor: pointer;
    }
    .card-header-title > a:after {
        content: "\f078"; /* fa-chevron-down */
        font-family: "Font Awesome 5 Free";
        font-weight: 900;
        position: absolute;
        right: 0;
    }
    .card-header-title > a[aria-expanded="true"]:after {
        content: "\f077"; /* fa-chevron-up */
    }

Result: enter image description here

And this is what I expected: expected


Solution

  • Do you mean something like this?

    .card-header-title > a {
      display: block;
      position: relative;
    }
    
    .card-header-title > a:hover {
      cursor: pointer;
    }
    
    .card-header-title i.fa-chevron-up {
      display: none;
    }
    
    .card-header-title > a[aria-expanded="true"] i.fa-chevron-down {
      display: none;
    }
    
    .card-header-title > a[aria-expanded="true"] i.fa-chevron-up {
      display: inline;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <div class="accordion" id="accordionExample">
      <div class="card mb-3">
        <div class="card-header" id="heading-1">
          <h5 class="mb-0 card-header-title d-flex justify-content-between align-items-center">
            <a class="btn btn-link text-left collapsed d-inline-flex justify-content-between w-100" role="button" data-toggle="collapse" data-target="#collapse-1" aria-expanded="false" aria-controls="collapse-1">
              Item 3
              <span class="float-right">
                <i class="fa-solid fa-chevron-down"></i>
                <i class="fa-solid fa-chevron-up"></i>
              </span>
            </a>
            <span><i class="fa-solid fa-trash float-right ml-2"></i></span>
          </h5>
        </div>
        <div id="collapse-1" class="collapse" data-parent="#accordionExample" aria-labelledby="heading-1">
          <div class="card-body">
            Description 1
          </div>
        </div>
      </div>
      
      <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>