Day 5 Bootstrap 5 Components (part-1)
Tags: bootstrap, tutorial
Published: 2022-08-30 03:10:00



In this tutorial we will learn about components. All our components responsively and with base and modifier classes.

###Accordion

The accordion uses collapse internally to make it collapsible. To render an accordion that’s expanded, add the .open class on the .accordion.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ztss3dzhrvi6s70tkyi.PNG)

```
 <div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        Accordion Item #1
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingTwo">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
        Accordion Item #2
      </button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingThree">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
        Accordion Item #3
      </button>
    </h2>
    <div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>
```


###Flush
 
Add .accordion-flush to remove the default background-color, some borders, and some rounded corners to render accordions edge-to-edge with their parent container. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzoqe62ykuic7kx0bvym.png)
```
<div class="accordion accordion-flush" id="accordionFlushExample">
  <div class="accordion-item">
    <h2 class="accordion-header" id="flush-headingOne">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseOne" aria-expanded="false" aria-controls="flush-collapseOne">
        Accordion Item #1
      </button>
    </h2>
    <div id="flush-collapseOne" class="accordion-collapse collapse" aria-labelledby="flush-headingOne" data-bs-parent="#accordionFlushExample">
      <div class="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> class. This is the first item's accordion body.</div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="flush-headingTwo">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwo" aria-expanded="false" aria-controls="flush-collapseTwo">
        Accordion Item #2
      </button>
    </h2>
    <div id="flush-collapseTwo" class="accordion-collapse collapse" aria-labelledby="flush-headingTwo" data-bs-parent="#accordionFlushExample">
      <div class="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> class. This is the second item's accordion body. Let's imagine this being filled with some actual content.</div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="flush-headingThree">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseThree" aria-expanded="false" aria-controls="flush-collapseThree">
        Accordion Item #3
      </button>
    </h2>
    <div id="flush-collapseThree" class="accordion-collapse collapse" aria-labelledby="flush-headingThree" data-bs-parent="#accordionFlushExample">
      <div class="accordion-body">Placeholder content for this accordion, which is intended to demonstrate the <code>.accordion-flush</code> class. This is the third item's accordion body. Nothing more exciting happening here in terms of content, but just filling up the space to make it look, at least at first glance, a bit more representative of how this would look in a real-world application.</div>
    </div>
  </div>
</div>
```


###Alerts

Alerts provide contextual feedback messages for typical user actions with a handful of responsible and flexible alert boxes.
Provide contextual feedback messages for typical user actions with the handful of available and flexible alert messages.

**Examples**

Alerts are available for any length of text, as well as an optional close button. For proper styling, use one of the eight required contextual classes (e.g., .alert-success). For inline dismissal, use the alerts JavaScript plugin.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7vechur55u3bimer3qi.png)

```
  <div class="alert alert-primary" role="alert">
  A simple primary alert—check it out!
</div>
<div class="alert alert-secondary" role="alert">
  A simple secondary alert—check it out!
</div>
<div class="alert alert-success" role="alert">
  A simple success alert—check it out!
</div>
<div class="alert alert-danger" role="alert">
  A simple danger alert—check it out!
</div>
<div class="alert alert-warning" role="alert">
  A simple warning alert—check it out!
</div>
<div class="alert alert-info" role="alert">
  A simple info alert—check it out!
</div>
<div class="alert alert-light" role="alert">
  A simple light alert—check it out!
</div>
<div class="alert alert-dark" role="alert">
  A simple dark alert—check it out!
</div>
```

**Link color**

Use the .alert-link utility class to quickly provide matching colored links within any alert.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st36r0uihm8m7xpo4182.png)

```
<div class="alert alert-primary" role="alert">
  A simple primary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-secondary" role="alert">
  A simple secondary alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-success" role="alert">
  A simple success alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-danger" role="alert">
  A simple danger alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-warning" role="alert">
  A simple warning alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-info" role="alert">
  A simple info alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-light" role="alert">
  A simple light alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
<div class="alert alert-dark" role="alert">
  A simple dark alert with <a href="#" class="alert-link">an example link</a>. Give it a click if you like.
</div>
```

**Dismissing**

Using the alert JavaScript plugin, it’s possible to dismiss any alert inline. Here’s how:

•	Be sure you’ve loaded the alert plugin, or the compiled Bootstrap JavaScript.

•	Add a close button and the .alert-dismissible class, which adds extra padding to the right of the alert and positions the close button.

•	On the close button, add the data-bs-dismiss="alert"  attribute, which triggers the JavaScript functionality. Be sure to use the button element with it for proper behavior across all devices.

•	To animate alerts when dismissing them, be sure to add the .fade and .show classes.

**Badges**
 
Badges add extra information like count or label to any content. Use counters, icons, or labels.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyjhr95n3c6gyxvfy9pq.png) 
```
<h2>Example heading <span class="badge bg-primary">New</span></h2>
```

**Sizes**

Badges scale to match the size of the immediate parent element by using relative font sizing and em units. As of v5, badges no longer have focus or hover styles for links.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eioz28sofv9vfjq7rui3.png)
```
<h1>Example heading <span class="badge bg-primary">New</span></h1>
<h2>Example heading <span class="badge bg-primary">New</span></h2>
<h3>Example heading <span class="badge bg-primary">New</span></h3>
<h4>Example heading <span class="badge bg-primary">New</span></h4>
<h5>Example heading <span class="badge bg-primary">New</span></h5>
<h6>Example heading <span class="badge bg-primary">New</span></h6>
```

**Button**

Badges can be used as part of links or buttons to provide a counter.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdbed6hpvo9kja559cvb.png)

```
<button type="button" class="btn btn-primary">
  Notifications<span class="badge badge text-bg-secondary ms-2">4</span>
</button>
```

**Colors**

Use our background utility classes to quickly change the appearance of a badge. Please note that when using Bootstrap’s default .bg-light, you’ll likely need a text color utility like .text-dark for proper styling. This is because background utilities do not set anything but background-color.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cyg5q2r07x9u503qyjg.png)

```
<span class="badge bg-primary">Primary</span>
<span class="badge bg-secondary">Secondary</span>
<span class="badge bg-success">Success</span>
<span class="badge bg-danger">Danger</span>
<span class="badge bg-warning text-dark">Warning</span>
<span class="badge bg-info">Info</span>
<span class="badge bg-light text-dark">Light</span>
<span class="badge bg-dark">Dark</span>
```

**Pills**

Use the .rounded-pill utility class to make badges more rounded with a larger border-radius.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/388n64f5ck3duupk6dfj.png)

```
<span class="badge rounded-pill bg-primary">Primary</span>
<span class="badge rounded-pill bg-secondary">Secondary</span>
<span class="badge rounded-pill bg-success">Success</span>
<span class="badge rounded-pill bg-danger">Danger</span>
<span class="badge rounded-pill bg-warning text-dark">Warning</span>
<span class="badge rounded-pill bg-info">Info</span>
<span class="badge rounded-pill bg-light text-dark">Light</span>
<span class="badge rounded-pill bg-dark">Dark</span>
```


**Positioned**
Use utilities to modify a .badge and position it in the corner of a link or button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cl0do2aegg80tj4hzaq.png)
 
```
<button type="button" class="btn btn-primary position-relative">
  Inbox
  <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
    99+
    <span class="visually-hidden">unread messages</span>
  </span>
</button>

```
You can also replace the .badge class with a few more utilities without a count for a more generic indicator.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8szkum3aw5nvisksbvp.png)

```
<button type="button" class="btn btn-primary position-relative">
  Profile
  <span class="position-absolute top-0 start-100 translate-middle p-2 bg-danger border border-light rounded-circle">
    <span class="visually-hidden">New alerts</span>
  </span>
</button>
```


###Buttons
Buttons provide predefined styles (warning, info, danger) for multiple button types: outline, rounded, social, floating, fixed, tags, etc.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/blnh23iuyy9ajauokdoq.png)
```
<button type="button" class="btn btn-primary">Button</button>
```

**Colors**

It includes several predefined button styles, each serving its own semantic purpose, with a few extras thrown in for more control.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3eedp15harn5xyq2yjkz.png)
```
<button type="button" class="btn btn-primary">Primary</button>
<button type="button" class="btn btn-secondary">Secondary</button>
<button type="button" class="btn btn-success">Success</button>
<button type="button" class="btn btn-danger">Danger</button>
<button type="button" class="btn btn-warning">Warning</button>
<button type="button" class="btn btn-info">Info</button>
<button type="button" class="btn btn-light">Light</button>
<button type="button" class="btn btn-dark">Dark</button>
<button type="button" class="btn btn-link">Link</button>
```

**Outline**

In need of a button, but not the hefty background colors they bring? Replace the default modifier classes with the .btn-outline-* ones to remove all background images and colors on any button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihfz3yxvwno79l255cgi.png)
```
<button type="button" class="btn btn-outline-primary">
    Primary
</button>
<button type="button" class="btn btn-outline-secondary">
    Secondary
</button>
<button type="button" class="btn btn-outline-success">
    Success
</button>
<button type="button" class="btn btn-outline-danger">
    Danger
</button>
<button type="button" class="btn btn-outline-warning">
    Warning
</button>
<button type="button" class="btn btn-outline-info">
    Info
</button>
<button type="button" class="btn btn-outline-dark">
    Dark
</button>
```

**Sizes**

Fancy larger or smaller buttons? Add .btn-lg or .btn-sm for additional sizes.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67oe9woxz8umf7ndhr09.png)

```
<button type="button" class="btn btn-primary btn-sm">Button</button>
<button type="button" class="btn btn-primary">Button</button>
<button type="button" class="btn btn-primary btn-lg">Button</button>
```

**Disabled state**

Make buttons look inactive by adding the disabled boolean attribute to any <button> element. Disabled buttons have pointer-events: none applied to, preventing hover and active states from triggering.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1s4968lqavw449qzneq6.png) 
```
<button type="button" class="btn btn-primary" disabled>Primary button</button>
<button type="button" class="btn btn-secondary" disabled>Button</button>
<button type="button" class="btn btn-outline-primary" disabled>Primary button</button>
<button type="button" class="btn btn-outline-secondary" disabled>Button</button>
```

Disabled buttons using the `<a>` element behave a bit different:
`<a>`s don’t support the disabled attribute, so you must add the .disabled class to make it visually appear disabled.
Some future-friendly styles are included to disable all pointer-events on anchor buttons. In browsers which support that property, you won’t see the disabled cursor at all.

Disabled buttons should include the **aria-disabled="true"** attribute to indicate the state of the element to assistive technologies.

**Block buttons**

Create responsive stacks of full-width, “block buttons” like those in Bootstrap 4 with a mix of our display and gap utilities. By using utilities instead of button specific classes, we have much greater control over spacing, alignment, and responsive behaviors.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4k0k1n5mwvor4ff4i2r.png)
```
<div class="d-grid gap-2">
  <button class="btn btn-primary" type="button">Button</button>
  <button class="btn btn-primary" type="button">Button</button>
</div>
```

Here we create a responsive variation, starting with vertically stacked buttons until the md breakpoint, where .d-md-block replaces the .d-grid class, thus nullifying the gap-2 utility. 

Resize your browser to see them change.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3yw8smantsx8ugg2mqc8.png)
```
<div class="d-grid gap-2 d-md-block">
  <button class="btn btn-primary" type="button">Button</button>
  <button class="btn btn-primary" type="button">Button</button>
</div>
```

You can adjust the width of your block buttons with grid column width classes. For example, for a half-width “block button”, use .col-6. Center it horizontally with .mx-auto, too.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wd1k4jk574od08hfkkm4.png)
```
<div class="d-grid gap-2 col-6 mx-auto">
  <button class="btn btn-primary" type="button">Button</button>
  <button class="btn btn-primary" type="button">Button</button>
</div>
```

Additional utilities can be used to adjust the alignment of buttons when horizontal. Here we’ve taken our previous responsive example and added some flex utilities and a margin utility on the button to right align the buttons when they’re no longer stacked.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umb7nt6z5degq28xmev8.png)
```
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
  <button class="btn btn-primary me-md-2" type="button">Button</button>
  <button class="btn btn-primary" type="button">Button</button>
</div>
```

###Toggle states

Add data-bs-toggle="button" to toggle a button’s active state. If you’re pre-toggling a button, you must manually add the .active class and aria-pressed="true" to ensure that it is conveyed appropriately to assistive technologies.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6xi9rr1spoj7ls5ciy8.png) 
```
<button type="button" class="btn btn-primary" data-bs-toggle="button">Toggle button</button>
<button type="button" class="btn btn-primary active" data-bs-toggle="button" aria-pressed="true">Active toggle button</button>
<button type="button" class="btn btn-primary" disabled data-bs-toggle="button">Disabled toggle button</button>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsx5csvh8jshfsz57vu7.png) 
```
<a href="#" class="btn btn-primary" role="button" data-bs-toggle="button">Toggle link</a>
<a href="#" class="btn btn-primary active" role="button" data-bs-toggle="button" aria-pressed="true">Active toggle link</a>
<a class="btn btn-primary disabled" aria-disabled="true" role="button" data-bs-toggle="button">Disabled toggle link</a>
```

###Button groups
 
Button group wraps a series of buttons together into a single line (navbar) or stack in a vertical column. 

Group a series of buttons together on a single line with the button group.

Basic example
Wrap a series of buttons with .btn in .btn-group.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhiemrarko7rqgkqrmua.png) 
```
<div class="btn-group" role="group" aria-label="Basic example">
  <button type="button" class="btn btn-primary">Left</button>
  <button type="button" class="btn btn-primary">Middle</button>
  <button type="button" class="btn btn-primary">Right</button>
</div>
```

These classes can also be added to links. Use the .active class to highlight a link.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hi3peyuj29yz9w48oa9k.png) 
```
<div class="btn-group">
  <a href="#!" class="btn btn-primary active">Active link</a>
  <a href="#!" class="btn btn-primary">Link</a>
  <a href="#!" class="btn btn-primary">Link</a>
</div>
```

**Outlined styles**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5mcdcwqe9b3jdnjm3ol.png) 
```
<div class="btn-group" role="group" aria-label="Basic outlined example">
  <button type="button" class="btn btn-outline-primary">Left</button>
  <button type="button" class="btn btn-outline-primary">Middle</button>
  <button type="button" class="btn btn-outline-primary">Right</button>
</div>
```

###Toolbar

Combine sets of button groups into button toolbars for more complex components. Use utility classes as needed to space out groups, buttons, and more.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0cbms93rcotzx746hegu.png) 
```
<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
  <div class="btn-group me-2" role="group" aria-label="First group">
    <button type="button" class="btn btn-primary">1</button>
    <button type="button" class="btn btn-primary">2</button>
    <button type="button" class="btn btn-primary">3</button>
    <button type="button" class="btn btn-primary">4</button>
  </div>
  <div class="btn-group me-2" role="group" aria-label="Second group">
    <button type="button" class="btn btn-secondary">5</button>
    <button type="button" class="btn btn-secondary">6</button>
    <button type="button" class="btn btn-secondary">7</button>
  </div>
  <div class="btn-group" role="group" aria-label="Third group">
    <button type="button" class="btn btn-info">8</button>
  </div>
</div
```

###Cards

A card is a flexible and extensible content container. It includes options for headers and footers, a wide variety of content, contextual background colors, and powerful display options. Cards are built with as little markup and styles as possible, but still manage to deliver a ton of control and customization. Built with flexbox, they offer easy alignment and mix well with other Bootstrap components.
**Simple card**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/613o6rn3gmnksmlkb622.png)
```
<div class="card" style="width: 18rem;">
  <img src="..." class="card-img-top" alt="...">
  <div class="card-body">
    <h5 class="card-title">Card title</h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    <a href="#" class="btn btn-primary">Go somewhere</a>
  </div>
</div>
```

**Content types**
Cards support a wide variety of content, including images, text, list groups, links, and more. Below are examples of what’s supported

**Body**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z01umt901rlac7q01a02.png)
```
<div class="card">
  <div class="card-body">
    This is some text within a card body.
  </div>
</div>
```

**Titles, text, and links**
Card titles are used by adding `.card-title` to a `<h*>` tag. In the same way, links are added and placed next to each other by adding `.card-link` to an `<a>` tag.

Subtitles are used by adding a `.card-subtitle` to a `<h*>` tag. If the `.card-title` and the `.card-subtitle` items are placed in a `.card-body` item, the card title and subtitle are aligned nicely.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k65gweu2ttyxnaa8b439.png)

```
<div class="card" style="width: 18rem;">
  <div class="card-body">
    <h5 class="card-title">Card title</h5>
    <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
    <a href="#" class="card-link">Card link</a>
    <a href="#" class="card-link">Another link</a>
  </div>
</div>
```