Categories
Bootstrap HTML

Bootstrap 5 — Dropdown

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to manipulate dropdowns with JavaScript and add content with Bootstrap 5.

Text

We can add any text to a dropdown menu.

For instance, we can write:

<div class="btn-group">  
  <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" data-display="static">  
    button  
  </button>  
  <div class="dropdown-menu p-4 text-muted">  
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin non libero congue lectus pharetra dictum quis ut eros. Nunc ac eleifend risus. In vitae arcu tincidunt, eleifend velit id, cursus nibh. Vestibulum hendrerit urna dictum, vulputate quam et, faucibus sapien. Suspendisse in mauris non diam facilisis aliquet eget ac nisi. Donec nec elit vel ex pellentesque pellentesque. Sed commodo tellus at enim vulputate ornare. Pellentesque vel elit ut libero hendrerit dictum a in dolor. Suspendisse cursus justo nulla, ac malesuada orci pretium quis. Sed sed nunc in lacus hendrerit consequat.  
  </div>  
</div>

to add text with some padding into our dropdown menu.

Forms

Forms can also be put inside the menu.

For example, we can write:

<div class="btn-group">  
  <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" data-display="static">  
    button  
  </button>  
  <div class="dropdown-menu p-1 text-muted">  
    <form class="px-4 py-3">  
      <div class="mb-3">  
        <label for="email" class="form-label">Email</label>  
        <input type="email" class="form-control" id="email" placeholder="email">  
      </div>  
      <div class="mb-3">  
        <label for="password" class="form-label">Password</label>  
        <input type="password" class="form-control" id="password" placeholder="Password">  
      </div>  
      <div class="mb-3">  
        <div class="form-check">  
          <input type="checkbox" class="form-check-input" id="check">  
          <label class="form-check-label" for="check">  
            Remember me  
          </label>  
        </div>  
      </div>  
      <button type="submit" class="btn btn-primary">Sign in</button>  
    </form>  
  </div>  
</div>

to add a form in the menu.

We just put the form straight into the dropdown menu container.

Dropdown Options

We can change the location of the dropdown with the data-offset attribute.

For example, we can write:

<div class="btn-group">  
  <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" data-offset="10,20">  
    button  
  </button>  
  <ul class="dropdown-menu dropdown-menu-lg-left">  
    <li><button class="dropdown-item" type="button">foo</button></li>  
    <li><button class="dropdown-item" type="button">bar</button></li>        
    <li><button class="dropdown-item" type="button">baz</button></li>  
  </ul>  
</div>

to move the menu farther away from the toggle button.

The numbers are the x and y distance in pixels we want to move the menu by.

We can use the data-reference attribute to change it to the parent:

<div class="btn-group">  
  <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" data-reference="parent">  
    button  
  </button>  
  <ul class="dropdown-menu dropdown-menu-lg-left">  
    <li><button class="dropdown-item" type="button">foo</button></li>  
    <li><button class="dropdown-item" type="button">bar</button></li>        
    <li><button class="dropdown-item" type="button">baz</button></li>  
  </ul>  
</div>

JavaScript

We can call a dropdown via JavaScript.

For instance, given the following dropdown:

<div class="btn-group">  
  <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown">  
    button  
  </button>  
  <ul class="dropdown-menu dropdown-menu-lg-left">  
    <li><button class="dropdown-item" type="button">foo</button></li>  
    <li><button class="dropdown-item" type="button">bar</button></li>        
    <li><button class="dropdown-item" type="button">baz</button></li>  
  </ul>  
</div>

We can show the menu by writing:

const dropdownToggleEl = document.querySelector('.dropdown-toggle')  
const dropdownList = new bootstrap.Dropdown(dropdownToggleEl);  
dropdownList.show();

We get the dropdown by the toggle by selecting the element with the .dropdown-toggle class.

Then we pass that to the bootstrap.Dropdown constructor.

Then we call show on the returned object to open the menu.

Other methods include toggle to toggle the menu.

hide to hide the menu.

update updates the position of the dropdown.

dispose destroys the dropdown.

getInstance gets the dropdown instance.

Events

The dropdown element also emits a few events.

It emits the show.bs.dropdown event when the dropdown is showing.

shown.bs.dropdown is emitted when it’s shown.

hide.bs.dropdown is emitted when it’s hiding.

hidden.bs.dropdown is emitted when it’s hidden.

We can listen to it with the addEventListener method:

const myDropdown = document.getElementById('myDropdown')  
myDropdown.addEventListener('show.bs.dropdown', () => {  
  // do something...  
})

Conclusion

We can add forms and text into a dropdown menu.

Also, we can listen to the events it emits and manipulates it with JavaScript.

Categories
Bootstrap HTML

Bootstrap 5 — Customize Form Layouts

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to add layouts to forms with Bootstrap 5.

Horizontal Form Label Sizing

We can change the sizing of form fields with the col-form-label-sm and col-form-label-lg classes.

They can be applied to the form labels and legends.

The size should match the size of the form control, which can be set with the form-control-lg and form-control-sm classes.

For example, we can write:

<div class="row mb-3">
  <label for="small" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
  <div class="col-sm-10">
    <input type="email" class="form-control form-control-sm" id="small" placeholder="small">
  </div>
</div>

<div class="row mb-3">
  <label for="medium" class="col-sm-2 col-form-label">Email</label>
  <div class="col-sm-10">
    <input type="email" class="form-control" id="medium" placeholder="medium">
  </div>
</div>

<div class="row">
  <label for="large" class="col-sm-2 col-form-label col-form-label-lg">Email</label>
  <div class="col-sm-10">
    <input type="email" class="form-control form-control-lg" id="large" placeholder="large">
  </div>
</div>

Column Sizing

The columns can be sized with the .col and .row classes.

For example, we can write:

<div class="row g-3">
  <div class="col-sm-6">
    <input type="text" class="form-control" placeholder="City">
  </div>

 <div class="col-sm">
    <input type="text" class="form-control" placeholder="Region">
  </div>

 <div class="col-sm">
    <input type="text" class="form-control" placeholder="Postal code">
  </div>
</div>

We add the col-sm-6 and col-sm classes to set the column widths if the breakpoint is sm or wider.

g-3 add some gutters between the input boxes.

Auto-Sizing

To change the size of form fields automatically, we can use the col and col-auto classes to size the columns.

For example, we can write:

<form class="row gy-2 gx-3 align-items-center">
  <div class="col-auto">
    <label class="sr-only" for="name">Name</label>
    <input type="text" class="form-control" id="name" placeholder="james smith">
  </div>

 <div class="col-auto">
    <label class="sr-only" for="username">Username</label>
    <div class="input-group">
      <div class="input-group-text">@</div>
      <input type="text" class="form-control" id="username" placeholder="Username">
    </div>
  </div>

 <div class="col-auto">
    <label class="sr-only" for="fruit">Fruit</label>
    <select class="form-select" id="fruit">
      <option selected>Choose...</option>
      <option value="1">apple</option>
      <option value="2">orange</option>
      <option value="3">grape</option>
    </select>
  </div>

 <div class="col-auto">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" id="agree">
      <label class="form-check-label" for="agree">
        I agree
      </label>
    </div>
  </div>

 <div class="col-auto">
    <button type="submit" class="btn btn-primary">Submit</button>
  </div>
</form>

We have multiple fields displayed side by side because we have the col-sm-3 and col-auto classes to size the columns.

They let us display the form fields side by side if we hit the sm breakpoint or wider.

Inline Forms

We can make forms inline with the .align-items-center to align the form elements to the middle.

The gutter is added with the g-3 class.

For example, we have:

`<form class="`row row-cols-md-auto g-3 align-items-center`">
  <div class="col-12">
    <label class="sr-only" for="name">Name</label>
    <input type="text" class="form-control" id="name" placeholder="james smith">
  </div>`

 <div class="col-12">
    <label class="sr-only" for="username">Username</label>
    <div class="input-group">
      <div class="input-group-text">@</div>
      <input type="text" class="form-control" id="username" placeholder="Username">
    </div>
  </div>

 <div class="col-12">
    <label class="sr-only" for="fruit">Fruit</label>
    <select class="form-select" id="fruit">
      <option selected>Choose...</option>
      <option value="1">apple</option>
      <option value="2">orange</option>
      <option value="3">grape</option>
    </select>
  </div>

 <div class="col-12">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" id="agree">
      <label class="form-check-label" for="agree">
        I agree
      </label>
    </div>
  </div>

 <div class="col-12">
    <button type="submit" class="btn btn-primary">Submit</button>
  </div>
</form>

Validation

Bootstrap 5 provides us with classes to display validation status for our form.

It scopes the styles for :invalid and :valid styles to the parent .was-validate class.

Also, it resets the appearance of the form.

The .was-validated class would be removed from the form after submission.

.is-invalid and .is-valid can be used as a fallback for pseudoclasses.

Custom Styles

If we want to show custom validation messages, we need the novalidate boolean attribute to our form.

It disables the browser default feedback tooltips but still let us use the form validation API in JavaScript.

When we try to submit, we’ll have the :invalid and :valid styles applied to the form controls.

Conclusion

We can size columns to the way we want to add layouts to forms.

Categories
Bootstrap HTML

Bootstrap 5 — Close Buttons and Collapsible Elements

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to add close buttons and collapsible elements with Bootstrap 5.

Close Button

We can add a close button with the close class.

For example, we can write:

<button type="button" class="close">
  <span>&times;</span>
</button>

Also, we can add a disabled button by writing:

<button type="button" class="close" disabled>
  <span>&times;</span>
</button>

We add the disabled attribute to disable it.

Collapse

We can toggle the visibility of the content with a few classes.

For example, we can write:

<p>
  <a class="btn btn-primary" data-toggle="collapse" href="#collapse">
    Link with href
  </a>
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapse">
    Button with data-target
  </button>
</p>

<div class="collapse" id="collapse">
  <div class="card card-body">
    Sed non tellus quis sapien pretium aliquet consequat in velit. Nulla dapibus tortor eu pellentesque porttitor. Proin dignissim mauris a libero semper laoreet. Pellentesque vestibulum porta magna eu tempus. Vivamus non dolor suscipit tellus porttitor ullamcorper ut id urna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur nibh risus, rutrum sit amet turpis eu, imperdiet aliquet ipsum.
  </div>
</div>

to add the toggle for our collapsible content and the collapsible content itself.

The toggles are in the p element.

We have the data-toggle set to collapse and the href set to the selector of the collapsible content to let it toggle the div with the collapse class.

The button is also a toggle for toggling the collapsible content.

It also has the same data-toggle attribute.

But instead of the href , we have the data-attribute with the selector for the collapsible content instead.

Multiple Targets

One toggle can toggle multiple targets.

For example, we can set the data-target attribute to a class instead of an ID of the collapsible element:

<p>
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target=".multi-collapse">Toggle both elements</button>
</p>

<div class="row">
  <div class="col">
    <div class="collapse multi-collapse" id="collapse-1">
      <div class="card card-body">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque magna dui, mattis ornare massa a, dapibus iaculis est. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam faucibus dolor at massa vestibulum, vitae tincidunt arcu fermentum. Integer vel sem velit. Vivamus nisi est, molestie a consectetur tempus, faucibus vitae arcu. Fusce ultricies viverra ligula vel auctor. Praesent pulvinar orci nulla, vel porttitor tellus ornare a. Quisque ut pellentesque odio. In quis metus turpis. Ut consectetur justo nec sapien volutpat condimentum. Cras nec lorem nec erat sodales pellentesque. Pellentesque iaculis quis leo sed auctor. Sed vestibulum est quis lacus faucibus, et sollicitudin nulla laoreet.
      </div>
    </div>
  </div>

  <div class="col">
    <div class="collapse multi-collapse" id="collapse-2">
      <div class="card card-body">
        Sed non tellus quis sapien pretium aliquet consequat in velit. Nulla dapibus tortor eu pellentesque porttitor. Proin dignissim mauris a libero semper laoreet. Pellentesque vestibulum porta magna eu tempus. Vivamus non dolor suscipit tellus porttitor ullamcorper ut id urna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur nibh risus, rutrum sit amet turpis eu, imperdiet aliquet ipsum.
      </div>
    </div>
  </div>
</div>

We have the data-toggle is set to collapse , and data-target is set to the .multi-collapse class to let us toggle both collapse elements.

Accordion

To add an accordion, we can use the .accordion class.

For example, we can write:

<div class="accordion" id="accordion">
  <div class="card">
    <div class="card-header" id="headingOne">
      <h2 class="mb-0">
        <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseOne">
          Collapsible 1
        </button>
      </h2>
    </div>

    <div id="collapseOne" class="collapse show" data-parent="#accordion">
      <div class="card-body">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque magna dui, mattis ornare massa a, dapibus iaculis est. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam faucibus dolor at massa vestibulum, vitae tincidunt arcu fermentum. Integer vel sem velit. Vivamus nisi est, molestie a consectetur tempus, faucibus vitae arcu. Fusce ultricies viverra ligula vel auctor. Praesent pulvinar orci nulla, vel porttitor tellus ornare a. Quisque ut pellentesque odio. In quis metus turpis. Ut consectetur justo nec sapien volutpat condimentum. Cras nec lorem nec erat sodales pellentesque. Pellentesque iaculis quis leo sed auctor. Sed vestibulum est quis lacus faucibus, et sollicitudin nulla laoreet.
      </div>
    </div>
  </div>

  <div class="card">
    <div class="card-header" id="headingTwo">
      <h2 class="mb-0">
        <button class="btn btn-link btn-block text-left collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo">
          Collapsible 2
        </button>
      </h2>
    </div>
    <div id="collapseTwo" class="collapse" data-parent="#accordion">
      <div class="card-body">
        Sed non tellus quis sapien pretium aliquet consequat in velit. Nulla dapibus tortor eu pellentesque porttitor. Proin dignissim mauris a libero semper laoreet. Pellentesque vestibulum porta magna eu tempus. Vivamus non dolor suscipit tellus porttitor ullamcorper ut id urna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur nibh risus, rutrum sit amet turpis eu, imperdiet aliquet ipsum.
      </div>
    </div>
  </div>

  <div class="card">
    <div class="card-header" id="headingThree">
      <h2 class="mb-0">
        <button class="btn btn-link btn-block text-left collapsed" type="button" data-toggle="collapse" data-target="#collapseThree">
          Collapsible 3
        </button>
      </h2>
    </div>
    <div id="collapseThree" class="collapse" data-parent="#accordion">
      <div class="card-body">
        Aliquam facilisis mauris odio, vitae volutpat mauris eleifend sit amet. Pellentesque a dolor dignissim, consectetur augue id, convallis velit. In lacinia venenatis orci maximus finibus. Cras eget consequat justo, vitae placerat velit. Nulla lacinia orci eu convallis luctus. Curabitur volutpat et dui sed condimentum. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam placerat scelerisque iaculis. Donec ultrices arcu id nibh rutrum dictum. In hac habitasse platea dictumst.
      </div>
    </div>
  </div>
</div>

We have the collapsible divs by creating the cards inside the div with the .collapse class.

And we have the headings for each item.

We add the headings with the data-toggle attribute set to collapse .

data-target set to the ID of the content div.

Conclusion

We can add the close button and collapsible elements with provided Bootstrap classes.

Categories
Bootstrap HTML

Bootstrap 5 — Carousels

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to add carousels with Bootstrap 5.

Carousel

A carousel is a slideshow component for cycling through elements.

The slides can have image or text.

If the browser supports the page visibility API, then the carousel will stop sliding if the screen isn’t visible to the user.

Slides Only

We can add a carousel with a div with the .carousel and ,slide classes.

For example, we can write:

<div class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>
    <div class="carousel-item">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>
    <div class="carousel-item">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>
  </div>
</div>

We have the div with the carousel and slide classes.

Also, we added the .data-ride attribute with the value carousel to add the carousel.

In addition, we have a div with the carousel-inner class to hold the items.

And to add the items, we add divs with the carousel-item class.

active makes it the active slide.

With Controls

We can add a carousel with previous and next controls.

We add them by adding a elements outside the slides div.

For example, we write:

<div id="carousel" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>

    <div class="carousel-item">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>

    <div class="carousel-item">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>
  </div>

  <a class="carousel-control-prev" href="#carousel" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
    <span class="sr-only">Previous</span>
  </a>

  <a class="carousel-control-next" href="#carousel" data-slide="next">
    <span class="carousel-control-next-icon"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

We have 2 a elements with the href set to the selector for the carousel.

Also, we have the data-slide attributes to indicate what kind of controls we have.

Inside it, we have the icon we can click on and the text for the screen reader.

With Indicators

We can add slide indicators in addition to the slides and controls.

To add that, we add an ol with the .carousel-indicators class.

Then in the li tag, we have the data-target attribute set to the selector of the carousel.

For example, we can write:

<div id="carousel" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carousel" data-slide-to="0" class="active"></li>
    <li data-target="#carousel" data-slide-to="1"></li>
    <li data-target="#carousel" data-slide-to="2"></li>
  </ol>

  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>

    <div class="carousel-item">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>

    <div class="carousel-item">
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">
    </div>
  </div>

  <a class="carousel-control-prev" href="#carousel" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
    <span class="sr-only">Previous</span>
  </a>

  <a class="carousel-control-next" href="#carousel" data-slide="next">
    <span class="carousel-control-next-icon"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

We have the ol element with the li s.

The data-target attribute is set to the #carousel to show the correct slide indicator.

data-slide-to has the index of the slide to slide to.

With Captions

Also, we can add captions to slides.

For example, we can write:

<div id="carousel" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carousel" data-slide-to="0" class="active"></li>
    <li data-target="#carousel" data-slide-to="1"></li>
    <li data-target="#carousel" data-slide-to="2"></li>
  </ol>

  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="http://placekitten.com/600/600" class="d-block w-100" alt="cat">
      <div class="carousel-caption d-none d-md-block">
        <h5>First slide label</h5>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque magna dui, mattis ornare massa a, dapibus iaculis est. Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
      </div>
    </div>

    <div class="carousel-item">
      <img src="http://placekitten.com/600/600" class="d-block w-100" alt="cat">
      <div class="carousel-caption d-none d-md-block">
        <h5>Second slide label</h5>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque magna dui, mattis ornare massa a, dapibus iaculis est. Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>
      </div>
    </div>

    <div class="carousel-item">
      <img src="http://placekitten.com/600/600" class="d-block w-100" alt="cat">
      <div class="carousel-caption d-none d-md-block">
        <h5>Third slide label</h5>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque magna dui, mattis ornare massa a, dapibus iaculis est. Lorem ipsum dolor sit amet, consectetur adipiscing elit. .</p>
      </div>
    </div>
  </div>

  <a class="carousel-control-prev" href="#carousel" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
    <span class="sr-only">Previous</span>
  </a>

  <a class="carousel-control-next" href="#carousel" data-slide="next">
    <span class="carousel-control-next-icon"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

to add captions to our slides.

We added divs with the .carousel-caption class to add the slide captions.

Conclusion

We can add the carousels with slides, controls, and captions.

Categories
Bootstrap HTML

Bootstrap 5 — Carousel Customization

Bootstrap 5 is in alpha when this is written and it’s subject to change.

Bootstrap is a popular UI library for any JavaScript apps.

In this article, we’ll look at how to customize carousels with Bootstrap 5.

Crossfade

We can add the .carousel-fade class to animate the slides with fade transitions.

For example, we can write:

<div id="carousel" class="carousel slide carousel-fade" data-ride="carousel">  
  <div class="carousel-inner">  
    <div class="carousel-item active">  
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">  
    </div>
<div class="carousel-item">  
  <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">  
</div>
    <div class="carousel-item">  
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">  
    </div>  
  </div>

<a class="carousel-control-prev" href="#carousel" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
<span class="sr-only">Previous</span>
</a>

  <a class="carousel-control-next" href="#carousel" data-slide="next">  
    <span class="carousel-control-next-icon"></span>  
    <span class="sr-only">Next</span>  
  </a>  
</div>
```

We add the `.carousel-fade` class to add the fade effect.

It’s not working in alpha version of Bootstrap 5 yet.

### Individual `.carousel-item` Interval

We can change the slide change duration for each slide with the `data-interval` attribute.

For example, we can write:

```
<div id="carousel" class="carousel slide" data-ride="carousel">  
  <div class="carousel-inner">  
    <div class="carousel-item active" data-interval="2000">  
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">  
    </div>
</code></pre>
<pre><code><div class="carousel-item" data-interval="3000">  
  <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">  
</div>
</code></pre>
<pre><code>    <div class="carousel-item" data-interval="4000">  
      <img src="http://placekitten.com/200/200" class="d-block w-100" alt="cat">  
    </div>  
  </div>
</code></pre>
<p><a class="carousel-control-prev" href="#carousel" data-slide="prev"><br>
<span class="carousel-control-prev-icon"></span><br>
<span class="sr-only">Previous</span><br>
</a></p>
<pre><code>  <a class="carousel-control-next" href="#carousel" data-slide="next">  
    <span class="carousel-control-next-icon"></span>  
    <span class="sr-only">Next</span>  
  </a>  
</div>
```

We have the `data-interval` attribute to set the duration that each slide is shown in milliseconds.

### JavaScript

We can create the carousel object with JavaScript and then do what we want with it.

For example, we can write:

```
const carouselEl = document.querySelector('#carousel')  
const carousel = new bootstrap.Carousel(carouselEl)
```

to create the carousel object from our carousel.

The `bootstrap.Carousel` constructor takes a DOM object with the carousel we created in HTML.

To add options, we can pass in a 2nd argument:

```
const carouselEl = document.querySelector('#carousel')  
const carousel = new bootstrap.Carousel(carouselEl, {  
  interval: 2000,  
  wrap: false  
})
```

We set the slide duration to change slides.

### Events

Carousels also emit a few events.

They emit the `slide.bs.carousel` event when the `slide` instance is invoked.

`slid.bs.carousel` is emitted when the carousel has completed the slide transition.

The event object has the `direction` property to indicate slide direction.

`relatedTarget` has the DOM element that’s slid into place.

`from` has the index of the current item.

`to` has the index of the next item.

To listen to an event, we can use `addEventListener` :

```
const carouselEl = document.querySelector('#carousel')

carouselEl.addEventListener(‘slide.bs.carousel’, () => {
// …
})


We get the carousel element and call `addEventListener` to it.

### Change Transition Duration

The duration of the transition can be changed with the `$carousel-transition` SASS variable.

For example, we can write:

transform 2s ease, opacity 1s ease-out


as its value.

### Conclusion

We can customize the transition effects of carousels.

Also, we can change the transition interval with various methods.