Category: 3. Components

https://cdn3d.iconscout.com/3d/premium/thumb/components-3d-icon-png-download-10523284.png

  • Badges

    This chapter will discuss about bootstrap badges and the classes to create badges. Badges are frequently used for highlighting important details on web pages, such as headings, warnings, and notification counters.

    Bootstrap badges are numerical indication or notification counters (of how many items are associated with a link).

    Badges

    To scale them according to the size of the immediate parent element, badge uses comparative font scaling and em units. In page links, focus or hover styles are no longer supported as of version 5.

    Following example demonstrates how to create inline badges using Bootstrap.

    Example

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><title>Bootstrap - Badges</title></head><body><h1>Sample heading <span class="badge bg-secondary">New</span></h1><h2>Sample heading <span class="badge bg-secondary">Offer</span></h2><h3>Sample heading <span class="badge bg-secondary">New</span></h3><h4>Sample heading <span class="badge bg-secondary">Offer</span></h4><h5>Sample heading <span class="badge bg-secondary">New</span></h5><h6>Sample heading <span class="badge bg-secondary">Offer</span></h6></body></html>

    Buttons

    Badges can be used as part of links or buttons to provide a counter. The users will only see the content of the badge because of how badges are styled, which gives a visual cue as to their purpose. These badges may appear to be random additions of words or numbers at the conclusion of a sentence, a link, or a button, based on the circumstances.

    Example

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><title>Bootstrap - Badges</title></head><body><button type="button" class="btn btn-success" style="margin: 20px;">
    
      Messages &lt;span class="badge text-bg-warning"&gt;6&lt;/span&gt;&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Notifications

    This example explains a visually hidden class with a portion of additional text where users can see the digit "32" represents the number of notifications.

    Example

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><title>Bootstrap - Badges</title></head><body><button type="button" class="btn btn-success position-relative" style="margin: 20px;">
    
    Notifications
    &lt;span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-primary"&gt;
      32+
      &lt;span class="visually-hidden"&gt;unread notifications&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Positioned

    .badge can be modified with top-0 start-100 translate-middle utilities and placed in the corner of a link or button. Let's look at the sample below to see how it generally functions.

    Example

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><title>Bootstrap - Badges</title></head><body><button type="button" class="btn btn-primary position-relative" style="margin: 20px;">
    
    Inbox
    &lt;span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-info"&gt;
      55+
      &lt;span class="visually-hidden"&gt;unread notifications&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Indicator

    Bootstrap Badges are a numerical indication of the number of items associated with a link. Items that are new or unread are highlighted using badges

    Example

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><title>Bootstrap - Badges</title></head><body><button type="button" class="btn btn-info position-relative" style="margin: 20px;">
    
    Webinar
    &lt;span class="position-absolute top-0 start-100 translate-middle p-2 bg-primary border border-light rounded-circle"&gt;&lt;span class="visually-hidden"&gt;New notifications&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Background colors

    Using the .text-bg-{color} helpers , users may set a background color with a contrasting foreground color. In the past, pairing the preferred .text-{color} and .bg-{color} utilities manually was necessary for styling; nevertheless, users are still free to do so if they wish.

    Use the text-bg-primarytext-bg-infotext-bg-success etc. utility classes offered by bootstrap's background colour framework to instantly change the appearance of the badges.

    Example

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><title>Bootstrap - Badges</title></head><body style="margin: 20px;"><span class="badge text-bg-primary">Primary</span><span class="badge text-bg-info">Info</span><span class="badge text-bg-secondary">Secondary</span><span class="badge text-bg-success">Success</span><span class="badge text-bg-light">Light</span><span class="badge text-bg-dark">Dark</span><span class="badge text-bg-warning">Warning</span><span class="badge text-bg-danger">Danger</span></body></html>

    Pill Badges

    Users can design badges in the shape of pills, or badges having more rounded corners, by using the .rounded-pill modifier class, as demonstrated in the example below.

    To quickly modify the appearance of the badges, use the .rounded-pill utility classes provided in bootstrap's background color framework.

    Example

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><title>Bootstrap - Badges</title></head><body style="margin: 20px;"><span class="badge rounded-pill text-bg-primary">Primary</span><span class="badge rounded-pill text-bg-secondary">Secondary</span><span class="badge rounded-pill text-bg-info">Info</span><span class="badge rounded-pill text-bg-success">Success</span><span class="badge rounded-pill text-bg-light">Light</span><span class="badge rounded-pill text-bg-dark">Dark</span><span class="badge rounded-pill text-bg-danger">Danger</span><span class="badge rounded-pill text-bg-warning">Warning</span></body></html>

  • Alerts

    This chapter discusses about the Bootstrap alerts. The alert messages are often the stand out messages shown to the user, where some user action is required, such as warning, error, information or confirmation messages.

    By extending the .alert base class with the contextual classes (such as .alert-success, .alert-warning, .alert-info, etc.), you can quickly and simply build attractive alert messages for any number of reasons using Bootstrap. To cancel any alert, you may also include an optional close button.

    Simple Alerts

    There is a total of 8 different alert types offered by Bootstrap. The most typical alerts, such as success, error or danger, warning and info alerts, etc., are demonstrated in the example below.

    Example

    You can edit and try running this code using the Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Alerts</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-3"><h4 class="fs-2">Examples of alert messages</h4><div class="alert alert-primary" role="alert">
    
                It is a primary alert box!
            &lt;/div&gt;&lt;div class="alert alert-secondary" role="alert"&gt;
                It is a secondary alert box!
            &lt;/div&gt;&lt;div class="alert alert-success" role="alert"&gt;
                It is a success alert box!
            &lt;/div&gt;&lt;div class="alert alert-danger" role="alert"&gt;
                It is a danger alert box!
            &lt;/div&gt;&lt;div class="alert alert-warning" role="alert"&gt;
                It is a warning alert box!
            &lt;/div&gt;&lt;div class="alert alert-info" role="alert"&gt;
                It is an info alert box!
            &lt;/div&gt;&lt;div class="alert alert-light" role="alert"&gt;
                It is a light alert box!
            &lt;/div&gt;&lt;div class="alert alert-dark" role="alert"&gt;
                It is a dark alert box!
            &lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Accessibility tip: Use of color to the alert messages just provides a visual indication, and this will not be helpful to users of assistive technologies like screen readers. Make sure that the meaning is clear from the content itself.

    Use alternative means to add clarity to the content using the .visually-hidden class.

    Live alert example

    You can add a live alert message on your webpage. In order to achieve this refer the example given below.

    Example

    You can edit and try running this code using the Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Alerts</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-3"><h4 class="fs-2">Live alert</h4><div class="alert alert-primary alert-dismissible" role="alert"><div id="liveAlertPlaceholder"></div><button type="button" class="btn btn-primary" id="liveAlertBtn">Show live alert</button></div></div><script>
    
                const alertPlaceholder = document.getElementById('liveAlertPlaceholder')
                const appendAlert = (message, type) =&gt; {
                const wrapper = document.createElement('div')
                wrapper.innerHTML = [
                &amp;lt;div class="alert alert-${type} alert-dismissible" role="alert"&amp;gt;,
                   &amp;lt;div&amp;gt;${message}&amp;lt;/div&amp;gt;,
                '   &lt;button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"&gt;&lt;/button&gt;',
                '&lt;/div&gt;'
                ].join('')
                alertPlaceholder.append(wrapper)
                }
                const alertTrigger = document.getElementById('liveAlertBtn')
                if (alertTrigger) {
                    alertTrigger.addEventListener('click', () =&gt; {
                    appendAlert('Amazing, this is a live alert message!', 'success')
                    })
                    }
            &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The following Javascript is used to trigger the live alert demo:

      const alertPlaceholder = document.getElementById('liveAlertPlaceholder')
    
            const appendAlert = (message, type) =&gt; {
            const wrapper = document.createElement('div')
            wrapper.innerHTML = [
            ``,
               ${message},
            '   ',
            ''
            ].join('')
              alertPlaceholder.append(wrapper)
            }
            const alertTrigger = document.getElementById('liveAlertBtn')
            if (alertTrigger) {
                alertTrigger.addEventListener('click', () =&gt; {
                appendAlert('Amazing, this is a live alert message!', 'success')
                 })
                }

    Link Color

    The utility class .alert-link can be used for any alert message to instantly produce matching coloured links, as demonstrated in the example below.

    Example

    You can edit and try running this code using the Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Alerts</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-3"><h4 class="fs-2">Alert messages with links</h4><div class="alert alert-primary" role="alert">
    
                It is a primary alert with &lt;a href="https://www.tutorialspoint.com/bootstrap/bootstrap_alerts.htm" class="alert-link"&gt;tutorialspoint.com&lt;/a&gt;&lt;/div&gt;&lt;div class="alert alert-secondary" role="alert"&gt;
                It is a secondary alert with &lt;a href="https://www.tutorialspoint.com/bootstrap/bootstrap_alerts.htm" class="alert-link"&gt;tutorialspoint.com&lt;/a&gt;&lt;/div&gt;&lt;div class="alert alert-success" role="alert"&gt;
                It is a success alert with &lt;a href="https://www.tutorialspoint.com/bootstrap/bootstrap_alerts.htm" class="alert-link"&gt;tutorialspoint.com&lt;/a&gt;&lt;/div&gt;&lt;div class="alert alert-danger" role="alert"&gt;
                It is a danger alert with &lt;a href="https://www.tutorialspoint.com/bootstrap/bootstrap_alerts.htm" class="alert-link"&gt;tutorialspoint.com&lt;/a&gt;&lt;/div&gt;&lt;div class="alert alert-warning" role="alert"&gt;
                It is a warning alert with &lt;a href="https://www.tutorialspoint.com/bootstrap/bootstrap_alerts.htm" class="alert-link"&gt;tutorialspoint.com&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Additional content

    Additional HTML elements like headers, paragraphs, and dividers can also be included in alerts. Following example demonstrates the same.

    Example

    You can edit and try running this code using the Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Alerts</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-3"><h4 class="fs-2">Alert messages with additional content</h4><div class="alert alert-success" role="alert"><h4 class="alert-heading">Congratulations! Sending my best wishes.</h4><p>Congratulations on your graduation! Sending you our best wishes for a happy and successful future.</p><hr><p class="mb-0">What could be better than something sweet to celebrate an accomplishment!!!</p></div></div></body></html>

    Alerts with Icons

    For creating alerts with icons use flexbox utilities and Bootstrap Icons.https://www.tutorialspoint.com/bootstrap/examples/alerts_icon.php

    Example

    You can edit and try running this code using the Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Alerts</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-3"><h4 class="fs-2">Alert messages with icons</h4><svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><symbol id="success-bg" viewBox="0 0 16 16"><path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-3.97-3.03a.75.75 0 0 0-1.08.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-.01-1.05z"/></symbol><symbol id="info-bg" viewBox="0 0 16 16"><path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zm.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2z"/></symbol><symbol id="warning-bg" viewBox="0 0 16 16"><path d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5zm.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2z"/></symbol></svg><div class="alert alert-primary d-flex align-items-center" role="alert"><svg class="bi flex-shrink-0 me-2" role="img" aria-label="Info:"><use xlink:href="#info-bg"/></svg><div>
    
                  It is an info alert with an info icon.
                &lt;/div&gt;&lt;/div&gt;&lt;div class="alert alert-success d-flex align-items-center" role="alert"&gt;&lt;svg class="bi flex-shrink-0 me-2" role="img" aria-label="Success:"&gt;&lt;use xlink:href="#success-bg"/&gt;&lt;/svg&gt;&lt;div&gt;
                  It is a success alert with a success icon.
                &lt;/div&gt;&lt;/div&gt;&lt;div class="alert alert-warning d-flex align-items-center" role="alert"&gt;&lt;svg class="bi flex-shrink-0 me-2" role="img" aria-label="Warning:"&gt;&lt;use xlink:href="#warning-bg"/&gt;&lt;/svg&gt;&lt;div&gt;
                  It is a warning alert with a warning icon.
                &lt;/div&gt;&lt;/div&gt;&lt;div class="alert alert-danger d-flex align-items-center" role="alert"&gt;&lt;svg class="bi flex-shrink-0 me-2" role="img" aria-label="Danger:"&gt;&lt;use xlink:href="#warning-bg"/&gt;&lt;/svg&gt;&lt;div&gt;
                  It is a danger alert with a danger icon.
                &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Dismissing Alerts

    Any alert can be dismissed inline by using the alert JavaScript plugin. Refer the following points:

    • Make sure the built-in Bootstrap JavaScript or the alert plugin is loaded.
    • Add a close button and the .alert-dismissible class, which places the close button and provides extra padding to the right of the alert.
    • The JavaScript feature can be turned on by adding the data-bs-dismiss="alert" attribute to the close button.

    Let us see this feature in the following example:

    Example

    You can edit and try running this code using the Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Alerts</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-3"><h4 class="fs-2">Dismissal of alerts</h4><div class="alert alert-primary alert-dismissible" role="alert"><strong>Primary Alert Box!</strong> Click on close icon to dismiss the alert box.
    
                &lt;button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"&gt;&lt;/button&gt;&lt;/div&gt;&lt;div class="alert alert-success alert-dismissible" role="alert"&gt;&lt;strong&gt;Success Alert Box!&lt;/strong&gt; Click on close icon to dismiss the alert box.
                &lt;button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"&gt;&lt;/button&gt;&lt;/div&gt;&lt;div class="alert alert-warning alert-dismissible" role="alert"&gt;&lt;strong&gt;Warning Alert Box!&lt;/strong&gt; Click on close icon to dismiss the alert box.
                &lt;button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Make sure to add tabindex="-1" to the element, when you are setting the focus on a non-interactive element that usually does not receive the focus, after closing an alert message. As, not doing so, might lose the focus and reset it to the start of the page, after an alert message is closed.

    Animated Alerts

    The utility classes .fade and .show create the animation effect, when you close an alert message. Let us see this feature in the following example.

    Example

    You can edit and try running this code using the Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Alerts</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script></head><body><div class="container mt-3"><h4 class="fs-2">Alert messages with animation</h4><div class="alert alert-info alert-dismissible fade show" role="alert"><strong>Primary Alert Box!</strong> This will close the alert box with fading effect.
    
                &lt;button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"&gt;&lt;/button&gt;&lt;/div&gt;&lt;div class="alert alert-success alert-dismissible fade show" role="alert"&gt;&lt;strong&gt;Success Alert Box!&lt;/strong&gt; This will close the alert box with fading effect.
                &lt;button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"&gt;&lt;/button&gt;&lt;/div&gt;&lt;div class="alert alert-warning alert-dismissible fade show" role="alert"&gt;&lt;strong&gt;Warning Alert Box!&lt;/strong&gt; This will close the alert box with fading effect.
                &lt;button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"&gt;&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Accordion

    This chapter will discuss about Bootstrap accordion. The accordion collapses elements vertically, shows and hides through class changes. Use the collapse JavaScript plugin to create accordions that collapse vertically.

    How it works

    In order to collapse, the accordion uses collapse internally. Use .open class to .accordion element to render an expanded accordion.

    Basic Example

    To expand and collapsethe content, click on the accordions below.https://www.tutorialspoint.com/bootstrap/examples/basic_example.php

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Accordion</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><style></style></head><body><div class="accordion" id="basicAccordion"><div class="accordion-item"><h2 class="accordion-header"><button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#firstCollapse" aria-expanded="true" aria-controls="firstCollapse">
    
             Bootstrap - Accordian
            &lt;/button&gt;&lt;/h2&gt;&lt;div id="firstCollapse" class="accordion-collapse collapse show" data-bs-parent="#basicAccordion"&gt;&lt;div class="accordion-body"&gt;
              The &lt;b&gt;accordion&lt;/b&gt; collapses elements vertically, shown and hidden by class change.
            &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="accordion-item"&gt;&lt;h2 class="accordion-header"&gt;&lt;button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#secondCollapse" aria-expanded="false" aria-controls="secondCollapse"&gt;
              Bootstrap - Buttons
            &lt;/button&gt;&lt;/h2&gt;&lt;div id="secondCollapse" class="accordion-collapse collapse" data-bs-parent="#basicAccordion"&gt;&lt;div class="accordion-body"&gt;
              You can use Bootstraps custom button styles for actions in forms, dialogs and many more.        
            &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Flush

    To render edge-to-edge accordions, use .accordion-flush to remove borders and rounded corners.

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Accordion</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><style></style></head><body><div class="accordion accordion-flush" id="flushAccordion"><div class="accordion-item"><h2 class="accordion-header"><button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
    
              data-bs-target="#flushCollapseOne" aria-expanded="false" aria-controls="flushCollapseOne"&gt;
              Bootstrap - Accordian
            &lt;/button&gt;&lt;/h2&gt;&lt;div id="flushCollapseOne" class="accordion-collapse collapse" data-bs-parent="#flushAccordion"&gt;&lt;div class="accordion-body"&gt;
              The &lt;b&gt;accordion&lt;/b&gt; collapses elements vertically, shown and hidden by class change.
            &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="accordion-item"&gt;&lt;h2 class="accordion-header"&gt;&lt;button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
              data-bs-target="#flushCollapseTwo" aria-expanded="false" aria-controls="flushCollapseTwo"&gt;
              Bootstrap - Buttons
            &lt;/button&gt;&lt;/h2&gt;&lt;div id="flushCollapseTwo" class="accordion-collapse collapse" data-bs-parent="#flushAccordion"&gt;&lt;div class="accordion-body"&gt;
              You can use Bootstraps custom button styles for actions in forms, dialogs and many more.
            &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Always open

    To keep accordion items open while another item is opened, remove data-bs-parent attribute from each .accordion-collapse.

    You can edit and try running this code using Edit & Run option.

    <!DOCTYPE html><html lang="en"><head><title>Bootstrap - Accordion</title><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script><style></style></head><body><div class="accordion" id="alwaysOpenAccordion"><div class="accordion-item"><h2 class="accordion-header"><button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#firstCollpaseofAlwaysOpen" aria-expanded="true" aria-controls="firstCollpaseofAlwaysOpen">
    
              Bootstrap - Accordian
            &lt;/button&gt;&lt;/h2&gt;&lt;div id="firstCollpaseofAlwaysOpen" class="accordion-collapse collapse show"&gt;&lt;div class="accordion-body"&gt;
              The &lt;b&gt;accordion&lt;/b&gt; collapses elements vertically, shown and hidden by class change.
            &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="accordion-item"&gt;&lt;h2 class="accordion-header"&gt;&lt;button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#secondCollpaseofAlwaysOpen" aria-expanded="false" aria-controls="secondCollpaseofAlwaysOpen"&gt;
              Bootstrap - Buttons
            &lt;/button&gt;&lt;/h2&gt;&lt;div id="secondCollpaseofAlwaysOpen" class="accordion-collapse collapse"&gt;&lt;div class="accordion-body"&gt;
              The &lt;b&gt;accordion&lt;/b&gt; collapses elements vertically, shown and hidden by class change.
            &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Accessibility

    See collapse accessibility section for details.