Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3 accordion styling on open/closed

Trying to stylize bs3 accordions... I had added an image to the title and the body. I have managed to set black title text on close and blue text on hover and/or open.

How can I change the background color of 'panel-heading' on hover and/or open? I have tried quite a few things with no results. Is this going to be a jquery only solution by adding/removing the styles?

<div class="panel panel-faq">
    <div class="panel-heading">
        <h4 class="panel-title">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#accordion1_2">
                1. Some title goes here
            </a>
        </h4>
    </div>
    <div id="accordion1_2" class="panel-collapse collapse">
        <div class="panel-body">
                sample entry text goes here
        </div>
    </div>
</div>


.panel-faq{
    border-color: #dddddd;
}
.panel-faq .panel-heading {
    color: #333333;
    background:#f5f5f5 url('/assets/img/faq-question.png') no-repeat 10px center;
    padding-left: 45px;
    border-color: #dddddd;
}

.panel-default > .panel-heading + .panel-collapse .panel-body {
  border-top-color: #dddddd;
}

.panel-default > .panel-footer + .panel-collapse .panel-body {
  border-bottom-color: #dddddd;
}

.panel-faq .panel-body {
    background: url('/assets/img/faq-answer.png') no-repeat 40px 20px;
    padding-left: 75px;
}

.panel-faq .accordion-toggle, .panel-faq a.accordion-toggle:hover{
    color:#428bca;
    text-decoration:none;
}

.panel-faq .accordion-toggle.collapsed {
    color:#333333;
}
like image 546
user756659 Avatar asked Dec 15 '13 07:12

user756659


2 Answers

Since I was unable to do anything in css only I went ahead and some jquery. For anyone interested you can add/remove an active class to the entire div then apply styling that way.

    $('.panel-faq').on('show.bs.collapse', function () {
         $(this).addClass('active');
    });

    $('.panel-faq').on('hide.bs.collapse', function () {
         $(this).removeClass('active');
    });
like image 177
user756659 Avatar answered Sep 21 '22 13:09

user756659


To do this the AngularJS way (no JQuery) you can put a conditional class on the accordion-group.

<div accordion-group is-open="first.open"
     ng-class="{'active': first.open}">
  <div accordion-heading>First Section</div>
  <div>First content</div>
</div>


.active[accordion-group] .panel-heading {
  background-color: black;
  color: white;
}

This way the active class is applied to the whole accordion-group when it is open and the panel-heading div (generated by bootstrap-ui) can be styled with a specific CSS selector.

like image 23
Ted Percival Avatar answered Sep 21 '22 13:09

Ted Percival