What do I need to know about Bootstrap Collapse?

Initially, everything was functioning smoothly. However, after deleting some JS files and unused CSS, I encountered a problem with the accordion buttons not expanding. I thought my implementation didn't require JS, but I made sure to reintegrate all relevant JS components that were present when it was working.

I suspect that my issue lies in the absence of the necessary data-toggle in the CSS. The original CSS file does not contain any collapse toggles related to button or accordion classes, only .nav.

HTML:

<div class="card">
  <div class="card-header py-4" id="heading-1-1" data-toggle="collapse" role="button" data-target="#collapse-1-1" aria-expanded="false" aria-controls="collapse-1-1">
    <h6 class="mb-0"><i data-feather="file" class="mr-3"></i>Title</h6>
  </div>
  <div id="collapse-1-1" class="collapse" aria-labelledby="heading-1-1" data-parent="#accordion-1">
    <div class="card-body">
      <p>Text</p>
    </div>
  </div>
</div>

CSS:

.collapse:not(.show) {
  display: none; }

.collapsing {
  position: relative;
  height: 0;
  overflow: hidden;
  transition: height 0.2s ease; }
  @media (prefers-reduced-motion: reduce) {
    .collapsing {
      transition: none; } }

Answer №1

Since you've included the bootstrap-4 tag, it suggests that Bootstrap 4 is being utilized.

To utilize collapse in Bootstrap 4, jQuery along with the Bootstrap JS bundle is required:

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="610b101404131821524f574f51">[email protected]</a>/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="21434e4e55525553405161150f170f10">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

Below is a simple example demonstrating the usage of collapse with both.

If Bootstrap 5 is being used, jQuery won't be needed as a dependency. Simply include the JS bundle.

Bootstrap 4

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8be9e4e4fff8fff9eafbcbbfa5bda5ba">[email protected]</a>/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef859e9a8a9d96afdcc1d9c1df">[email protected]</a>/dist/jquery.slim.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b3934342f282f293a2b1b6f756d756a">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    Title
  </button>

<div class="collapse" id="collapseExample">
  <div class="card card-body">
    <p>Text</p>
  </div>
</div>

Bootstrap 5 (no jQuery)

From https://getbootstrap.com/docs/5.0/components/collapse/

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a5a8a8b3b4b3b5a6b787f2e9f7e9f5">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9aba6a6bdbabdbba8b989fce7f9e7fb">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>


<p>
  <a class="btn btn-primary" data-bs-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
    Link with href
  </a>
  <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    Button with data-bs-target
  </button>
</p>
<div class="collapse" id="collapseExample">
  <div class="card card-body">
    Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
  </div>
</div>

Answer №2

The collapsible feature in the accordion is achieved using internal collapse functionality. In order to utilize the collapse component, it is necessary to include the js bootstrap bundle.

Ensure that the js bundle is imported within the <head> element of your HTML structure.

If you are using a CDN, you can include the following script:

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a6ababb0b7b0b6a5b484f1eaf6eaf6">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-IDwe1+LCz02ROU9k972gdyvl+AESN10+x7tBKgc9I5HFtuNz0wWnPclzo6p9vxnk" crossorigin="anonymous"></script>

Please note that certain components require Popper JS in addition to the Bootstrap bundle. Popper Js should be imported before the Js Bootstrap bundle.

For more detailed information regarding components that necessitate the Js Bootstrap bundle, refer to the official documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Maintaining the active status of section 1 after the page is refreshed using Javascript and CSS

I have a total of 3 buttons and 3 hidden sections available, which can be seen in the image below: click here for image description Whenever one of the buttons is clicked, the respective section transitions from being hidden to becoming visible: click he ...

How can I overlay a background image on top of another div?

This inquiry might not be the most trendy, but I am seeking guidance on the best approach to tackle this task, utilizing HTML, CSS, and Bootstrap. For reference, something similar to the image at this link. ...

Tips for dynamically positioning data in grid columns based on backend inputs

I have a challenge with displaying maps in a grid system from the backend. Currently, the maps are appearing vertically instead of in a grid format. My goal is for the grid layout to adjust based on the number of maps available. For example, if there are ...

Creating a responsive gallery using CSS techniques

I'm currently working on creating a simple gallery. My goal is to achieve the following design: https://i.sstatic.net/jq8pe.jpg However, the result I'm getting looks like this: https://i.sstatic.net/hSZyj.png The issue I'm facing is that ...

Is there a way to implement an event listener for a customized select element within a table on a webpage?

I have a table that contains columns populated from a database. I also have a custom select dropdown in one of the columns, along with an update button. I want to create an event listener for the button that captures the user's selection from the cust ...

Using jQuery and CSS to choose a specific set of elements

My goal is to choose a specific set of anchor elements using the nth-child pseudo selector. However, I am facing an issue because nth-child only works with child elements, and my structure looks like this: <div> <a>first link> </div> ...

Transforming the primary image to the thumbnail image when hovering over it with JSON objects

Currently, I am facing an issue with my single-page web application project that involves using jQuery, JSON Bootstrap. So far, I have successfully created a category page, product selection page, and item page. The problem arises on the item page where ...

Bring the element to the top of the page by clicking on the anchor within the element or anywhere within the specified div ID

I am looking to implement a functionality where the page scrolls to the top of the navigation div ID when a link inside the navigation div is clicked, or ideally even when clicking anywhere within the div itself that contains the navigation links. After r ...

Is concealing content using Javascript or jQuery worth exploring?

While I have been hiding content using display:none; in css, there are concerns that Google may not like this approach. However, due to the needs of jQuery animations, it has been necessary for me. Recently, I have come across a new method for hiding conte ...

Glitch in Safari 6: Round Corners Issue

Greetings! Upon observing the image provided, an unusual behavior can be noticed in Safari 6 concerning round corners. { border-radius: 5px 5px 5px 5px; } Upon hovering over the elements, a thin line appears that seems to accumulate with each subsequent ...

Tips for creating a clickable A href link in the menu bar that triggers an accordion to open in the body when clicked - html

Is there a way to open the first accordion when clicking on the "open 1st accordion" link, and do the same for the second link? The accordions themselves work perfectly fine, I just need a way to trigger them from outside their scope by clicking on links i ...

Enhancing CSS compatibility for Internet Explorer 9 with CSS3

I need to create a Jagged Edge border without relying on an image, and I want it to be compatible with IE9 and newer versions. Currently, it works in most browsers but not in IE9. Any suggestions or advice would be greatly appreciated. Thank you! Below is ...

Challenges with the grid system in Bootstrap version 5.3

Hello there, I am trying to craft the front end of my website using Bootstrap 5.3. However, I am encountering an issue with the grid system. It seems like the classes are not behaving as they should be. Below is the code I am using: <div class="co ...

How can I create a black border around an input button in the OPERA browser

List item Why does a black box appear around the input button in Opera? Check out the jsfiddle link for reference: http://jsfiddle.net/PKRRj/ Give it a try by clicking on the button. Test it in Opera, as there are no issues with other browsers. Here is ...

Adjusting the height of a vertical slider in Vuetify2 is not customizable

I've been trying to adjust the height of a vertical slider in vuetify2, but setting it to "800px" or using style="height:800px" doesn't seem to work as intended. Even though the box within my grid expands, the height of the slider remains unchan ...

Is there a way for me to identify what deletes CSS classes from an element during the first page load?

On a page where an element (specifically, a TextBox) initially has two CSS classes assigned when it loads, something strange is happening. After the page renders and JavaScript runs, the class attribute of the input element mysteriously becomes empty. I c ...

Angular ng-grid is not utilizing its maxWidth property

In my quest to make an angular ng-grid element with autosizing properties, I stumbled upon this code snippet: for (var i = 0; i < $scope.dates.length; i++) { var dateElement = { field: 'reportMap.' + $scope. ...

Having trouble getting my image and text to align properly in two columns

<img id="story_image" src="images/builder_story_side.png" alt="photo"/> <div id="story_right"> <h2 class="story_header">fdgdfgdfgdfgdfgfdgdfgfdgdfgdfgdfgdfgdfgdfgdfgdfgdfgfdgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfgdfg</h2> <p ...

Is it possible to conceal a link once it has been visited?

I am trying to figure out how to remove a link from a page once it has been visited. However, I am facing privacy restrictions with the :visited pseudo-class. Is there a way to achieve this without using display: none? For example: .someclass a:link {dis ...

Beautiful prompt interface for HTML

I'm attempting to create a sweet alert using the HTML option: swal({ title: "HTML <small>Title</small>!", text: "A custom <span style="color:#F8BB86">html<span> message.", html: true }); Instead of just text, ...