Friday, April 19, 2024
HomejQuery5 Examples to Study Utilizing Bootstrap 5 Accordion/Collapse

5 Examples to Study Utilizing Bootstrap 5 Accordion/Collapse


Bootstrap 5 Accordion/Collapsible

Similar to older Bootstrap variations, chances are you’ll create accordion or collapsible elements in your net pages utilizing the Bootstrap 5 framework – with out utilizing JavaScript.

Why use Accordion?

You probably have much less area on an online web page and extra content material to indicate then chances are you’ll create a panel line part that expands because the consumer clicks on the heading. In accordion, the content material is pushed down fairly than overlapping the highest web page content material (like overlays).

The content material hidden beneath the heading could also be textual content, photos or video, and so forth.

Bootstrap 5 has built-in courses e.g. collapse, accordion-collapse, accordion-button, and so forth. that you could be use together to create panels with content material in your web site.

An instance of a easy accordion in Bootstrap 5

Within the first instance, we have now created an accordion part with three panels. As the online web page masses, the primary panel is opened with its content material.

The accordion may be closed/opened by utilizing the arrows:

Bootstrap-accordion-bas



That is how the code labored:

  • Bootstrap 5 makes use of a collapse plug-in internally for making the accordion collapsible.
  • In the primary <div>, the .accordion class is used
  • Inside that div, the <h2> tag is used that refers .accordion-header class for creating the heading of the panels.
  • The contents div makes use of accordion-collapse collapse. If you wish to open that panel by default then add .present class.
  • Lastly, the .card-body class is used the place we positioned the content material.
  • Repeat the method for creating different panels with their contents.

An accordion with varied coloration panels instance

Through the use of Bootstrap 5 coloration courses, chances are you’ll create panels/headings with totally different colours. The instance under makes use of the next coloration courses for creating an accordion:

  • bg-primary text-white
  • bg-success text-white
  • bg-warning text-white

You could be taught extra about background and textual content coloration courses.

accordion-colorful



Discover the next strains that comprise heading within the code:

Maintain opened accordions as different is clicked

Within the above two examples, the opened panel is closed if you open one other panel. In case you wish to maintain opened panels when a closed one is clicked, observe this stuff:

  • Take away or don’t use the data-bs-parent attribute on every .accordion-collapse

See the instance under:

accordion-opened



You may see, all accordions stay open.

Need to change icons to open/shut accordions? See an instance with circle arrows

You may want to change the icons of the accordion which might be offered by default as utilizing the Bootstrap 5 framework.

That is easy to vary. First, see an instance under the place we used the up/down circle arrow (svg icons) offered by Bootstrap.

accordion-icons

How one can change the icon within the accordion?

  • You may even see the icon listing right here.
  • Above the </head> tag, the <type> is used to switch the default icons.
  • You should use accordion-button::after and accordion-button:not courses and exchange the code for the icon. We used SVG offered by Bootstrap.

Styling the content material space of the accordion instance

Similar to the panels, you might also change the background, textual content coloration, and so forth. of the content material space of an accordion.

For that, simply add the contextual background/textual content courses or different customized courses that you simply wish to type the content material space.

Within the demo under, we simply added the background coloration to vary the look of content material space:




You may see varied coloration panels together with their content material areas.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments