Make accordion closed by default in Bootstrap

I am using Bootstrap Accordion. I want all panels to be closed by default, but my panels begin to expand.

Here is my code :

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="false"> <c:forEach items="${proposals}" var="proposal" varStatus="serial"> <div class="panel panel-default"> <div class="panel-heading" role="tab" id="heading${proposal.propID}" > <h4 class="panel-title"> <span class="fa fa-paperclip" aria-hidden="true"></span> <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapse${proposal.propID}" aria-expanded="false" aria-controls="collapse${proposal.propID}"> ${proposal.title} </a> </h4> </div> <div id="collapse${proposal.propID}" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading${proposal.propID}"> ${proposal.interest} </div> </div> </c:forEach> </div> 

I also used aria-expanded="false" , but it does not work.

What mistake?

+8
source share
2 answers

This line seems guilty:

 <div id="collapse${proposal.propID}" class="panel-collapse collapse in" ... 

If you get rid of in in a class, it should work the way you structured it. Take a look at this example to see the difference between the two panels:

 <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" /> <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> <div class="panel panel-default"> <div class="panel-heading" role="tab" id="headingOne"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"> This Panel is Open By Default </a> </h4> </div> <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne"> <div class="panel-body"> Open </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading" role="tab" id="headingTwo"> <h4 class="panel-title"> <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> This Panel Is Closed By Default </a> </h4> </div> <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo"> <div class="panel-body"> Closed </div> </div> </div> </div> 

Note that in the first panel there is no class=collapsed tag on it <a> and has a class in on it <div> and OPEN is launched. The second panel switches them and starts CLOSED.

EDIT

Unable to create Bootply, keep getting "Application Error".

+17
source

I recently worked with Bootstrap 4 and could not figure out where the class name was "in" (it is mentioned in several answers to similar questions).

After a long search, I noticed updated documentation that states:

The collapse plugin uses several classes for working with heavy loads:

.collapse hides content

.collapse.show shows the content

.collapsing is added when the transition starts and is deleted when the transition ends.

So, the sample code now looks something like this:

  <div id="accordion"> <div class="card"> <div class="card-header" id="headingOne"> <h5 class="mb-0"> <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> Collapsible Group Item #1 </button> </h5> </div> <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion"> <div class="card-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> </div> </div> 

From which, it is important a little:

 <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion"> 

Removing the class name "show" ensures that the default Bootstrap accordion remains collapsed.

Hope this helps!

0
source

Source: https://habr.com/ru/post/1211275/


All Articles