How to use CSS to modify an icon with the bootstrap collapse

How to use CSS to modify an icon with the bootstrap collapse

Problem Description:

I’m trying to build a list with subpoints that collapse on command using the bootstrap collapse. This has worked fine, but I need to add an arrow that points right when the collapse is up, and down when it’s dropped down.

This is my HTML…

<ul>
<li data-toggle="collapse" data-target="#Sub1">List Item One <em class="glyphicon glyphicon-chevron-right"></em><em class="glyphicon glyphicon-chevron-down"></em>
<ul class="collapse" id="Sub1">
<li>Sub-item</li>
</ul>
<li>List Item Two</li>
</ul>

Currently I’m using this CSS to modify it…

.collapse {display: none;}
.collapse.in {display: block;}
.collapsed > .glyphicon .glyphicon-chevron-down {display: inline-block;}
.collapsed > .glyphicon .glyphicon-chevron-right {display: none;}

http://jsfiddle.net/ddx0c3c2/

The problem I’m encountering is that both icons are displaying, and not disappearing when the class changes. Is it possible to do this with just CSS, or do I need to use jQuery?

Solution – 1

Here you are: JSFiddle. I’ve solved it with jQuery.

Your first “mistake” was to have a false CSS-Selector order. Your glyphicon isn’t inside the .collapse class.

HTML:

<ul>
    <li data-toggle="collapse" data-target="#Sub1">List Item One <em class="glyphicon glyphicon-chevron-right arrow_show"></em><em class="glyphicon glyphicon-chevron-down"></em>

        <ul class="collapse" id="Sub1">
            <li>Sub-item</li>
        </ul>
        <li>List Item Two</li>
</ul>

CSS:

.collapse {
  display: none;
}

.collapse.in {
  display: block;
}

.glyphicon {
    display:none;
}
.arrow_show {
    display: inline;
}

JS:

$('li[data-target="#Sub1"]').on("click", function() {
  $('.glyphicon-chevron-right').toggleClass('arrow_show');
  $('.glyphicon-chevron-down').toggleClass('arrow_show');
 })

Solution – 2

If set up correctly, you don’t need jQuery, you just need to set up your css:

DEMO: http://jsbin.com/peqihe/2/edit

enter image description here

CSS — these are my styles for making it better looking, it’s a bit much.

 .panel-group, 
 .panel-group .panel, 
 .panel-group .panel-heading,
 .panel-group .panel-heading a,  
 .panel-group .panel-title,  
 .panel-group .panel-title a,  
 .panel-group .panel-body,
 .panel-group .panel-group .panel-heading + .panel-collapse > .panel-body  {
    border-radius:2px;
    border:0;
}
.panel-group .panel-heading + .panel-collapse > .panel-body {
  border-top: 0;
}


 .panel-group .panel-heading {
    padding: 0;
}
 .panel-group .panel-heading a {
    display: block;
    background: #668bb1;
    color:#fff;
    padding:15px;
     text-decoration:none;
   position: relative;
}
 .panel-group .panel-heading a.collapsed {
    background: #c4ced5; 
    color:inherit;
}

 .panel-group .panel-heading a:after {
    content: '-';
    position: absolute;
    right: 20px;
}
 .panel-group .panel-heading a.collapsed:after {
    content: '+';
}
 .panel-group .panel-collapse {
    margin-top:5px!important;
}
 .panel-group .panel-body  {
    background: #f7f7f7;
    padding:15px;
}
 .panel-group .panel {
  background-color: transparent;
}

.panel-group .panel-body p:last-child,
.panel-group .panel-body  ul:last-child,
.panel-group .panel-body  ol:last-child {
  margin-bottom: 0;
}

HTML

<div class="container">

         <div class="panel-group" id="accordion">

            <div class="panel panel-default">
               <div class="panel-heading">
                  <h4 class="panel-title">
                     <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                     Collapsible Group Item #1
                     </a>
                  </h4>
               </div><!--/.panel-heading -->
               <div id="collapseOne" class="panel-collapse collapse in">
                  <div class="panel-body">
                     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                  </div><!--/.panel-body -->
               </div><!--/.panel-collapse -->
            </div><!-- /.panel -->

            <div class="panel panel-default">
               <div class="panel-heading">
                  <h4 class="panel-title">
                     <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                     Collapsible Group Item #2
                     </a>
                  </h4>
               </div><!--/.panel-heading -->
               <div id="collapseTwo" class="panel-collapse collapse">
                  <div class="panel-body">
                     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                  </div><!--/.panel-body -->
               </div><!--/.panel-collapse -->
            </div><!-- /.panel -->

            <div class="panel panel-default">
               <div class="panel-heading">
                  <h4 class="panel-title">
                     <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
                     Collapsible Group Item #3
                     </a>
                  </h4>
               </div><!--/.panel-heading -->
               <div id="collapseThree" class="panel-collapse collapse">
                  <div class="panel-body">
                     Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                  </div><!--/.panel-body -->
               </div><!--/.panel-collapse -->
            </div><!-- /.panel -->
         </div><!-- /.panel-group -->

</div><!-- /.container -->  
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject