Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap accordion with arrows

I have a simple question regarding the Bootstrap accordion.

I created an accordion which is clickable on the header to expand. This works well but my problem is that the arrows are not showing in the header.

.panel-heading [data-toggle="collapse"]:after {
  font-family: 'Glyphicons Halflings';
  content: "\e072";
  /* "play" icon */
  float: right;
  color: #b0c5d8;
  font-size: 18px;
  line-height: 22px;
  /* rotate "play" icon from > (right arrow) to down arrow */
  -webkit-transform: rotate(-90deg);
  -moz-transform: rotate(-90deg);
  -ms-transform: rotate(-90deg);
  -o-transform: rotate(-90deg);
  transform: rotate(-90deg);
}

.panel-heading [data-toggle="collapse"].collapsed:after {
  /* rotate "play" icon from > (right arrow) to ^ (up arrow) */
  -webkit-transform: rotate(90deg);
  -moz-transform: rotate(90deg);
  -ms-transform: rotate(90deg);
  -o-transform: rotate(90deg);
  transform: rotate(90deg);
}
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Accordion START -->
<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" data-target="#collapseOne">
      <h4 class="panel-title">Test1</h4>

    </div>
    <div id="collapseOne" class="panel-collapse collapse">
      <div class="panel-body">
        <p>sadfsadfsdaf sadf</p>
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" data-target="#collapseTwo">
      <h4 class="panel-title">Test2</h4>

    </div>
    <div id="collapseTwo" class="panel-collapse collapse">
      <div class="panel-body">
        <p>
          Bla bla bla
        </p>
      </div>
    </div>
  </div>
</div>
<!-- Accordion END -->
like image 992
Christian Avatar asked Jun 23 '16 06:06

Christian


People also ask

How do you add arrows to bootstrap accordion?

you can try bind a class on panel element on bootstrap events with javascript: v4-alpha.getbootstrap.com/components/collapse/#events and then css transform (rotate 180%) the arrow when class is on panel :-).

Does bootstrap have accordion?

This is the first item's accordion body. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables.


2 Answers

Problem is in space between selectors:

.panel-heading [data-toggle="collapse"]:after
              ^------- // remove this space to make this selector work

Now you are selecting all elements having data-toggle attribute which are descendants of .panel-heading. It should be:

.panel-heading[data-toggle="collapse"]:after

.panel-heading {
  position: relative;
}
.panel-heading[data-toggle="collapse"]:after {
  font-family: 'Glyphicons Halflings';
  content: "\e072"; /* "play" icon */
  position: absolute;
  color: #b0c5d8;
  font-size: 18px;
  line-height: 22px;
  right: 20px;
  top: calc(50% - 10px);

  /* rotate "play" icon from > (right arrow) to down arrow */
  -webkit-transform: rotate(-90deg);
  -moz-transform:    rotate(-90deg);
  -ms-transform:     rotate(-90deg);
  -o-transform:      rotate(-90deg);
  transform:         rotate(-90deg);
}
.panel-heading[data-toggle="collapse"].collapsed:after {
  /* rotate "play" icon from > (right arrow) to ^ (up arrow) */
  -webkit-transform: rotate(90deg);
  -moz-transform:    rotate(90deg);
  -ms-transform:     rotate(90deg);
  -o-transform:      rotate(90deg);
  transform:         rotate(90deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Accordion START -->
      <div class="panel-group" id="accordion">
        <div class="panel panel-default">
          <div class="panel-heading accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" data-target="#collapseOne">
            <h4 class="panel-title">Test1</h4>

          </div>
          <div id="collapseOne" class="panel-collapse collapse">
            <div class="panel-body">
              <p>sadfsadfsdaf sadf</p>
            </div>
          </div>
        </div>
        <div class="panel panel-default">
          <div class="panel-heading accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" data-target="#collapseTwo">
            <h4 class="panel-title">Test2</h4>

          </div>
          <div id="collapseTwo" class="panel-collapse collapse">
            <div class="panel-body">
<p>
Bla bla bla
</p>
            </div>
          </div>
        </div>
      </div>
      <!-- Accordion END -->
like image 132
Mohammad Usman Avatar answered Oct 07 '22 17:10

Mohammad Usman


No fonts (icons) needed:

I come with a simple solution:

.card-link::after {
    content: "";
    border-bottom: 0;
    border-top: .3em solid;
    border-right: .3em solid transparent;
    border-left: .3em solid transparent;
}

.card-link[aria-expanded="true"]::after {
    content: "";
    border-top: 0;
    border-bottom: .3em solid;
}
like image 37
Amir Fo Avatar answered Oct 07 '22 19:10

Amir Fo