Blog

  • Bootstrap Media Objects

    Using the Media Object in Bootstrap

    Bootstrap media object has been discontinued from version 5. However, you can still create a layout that contains left- or right-aligned media object like images or video alongside the textual content such as blog comments, Tweets, etc. using the flex and spacing utility classes.

    Example

    <div class="d-flex">
    
    &lt;div class="flex-shrink-0"&gt;
        &lt;img src="images/avatar.svg" alt="Sample Image"&gt;
    &lt;/div&gt;
    &lt;div class="flex-grow-1 ms-3"&gt;
        &lt;h5&gt;Jhon Carter &lt;small class="text-muted"&gt;&lt;i&gt;Posted on January 10, 2021&lt;/i&gt;&lt;/small&gt;&lt;/h5&gt;
        &lt;p&gt;Excellent feature! I love it. One day I'm definitely going to put this Bootstrap component into use and I'll let you know once I do.&lt;/p&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Media Objects

    You can also create other variations of media object. Apply the image modifier classes like .rounded or .rounded-circle to the image to create rounded corner or circular image.

    Example

    <div class="d-flex">
    
    &lt;div class="flex-shrink-0"&gt;
        &lt;img src="images/avatar.svg" class="rounded-circle" alt="Sample Image"&gt;
    &lt;/div&gt;
    &lt;div class="flex-grow-1 ms-3"&gt;
        &lt;h5&gt;Jhon Carter &lt;small class="text-muted"&gt;&lt;i&gt;Posted on January 10, 2021&lt;/i&gt;&lt;/small&gt;&lt;/h5&gt;
        &lt;p&gt;Excellent feature! I love it. One day I'm definitely going to put this Bootstrap component into use and I'll let you know once I do.&lt;/p&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Rounded Media Objects

    Creating Nested Media Objects

    Media objects can also be nested inside other media object. It can be very useful for creating comment threads in a blog post. Let’s check out an example:

    Example

    <div class="d-flex">
    
    &lt;div class="flex-shrink-0"&gt;
        &lt;img src="images/avatar.svg" class="rounded-circle" alt="Sample Image"&gt;
    &lt;/div&gt;
    &lt;div class="flex-grow-1 ms-3"&gt;
        &lt;h5&gt;Jhon Carter &lt;small class="text-muted"&gt;&lt;i&gt;Posted on January 10, 2021&lt;/i&gt;&lt;/small&gt;&lt;/h5&gt;
        &lt;p&gt;Excellent feature! I love it. One day I'm definitely going to put this Bootstrap component into use and I'll let you know once I do.&lt;/p&gt;
        
        &lt;!-- Nested media object --&gt;
        &lt;div class="d-flex mt-4"&gt;
            &lt;div class="flex-shrink-0"&gt;
                &lt;img src="images/avatar.svg" class="rounded-circle" alt="Sample Image"&gt;
            &lt;/div&gt;
            &lt;div class="flex-grow-1 ms-3"&gt;
                &lt;h5&gt;Clark Kent &lt;small class="text-muted"&gt;&lt;i&gt;Posted on January 12, 2021&lt;/i&gt;&lt;/small&gt;&lt;/h5&gt;
                &lt;p&gt;Thanks, you found this component useful. Don't forget to check out other Bootstrap components as well. They're also very useful.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Nested Media Objects

    Alignment of Media Objects

    You can also change the horizontal alignment of content and media by simply tweaking the HTML code itself, as demonstrated in the following example:

    Example

    <div class="d-flex">
    
    &lt;div class="flex-grow-1 me-3"&gt;
        &lt;h5&gt;Jhon Carter &lt;small class="text-muted"&gt;&lt;i&gt;Posted on January 10, 2021&lt;/i&gt;&lt;/small&gt;&lt;/h5&gt;
        &lt;p&gt;Excellent feature! I love it. One day I'm definitely going to put this Bootstrap component into use and I'll let you know once I do.&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class="flex-shrink-0"&gt;
        &lt;img src="images/avatar.svg" alt="Sample Image"&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Media Objects Horizontal Alignment

    Besides that you can also align the images or other media at the middle or bottom of the content block using the flexbox utilities classes, for example, you can use the class .align-self-center for vertical center alignment, and the class .align-self-end for bottom alignment.

    By default, the media inside a media object is top aligned. Here’s an example:

    Example

    <!--Top aligned media-->
    <div class="d-flex">
    
    &lt;div class="flex-shrink-0"&gt;
        &lt;img src="images/avatar.svg" alt="Sample Image"&gt;
    &lt;/div&gt;
    &lt;div class="flex-grow-1 ms-3"&gt;
        &lt;h5&gt;Top aligned media &lt;small class="text-muted"&gt;&lt;i&gt;This is Default&lt;/i&gt;&lt;/small&gt;&lt;/h5&gt;
        &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit...&lt;/p&gt;
    &lt;/div&gt;
    </div> <hr>
  • Bootstrap Cards

    Using the Bootstrap Cards

    Bootstrap 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. Card replaces panel, well, and thumbnail components in old Bootstrap 3 version.

    In the following sections, you will see what you can do with the card component.

    Creating a Basic Card

    The card markup is pretty straight forward. The outer wrapper require the base class .card, whereas content can be placed inside the .card-body element. The following example will show you how to create a card with a picture, mixed with some text content and a button.

    Example

    <div class="card" style="width: 300px;">
    
    &lt;img src="images/sample.svg" class="card-img-top" alt="..."&gt;
    &lt;div class="card-body text-center"&gt;
        &lt;h5 class="card-title"&gt;Alice Liddel&lt;/h5&gt;
        &lt;p class="card-text"&gt;Alice is a freelance web designer and developer based in London. She is specialized in HTML5, CSS3, JavaScript, Bootstrap, etc.&lt;/p&gt;
        &lt;a href="#" class="btn btn-primary"&gt;View Profile&lt;/a&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card

    Note: Card doesn’t have fixed width, they’ll occupy the full width of its parent element. So, if you need a fixed width card you need to apply the width property on card yourself. Also, card have no margin by default, use spacing utility classes if needed.


    Content Types for Card Component

    The card component support a wide variety of content, including images, text, list groups, links, navs, and more. Here are the examples of what’s supported by the card.

    Body Only Card

    You can simply use .card with .card-body within, whenever you need to create a padded box.

    Example

    <div class="card">
    
    &lt;div class="card-body"&gt;This is some text within a padded box.&lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Basic Card

    Card with Titles, Text, and Links

    Further, you can also place title and links inside the card along with text, like this:

    Example

    <div class="card" style="width: 300px;">
    
    &lt;div class="card-body"&gt;
        &lt;h5 class="card-title"&gt;Eiffel Tower&lt;/h5&gt;
        &lt;h6 class="card-subtitle mb-3 text-muted"&gt;Champ de Mars, Paris, France&lt;/h6&gt;
        &lt;p class="card-text"&gt;Built in 1889 Eiffel Tower is one of the most iconic landmarks in the world.&lt;/p&gt;
        &lt;a href="#" class="card-link"&gt;View pictures&lt;/a&gt;
        &lt;a href="#" class="card-link"&gt;Discover history&lt;/a&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card with Titles, Text, and Links

    Card with Header and Footer

    You can also add header and footer within your cards using the .card-header and .card-footer class, respectively. Let’s take a look at the following example:

    Example

    <div class="card text-center">
    
    &lt;div class="card-header"&gt;Featured&lt;/div&gt;
    &lt;div class="card-body"&gt;
        &lt;h5 class="card-title"&gt;NASA Launched Solar Probe&lt;/h5&gt;
        &lt;p class="card-text"&gt;NASA launched Parker space probe in 2018 with the mission of making observations of the outer corona of the Sun. It is the first-ever mission to "touch" the Sun.&lt;/p&gt;
        &lt;a href="#" class="btn btn-primary"&gt;Know more&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class="card-footer text-muted"&gt;3 years ago&lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card with Header and Footer

    Tip: You can use text align utility classes such as .text-center and .text-end to align card’s content to the center and right end, respectively. By default they’re left aligned.

    Placing List Groups within Card

    You can also place list groups inside the card along with other content types, as shown here.

    Example

    <div class="card" style="width: 300px;">
    
    &lt;div class="card-header"&gt;Featured&lt;/div&gt;
    &lt;ul class="list-group list-group-flush"&gt;
        &lt;li class="list-group-item"&gt;An item&lt;/li&gt;
        &lt;li class="list-group-item"&gt;A second item&lt;/li&gt;
        &lt;li class="list-group-item"&gt;A third item&lt;/li&gt;
    &lt;/ul&gt;
    &lt;div class="card-body"&gt;
        &lt;a href="#" class="card-link"&gt;Add More&lt;/a&gt;
        &lt;a href="#" class="card-link"&gt;Share&lt;/a&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card with List Group

    Mix and Match Multiple Content Types within Card

    Feel free to mix and match multiple content types to create the card you need. The following example will create a fixed-width card with an image, text, list group, and hyperlinks.

    Example

    <div class="card" style="width: 300px;">
    
    &lt;img src="images/thumbnail.svg" class="w-100 border-bottom" alt="..."&gt;
    &lt;div class="card-body"&gt;
        &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
        &lt;p class="card-text"&gt;Here is some example text to make up the card's content. Replace it with your own text anytime.&lt;/p&gt;
    &lt;/div&gt;
    &lt;ul class="list-group list-group-flush"&gt;
        &lt;li class="list-group-item"&gt;An item&lt;/li&gt;
        &lt;li class="list-group-item"&gt;A second item&lt;/li&gt;
        &lt;li class="list-group-item"&gt;A third item&lt;/li&gt;
    &lt;/ul&gt;
    &lt;div class="card-body"&gt;
        &lt;a href="#" class="card-link"&gt;Card link&lt;/a&gt;
        &lt;a href="#" class="card-link"&gt;Another link&lt;/a&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card with Multiple Content Types

    Adding Navigation to Cards

    You can also add Bootstrap’s nav components such as tabs and pills to the card header.

    To add tabs navigation to a card simply place the tabs markup inside the card header, and the tabs content inside the card body. You are also required to use an additional class .card-header-tabs on the .nav element along with the class .nav-tabs for proper alignment.

    Let’s try out the following example which creates an elegant tabbed navigation.

    Example

    <div class="card text-center">
    
    &lt;div class="card-header"&gt;
        &lt;ul class="nav nav-tabs card-header-tabs"&gt;
            &lt;li class="nav-item"&gt;
                &lt;a href="#home" class="nav-link active" data-bs-toggle="tab"&gt;Home&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class="nav-item"&gt;
                &lt;a href="#profile" class="nav-link" data-bs-toggle="tab"&gt;Profile&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class="nav-item"&gt;
                &lt;a href="#messages" class="nav-link" data-bs-toggle="tab"&gt;Messages&lt;/a&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class="card-body"&gt;
        &lt;div class="tab-content"&gt;
            &lt;div class="tab-pane fade show active" id="home"&gt;
                &lt;h5 class="card-title"&gt;Home tab content&lt;/h5&gt;
                &lt;p class="card-text"&gt;Here is some example text to make up the tab's content. Replace it with your own text anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Go somewhere&lt;/a&gt;
            &lt;/div&gt;
            &lt;div class="tab-pane fade" id="profile"&gt;
                &lt;h5 class="card-title"&gt;Profile tab content&lt;/h5&gt;
                &lt;p class="card-text"&gt;Here is some example text to make up the tab's content. Replace it with your own text anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Go somewhere&lt;/a&gt;
            &lt;/div&gt;
            &lt;div class="tab-pane fade" id="messages"&gt;
                &lt;h5 class="card-title"&gt;Messages tab content&lt;/h5&gt;
                &lt;p class="card-text"&gt;Here is some example text to make up the tab's content. Replace it with your own text anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Go somewhere&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card with Tabs Navigation

    Similarly, you can add pills nav to the card by using an additional class .card-header-pills along with the class .nav-pills on the .nav element, as shown below:

    Example

    <div class="card text-center">
    
    &lt;div class="card-header"&gt;
        &lt;ul class="nav nav-pills card-header-pills"&gt;
            &lt;li class="nav-item"&gt;
                &lt;a href="#home" class="nav-link active" data-bs-toggle="tab"&gt;Home&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class="nav-item"&gt;
                &lt;a href="#profile" class="nav-link" data-bs-toggle="tab"&gt;Profile&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class="nav-item"&gt;
                &lt;a href="#messages" class="nav-link" data-bs-toggle="tab"&gt;Messages&lt;/a&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class="card-body"&gt;
        &lt;div class="tab-content"&gt;
            &lt;div class="tab-pane fade show active" id="home"&gt;
                &lt;h5 class="card-title"&gt;Home tab content&lt;/h5&gt;
                &lt;p class="card-text"&gt;Here is some example text to make up the tab's content. Replace it with your own text anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Go somewhere&lt;/a&gt;
            &lt;/div&gt;
            &lt;div class="tab-pane fade" id="profile"&gt;
                &lt;h5 class="card-title"&gt;Profile tab content&lt;/h5&gt;
                &lt;p class="card-text"&gt;Here is some example text to make up the tab's content. Replace it with your own text anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Go somewhere&lt;/a&gt;
            &lt;/div&gt;
            &lt;div class="tab-pane fade" id="messages"&gt;
                &lt;h5 class="card-title"&gt;Messages tab content&lt;/h5&gt;
                &lt;p class="card-text"&gt;Here is some example text to make up the tab's content. Replace it with your own text anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Go somewhere&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card with Pills Navigation

    Customizing the Card Styles

    There are several options available for customizing the card’s backgrounds, borders, and color.

    Customizing Background and Color

    You can simply use the background and color utility classes to change the appearance of a card. Let’s try out the following example to understand how it basically works:

    Example

    <div class="row">
    
    &lt;div class="col-6"&gt;
        &lt;div class="card text-white bg-primary mb-4"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Primary card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card text-white bg-secondary mb-4"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Secondary card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card text-white bg-success mb-4"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Success card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card text-white bg-danger mb-4"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Danger card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card text-white bg-warning mb-4"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Warning card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card text-white bg-info mb-4"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Info card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card text-white bg-dark"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Dark card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card bg-light"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Light card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card Styles

    Customizing Border and Color

    Similarly, you can customize the border and text color of any card using the text and border utility classes. Just apply these classes on the .card or its child elements, as shown below:

    Example

    <div class="row">
    
    &lt;div class="col-6"&gt;
        &lt;div class="card border-primary mb-4"&gt;
            &lt;div class="card-body text-primary"&gt;
                &lt;h5 class="card-title"&gt;Primary card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card border-secondary mb-4"&gt;
            &lt;div class="card-body text-secondary"&gt;
                &lt;h5 class="card-title"&gt;Secondary card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card border-success mb-4"&gt;
            &lt;div class="card-body text-success"&gt;
                &lt;h5 class="card-title"&gt;Success card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card border-danger mb-4"&gt;
            &lt;div class="card-body text-danger"&gt;
                &lt;h5 class="card-title"&gt;Danger card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card border-warning mb-4"&gt;
            &lt;div class="card-body text-warning"&gt;
                &lt;h5 class="card-title"&gt;Warning card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card border-info mb-4"&gt;
            &lt;div class="card-body text-info"&gt;
                &lt;h5 class="card-title"&gt;Info card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card border-dark mb-4"&gt;
            &lt;div class="card-body text-dark"&gt;
                &lt;h5 class="card-title"&gt;Dark card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="card border-light mb-4"&gt;
            &lt;div class="card-body text-muted"&gt;
                &lt;h5 class="card-title"&gt;Light card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card Styles Outline

    Card Layout Options

    In addition to styling of the cards, Bootstrap also includes a few options for laying out the series of cards. However, these layouts are not responsive yet.

    Creating the Card Groups

    You can use card groups to render cards as a single, attached element with equal width and height columns. However, cards inside a card group become horizontally stacked on extra small devices (i.e. viewport width <576px). Let’s try out an example and see how it actually works:

    Example

    <div class="card-group">
    
    &lt;div class="card"&gt;
        &lt;img src="images/thumbnail.svg" class="card-img-top" alt="..."&gt;
        &lt;div class="card-body"&gt;
            &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
            &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
        &lt;/div&gt;
        &lt;div class="card-footer"&gt;
            &lt;small class="text-muted"&gt;Last updated 5 mins ago&lt;/small&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="card"&gt;
        &lt;img src="images/thumbnail.svg" class="card-img-top" alt="..."&gt;
        &lt;div class="card-body"&gt;
            &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
            &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
        &lt;/div&gt;
        &lt;div class="card-footer"&gt;
            &lt;small class="text-muted"&gt;Last updated 5 mins ago&lt;/small&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="card"&gt;
        &lt;img src="images/thumbnail.svg" class="card-img-top" alt="..."&gt;
        &lt;div class="card-body"&gt;
            &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
            &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
        &lt;/div&gt;
        &lt;div class="card-footer"&gt;
            &lt;small class="text-muted"&gt;Last updated 5 mins ago&lt;/small&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Cards Group

    Creating the Card Grids

    You can use the Bootstrap grid system and its .row-cols-* classes to control how many grid columns (wrapped around your cards) to show per row. For example, you can use the class .row-cols-1 to show one card per row, similarly you can use the class .row-cols-md-2 to show two cards per row, from the medium breakpoint up (i.e. viewport width ≥768px).

    Example

    <div class="row row-cols-1 row-cols-md-2 g-4">
    
    &lt;div class="col"&gt;
        &lt;div class="card"&gt;
            &lt;img src="images/thumbnail.svg" class="card-img-top" alt="..."&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
            &lt;div class="card-footer"&gt;
                &lt;small class="text-muted"&gt;Last updated 5 mins ago&lt;/small&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col"&gt;
        &lt;div class="card"&gt;
            &lt;img src="images/thumbnail.svg" class="card-img-top" alt="..."&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
            &lt;div class="card-footer"&gt;
                &lt;small class="text-muted"&gt;Last updated 5 mins ago&lt;/small&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col"&gt;
        &lt;div class="card"&gt;
            &lt;img src="images/thumbnail.svg" class="card-img-top" alt="..."&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
            &lt;div class="card-footer"&gt;
                &lt;small class="text-muted"&gt;Last updated 5 mins ago&lt;/small&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col"&gt;
        &lt;div class="card"&gt;
            &lt;img src="images/thumbnail.svg" class="card-img-top" alt="..."&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
            &lt;/div&gt;
            &lt;div class="card-footer"&gt;
                &lt;small class="text-muted"&gt;Last updated 5 mins ago&lt;/small&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Cards Grid

    Creating Horizontal Cards

    You can also create horizontal cards where image and text content are placed side-by-side using a combination of grid and utility classes, as shown in the following example:

    Example

    <div class="card" style="max-width: 500px;">
    
    &lt;div class="row g-0"&gt;
        &lt;div class="col-sm-5"&gt;
            &lt;img src="images/sample.svg" class="card-img-top h-100" alt="..."&gt;
        &lt;/div&gt;
        &lt;div class="col-sm-7"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Alice Liddel&lt;/h5&gt;
                &lt;p class="card-text"&gt;Alice is a freelance web designer and developer based in London. She is specialized in HTML5, CSS3, JavaScript, Bootstrap, etc.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary stretched-link"&gt;View Profile&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Horizontal Card

    Card Image Overlays

    You can even turn an image into a card background and place the card’s text on the top it using the class .card-img-overlay in place of .card-body. Depending on the image, you may need additional styles for better adjustments. Let’s check out an example:

    Example

    <div class="card text-white" style="width: 350px;">
    
    &lt;img src="images/sample.svg" class="card-img-top" alt="..."&gt;
    &lt;div class="card-img-overlay"&gt;
        &lt;h5 class="card-title"&gt;Alice Liddel&lt;/h5&gt;
        &lt;p class="card-text"&gt;Alice is a freelance web designer and developer based in London. She is specialized in HTML5, CSS3, JavaScript, Bootstrap, etc.&lt;/p&gt;
        &lt;a href="#" class="btn btn-primary stretched-link"&gt;View Profile&lt;/a&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card Image Overlays

    Note: The card content should not be larger than the height of the image. If content is larger than the image the content will be displayed outside the image.


    Text Alignment inside Card

    You can easily change the text alignment of any card—entirely or specific parts—with the text alignment utility classes. For example, you can use the class .text-center and .text-end to align the card’s text content to the center and to the right end, respectively.

    Example

    <div class="row row-cols-1 row-cols-md-3 g-3">
    
    &lt;div class="col"&gt;
        &lt;!-- Card with default left text alignment --&gt;
        &lt;div class="card"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Know more&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col"&gt;
        &lt;!-- Card with center text alignment --&gt;
        &lt;div class="card text-center"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Know more&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col"&gt;
    &lt;!-- Card with right text alignment --&gt;
        &lt;div class="card text-end"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Know more&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Card Text Alignment

    Specifying Card Size

    Cards have no specific width, they are 100% wide by default. However, you can change this as needed with custom CSS, grid classes, or sizing utility classes.

    Let’s try out the following example to understand how it basically works:

    Example

    <!-- Card sizing using grid markup -->
    <div class="row">
    
    &lt;div class="col-sm-6"&gt;
        &lt;div class="card"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Know more&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-sm-6"&gt;
        &lt;div class="card"&gt;
            &lt;div class="card-body"&gt;
                &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
                &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
                &lt;a href="#" class="btn btn-primary"&gt;Know more&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div> <!-- Card sizing using sizing utility classes --> <div class="card w-75">
    &lt;div class="card-body"&gt;
        &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
        &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
        &lt;a href="#" class="btn btn-primary"&gt;Know more&lt;/a&gt;
    &lt;/div&gt;
    </div> <!-- Card sizing using sizing utility classes --> <div class="card" style="width: 15rem;">
    &lt;div class="card-body"&gt;
        &lt;h5 class="card-title"&gt;Card title&lt;/h5&gt;
        &lt;p class="card-text"&gt;Some dummy text to make up the card's content. You can replace it anytime.&lt;/p&gt;
        &lt;a href="#" class="btn btn-primary"&gt;Know more&lt;/a&gt;
    &lt;/div&gt;
    </div>

    Card with Stretched Link

    You can add the class .stretched-link to a link inside the card to make the whole card clickable (i.e. whole card act like a link). Multiple links are not recommended with stretched links.

    Try out the following example to see how this actually works:

    Example

    <div class="card" style="width: 300px;">
    
    &lt;img src="images/sample.svg" class="card-img-top" alt="..."&gt;
    &lt;div class="card-body text-center"&gt;
        &lt;h5 class="card-title"&gt;Alice Liddel&lt;/h5&gt;
        &lt;p class="card-text"&gt;Alice is a freelance web designer and developer based in London. She is specialized in HTML5, CSS3, JavaScript, Bootstrap, etc.&lt;/p&gt;
        &lt;a href="#" class="btn btn-primary stretched-link"&gt;View Profile&lt;/a&gt;
    &lt;/div&gt;
    </div>
  • Bootstrap Images

    Styling Images with Bootstrap

    Images are very common in modern web design. So styling images and placing it properly on the web pages is very important for improving the user experience.

    Using the Bootstrap built-in classes you can easily style images such as making the round cornered or circular images, or give them effect like thumbnails.

    Example

    <img src="images/avatar.svg" class="rounded" alt="Rounded Image">
    <img src="images/avatar.svg" class="rounded-circle" alt="Circular Image">
    <img src="images/avatar.svg" class="img-thumbnail" alt="Thumbnail Image">

    — The output of the above example will look something like this:

    Bootstrap Image Styling

    Creating Responsive Images and Videos

    With Bootstrap you can make the images responsive too. Just add the class .img-fluid to the <img> tag. This class mainly applies the styles max-width: 100%; and height: auto; to the image so that it scales nicely to fit the containing element — in case if the width of the image is larger than the containing element itself. Check out the following example to see how it works:

    Example

    <img src="images/kites.jpg" class="img-fluid" alt="Flying Kites">
    <img src="images/sky.jpg" class="img-fluid" alt="Cloudy Sky">
    <img src="images/balloons.jpg" class="img-fluid" alt="Hot Air Balloons">

    Note: When making the responsive layouts consider making your images responsive too, otherwise if an image width is larger than the parent element’s width in any case, it will overflow and may break your web page layout.

    You can also make the video or slideshow embedded in a web page responsive without affecting its original aspect ratio. To do this wrap any embed like an <iframe>, or <video> in a <div> element and apply the class .embed-responsive, and an aspect ratio class such as .embed-responsive-16by9.

    You can optionally apply an explicit descendant class .embed-responsive-item on the embedded element to match the styling for other attributes. Here’s is an example:

    Example

    <!-- 21:9 aspect ratio -->
    <div class="ratio ratio-21x9">
    
    &lt;iframe src="//www.youtube.com/embed/YE7VzlLtp-4" title="YouTube video" allowfullscreen&gt;&lt;/iframe&gt;
    </div> <!-- 16:9 aspect ratio --> <div class="ratio ratio-16x9">
    &lt;iframe src="//www.youtube.com/embed/YE7VzlLtp-4" title="YouTube video" allowfullscreen&gt;&lt;/iframe&gt;
    </div> <!-- 4:3 aspect ratio --> <div class="ratio ratio-4x3">
    &lt;iframe src="//www.youtube.com/embed/YE7VzlLtp-4" title="YouTube video" allowfullscreen&gt;&lt;/iframe&gt;
    </div> <!-- 1:1 aspect ratio --> <div class="ratio ratio-1x1">
    &lt;iframe src="//www.youtube.com/embed/YE7VzlLtp-4" title="YouTube video" allowfullscreen&gt;&lt;/iframe&gt;
    </div>

    In the above example, we’ve created the 4 responsive embedded videos with 4 different aspect ratios (21:916:94:3, and 1:1) by using the classes .ratio-21x9.ratio-16x9.ratio-4x3, and .ratio-1x1, respectively in addition to the base class .ratio on the wrapper element.

    Tip: The aspect ratio of an image describes the proportional relationship between its width and its height. Two common videographic aspect ratios are 16:9 and 4:3.


    Horizontal Alignment of Images

    You can use the text alignment classes such as .text-center, and .text-end on the parent container to align the inline images horizontally center and right. You can also align images to the left and right within a larger box using the classes .float-start and .float-end.

    However, to center align the block-level images you need to use the .mx-auto margin utility class. Let’s try out the following example to understand how it really works:

    Example

    <!-- Center alignment using text alignment classes -->
    <div class="text-center">
    
    &lt;img src="images/avatar.svg" alt="Sample Image"&gt;
    </div> <!-- Right alignment using text alignment classes --> <div class="text-end">
    &lt;img src="images/avatar.svg" alt="Sample Image"&gt;
    </div> <!-- Horizontal alignment using float classes --> <div class="clearfix">
    &lt;img src="images/avatar.svg" class="float-start" alt="Sample Image"&gt;
    &lt;img src="images/avatar.svg" class="float-end" alt="Sample Image"&gt;
    </div> <!-- Center alignment of block-level image using auto margin --> <div>
    &lt;img src="images/avatar.svg" class="d-block mx-auto" alt="Sample Image"&gt;
    </div>
  • Bootstrap Button Groups

    Creating Button Groups with Bootstrap

    In the previous chapter you’ve learnt how to create different types of individual buttons and modify them with predefined classes. Bootstrap however, also allows you to group a series of buttons together in a single line through the button group component.

    To create a button group just wrap a series of buttons with .btn class in a <div> element and apply the class .btn-group on it. You can additionally apply the class .active on an individual button to indicate the active state. Let’s take a look at the following example:

    Example

    <div class="btn-group">
    
    &lt;button type="button" class="btn btn-primary"&gt;Home&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary active"&gt;About&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;Services&lt;/button&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Button Groups

    Similarly, you can also create button groups using outline buttons, like this:

    Example

    <div class="btn-group">
    
    &lt;button type="button" class="btn btn-outline-primary"&gt;Home&lt;/button&gt;
    &lt;button type="button" class="btn btn-outline-primary active"&gt;About&lt;/button&gt;
    &lt;button type="button" class="btn btn-outline-primary"&gt;Services&lt;/button&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Outline Button Groups

    See the tutorial on Bootstrap stateful buttons to learn how to enable radio buttons and checkboxes like toggling on button groups without using any JavaScript code.


    Mixed Styles Button Groups

    You can also mix and match different button styles to create button groups like this:

    Example

    <div class="btn-group">
    
    &lt;button type="button" class="btn btn-success"&gt;
        &lt;i class="bi-eye"&gt;&lt;/i&gt; View
    &lt;/button&gt;
    &lt;button type="button" class="btn btn-warning"&gt;
        &lt;i class="bi-pencil"&gt;&lt;/i&gt; Edit
    &lt;/button&gt;
    &lt;button type="button" class="btn btn-danger"&gt;
        &lt;i class="bi-trash"&gt;&lt;/i&gt; Delete
    &lt;/button&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Button Groups Mixed Styles

    Creating Button Toolbar

    You can also combine sets of button groups together for creating more complex components like button toolbar. To create button toolbar just wrap sets of button groups in a <div> element and apply the class .btn-toolbar on it, as shown in the following example:

    Example

    <div class="btn-toolbar">
    
    &lt;div class="btn-group me-2"&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-fonts"&gt;&lt;/i&gt;
        &lt;/button&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-type-bold"&gt;&lt;/i&gt;
        &lt;/button&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-type-italic"&gt;&lt;/i&gt;
        &lt;/button&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-type-underline"&gt;&lt;/i&gt;
        &lt;/button&gt;
    &lt;/div&gt;
    &lt;div class="btn-group me-2"&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-text-left"&gt;&lt;/i&gt;
        &lt;/button&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-text-center"&gt;&lt;/i&gt;
        &lt;/button&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-text-right"&gt;&lt;/i&gt;
        &lt;/button&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-justify"&gt;&lt;/i&gt;
        &lt;/button&gt;
    &lt;/div&gt;
    &lt;div class="btn-group"&gt;
        &lt;button type="button" class="btn btn-primary"&gt;
            &lt;i class="bi-code"&gt;&lt;/i&gt;
        &lt;/button&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Button Toolbar

    Height Sizing of Button Groups

    Instead of applying button sizing classes to each button in a group, you can simply apply button group sizing classes like .btn-group-lg or .btn-group-sm directly to each .btn-group element to create larger or smaller button groups, as shown in the example below:

    Example

    <!-- Large button group -->
    <div class="btn-group mb-2 btn-group-lg">
    
    &lt;button type="button" class="btn btn-primary"&gt;Home&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;About&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;Services&lt;/button&gt;
    </div> <br> <!-- Default button group --> <div class="btn-group mb-2">
    &lt;button type="button" class="btn btn-primary"&gt;Home&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;About&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;Services&lt;/button&gt;
    </div> <br> <!-- Small button group --> <div class="btn-group btn-group-sm">
    &lt;button type="button" class="btn btn-primary"&gt;Home&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;About&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;Services&lt;/button&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Button Groups Height Sizing

    Nesting Button Groups

    Button groups can also be nested. The following example demonstrates how to place a .btn-group within another .btn-group to create dropdown menus inside button groups.

    Example

    <div class="btn-group">
    
    &lt;a href="#" class="btn btn-primary"&gt;Home&lt;/a&gt;
    &lt;a href="#" class="btn btn-primary"&gt;About&lt;/a&gt;
    &lt;div class="btn-group"&gt;
        &lt;a href="#" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown"&gt;Services&lt;/a&gt;
        &lt;div class="dropdown-menu"&gt;
            &lt;a class="dropdown-item" href="#"&gt;Web Design&lt;/a&gt;
            &lt;a class="dropdown-item" href="#"&gt;Web Development&lt;/a&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Button Groups with Nested Dropdown

    You will learn about Bootstrap dropdowns in detail in the advanced section.


    Vertically Stacked Button Groups

    You can also make the button groups appear vertically stacked rather than horizontally. To do this just replace the class .btn-group with the class .btn-group-vertical, like this:

    Example

    <div class="btn-group-vertical">
    
    &lt;a href="#" class="btn btn-primary"&gt;Home&lt;/a&gt;
    &lt;a href="#" class="btn btn-primary"&gt;About&lt;/a&gt;
    &lt;div class="btn-group"&gt;
        &lt;a href="#" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown"&gt;Services&lt;/a&gt;
        &lt;div class="dropdown-menu"&gt;
            &lt;a class="dropdown-item" href="#"&gt;Web Design&lt;/a&gt;
            &lt;a class="dropdown-item" href="#"&gt;Web Development&lt;/a&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Vertical Button Groups

    Creating Justified Button Groups

    You can also stretch your button groups to fill all the available width by applying the flex utility class .d-flex to the .btn-group element. Every button has equal width in this case.

    Example

    <div class="btn-group d-flex">
    
    &lt;button type="button" class="btn btn-primary"&gt;Home&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;About&lt;/button&gt;
    &lt;button type="button" class="btn btn-primary"&gt;Services&lt;/button&gt;
    </div>
  • Bootstrap Buttons

    Creating Buttons with Bootstrap

    Buttons are the integral part of a website and application. They are used for various purposes like, submit or reset an HTML form, performing interactive actions such as showing or hiding something on a web page on click of the button, redirecting user to another page, and so on. Bootstrap provides a quick and easy way to create and customize the buttons.

    Bootstrap Button Styles

    Different classes are available in Bootstrap for styling the buttons as well as to indicate the different states or semantic. Button styles can be applied to any element. However, it is applied normally to the <a><input>, and <button> elements for the best rendering.

    The following example will show you how to create different styles of buttons in Bootstrap:

    Example

    <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-dark">Dark</button>
    <button type="button" class="btn btn-light">Light</button>
    <button type="button" class="btn btn-link">Link</button>

    — The output of the above example will look something like this:

    Bootstrap Buttons

    Bootstrap Outline Buttons

    You can also create outline buttons by replacing the button modifier classes, like this:

    Example

    <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>
    <button type="button" class="btn btn-outline-light">Light</button>

    — The output of the above example will look something like this:

    Bootstrap Outline Buttons

    Changing the Sizes of Buttons

    Bootstrap gives you option further to scaling a button up or down.

    To make buttons larger add an extra class .btn-lg to the buttons, like this:

    Example

    <button type="button" class="btn btn-primary btn-lg">Large button</button>
    <button type="button" class="btn btn-secondary btn-lg">Large button</button>

    — The output of the above example will look something like this:

    Bootstrap Large Buttons

    Similarly, to make buttons smaller add an extra class .btn-sm to the buttons, like this:

    Example

    <button type="button" class="btn btn-primary btn-sm">Small button</button>
    <button type="button" class="btn btn-secondary btn-sm">Small button</button>

    — The output of the above example will look something like this:

    Bootstrap Small Buttons

    You can also create full-width or block buttons (buttons that covers the full width of the parent elements) through using the Bootstrap’s display and gap utility classes. These utilities offers much greater control over spacing, alignment, and responsive behaviors.

    Example

    <div class="d-grid gap-2">
    
    &lt;button type="button" class="btn btn-primary"&gt;Block button&lt;/button&gt;
    &lt;button type="button" class="btn btn-secondary"&gt;Block button&lt;/button&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Block Buttons

    You can also create responsive variation of these buttons using the .d-{breakpoint}-block classes.

    In the following example buttons will be vertically stacked on small and extra small devices (i.e. viewport width <768px). From medium (md) breakpoint up .d-md-block replaces the .d-grid class, thus nullifying the .gap-2 class. Let’s try it out and see how it really works:

    Example

    <div class="d-grid gap-2 d-md-block">
    
    &lt;button type="button" class="btn btn-primary"&gt;Button&lt;/button&gt;
    &lt;button type="button" class="btn btn-secondary"&gt;Button&lt;/button&gt;
    </div>

    Bootstrap Disabled Buttons

    Sometimes we need to disable a button for certain reasons, for example, a user in case is not eligible to perform this particular action, or we want to ensure that user should performed all other required actions before proceed to this particular action. Let’s see how to do that.

    Creating Disabled Buttons Using Button and Input Element

    Buttons created through <button> or <input> tag can be disabled by adding the disabled attribute to the respective element, as shown in the following example:

    Example

    <button type="button" class="btn btn-primary" disabled>Primary button</button>
    <button type="button" class="btn btn-secondary" disabled>Secondary button</button>

    — The output of the above example will look something like this:

    Bootstrap Disabled Buttons

    Creating Disabled Buttons Using Anchor Elements

    Buttons created through <a> tag can be disabled by adding the class .disabled, like this:

    Example

    <a href="#" class="btn btn-primary disabled">Primary link</a>
    <a href="#" class="btn btn-secondary disabled">Secondary link</a>

    — The output of the above example will look something like this:

    Bootstrap Disabled Anchor Buttons

    Note: The .disabled class only make links visually appear like disabled, however the link will remain clickable unless you remove the href attribute from it. Alternatively, you could implement custom JavaScript to prevent those clicks.


    Bootstrap Active Buttons

    Moreover, you can also apply the class .active to force the buttons look like active (i.e. pressed). Usually you don’t need to add this class to the buttons, as their active state is automatically styled by the Bootstrap using CSS :active pseudo-class. Here’s an example:

    Example

    <button type="button" class="btn btn-primary active">Primary button</button>
    <button type="button" class="btn btn-secondary active">Secondary button</button>

    — The output of the above example will look something like this:

    Bootstrap Active Buttons

    Creating Spinner Buttons

    With Bootstrap you can easily include spinner icon in a button to indicate the loading state in your application. Check out the following example to see how it works:

    Example

    <button type="button" class="btn btn-primary" disabled>
    
    &lt;span class="spinner-border spinner-border-sm"&gt;&lt;/span&gt;
    </button> <button type="button" class="btn btn-primary" disabled>
    &lt;span class="spinner-border spinner-border-sm"&gt;&lt;/span&gt; Loading...
    </button> <button type="button" class="btn btn-primary" disabled>
    &lt;span class="spinner-grow spinner-grow-sm"&gt;&lt;/span&gt; Loading...
    </button>

    — The output of the above example will look something like this:

    In the next chapter you will learn how to combine multiple buttons horizontally or vertically in a line like toolbars using the Bootstrap button groups component. Also, in the advanced section you will learn how to create toggle buttons without using any JavaScript code.

  • Bootstrap Input Groups

    Extending Form Controls with Bootstrap

    Bootstrap input group component is a very flexible and powerful component for creating interactive and elegant form controls, however, it is limited to text input, select, and textarea only.

    In the following sections you will see how to extend form controls by adding the text, icons and buttons before, after, or on both sides of it to make your form more attractive.

    Creating Prepended and Appended Inputs

    Input groups are created using the class .input-group. It act as a container for inputs and addons.

    Further, wrap the text or icon in a <span> element as well as apply the class .input-group-text on it and place it before or after the input. Let’s take a look at the following example:

    Example

    <div class="row g-2">
    
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-text"&gt;
                &lt;span class="bi-person"&gt;&lt;/span&gt;
            &lt;/span&gt;
            &lt;input type="text" class="form-control" placeholder="Username"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;            
            &lt;input type="text" class="form-control" placeholder="Amount"&gt;
            &lt;span class="input-group-text"&gt;.00&lt;/span&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-text"&gt;https://www.&lt;/span&gt;
            &lt;input type="text" class="form-control" placeholder="Domain name"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-text"&gt;$&lt;/span&gt;
            &lt;input type="text" class="form-control" placeholder="US Dollar"&gt;
            &lt;span class="input-group-text"&gt;.00&lt;/span&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Prepended and Appended Inputs

    Since Bootstrap 5 you can also prepend or append select box dropdown and textarea form controls. Let’s try out the following example and see how it basically works:

    Example

    <div class="row g-2">
    
    &lt;div class="col-12"&gt;
        &lt;div class="input-group"&gt;            
            &lt;span class="input-group-text"&gt;Address&lt;/span&gt;
            &lt;textarea class="form-control"&gt;&lt;/textarea&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;label class="input-group-text"&gt;Country&lt;/label&gt;
            &lt;select class="form-select"&gt;
                &lt;option selected&gt;Choose...&lt;/option&gt;
                &lt;option&gt;France&lt;/option&gt;
                &lt;option&gt;Germany&lt;/option&gt;
                &lt;option&gt;Hungary&lt;/option&gt;
            &lt;/select&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;select class="form-select"&gt;
                &lt;option selected&gt;Choose...&lt;/option&gt;
                &lt;option&gt;One&lt;/option&gt;
                &lt;option&gt;Two&lt;/option&gt;
                &lt;option&gt;Three&lt;/option&gt;
            &lt;/select&gt;
            &lt;button type="button" class="btn btn-secondary"&gt;Submit&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Prepended and Appended Select box and Textarea

    Similarly, you can prepend or append addons to Bootstrap’s custom file input, like this:

    Example

    <div class="input-group">
    
    &lt;input type="file" class="form-control"&gt;
    &lt;button type="button" class="btn btn-secondary"&gt;Upload&lt;/button&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Prepended and Appended Select box and Textarea

    Checkboxes and Radio Buttons Addons

    Similarly, you can place checkbox or radio button within input group’s addon instead of text.

    Example

    <div class="row">
    
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-text"&gt;
                &lt;input type="checkbox" class="form-check-input mt-0"&gt;
            &lt;/span&gt;
            &lt;input type="text" class="form-control"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-text"&gt;
                &lt;input type="radio" class="form-check-input mt-0"&gt;
            &lt;/span&gt;
            &lt;input type="text" class="form-control"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Input groups with Checkbox and Radio Buttons

    Placing Multiple Inputs or Addons

    You can also place multiple inputs side-by-side within an input group easily, like this:

    Example

    <div class="input-group">
    
    &lt;span class="input-group-text"&gt;Your Name&lt;/span&gt;
    &lt;input type="text" class="form-control" placeholder="First name"&gt;
    &lt;input type="text" class="form-control" placeholder="Last name"&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Input Group with Multiple Inputs

    Similarly, you can also place multiple addons side-by-side within an input group. You can also mix it with checkbox and radio inputs, as shown in the following example:

    Example

    <div class="row">
    
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-text"&gt;
                &lt;input type="checkbox" class="form-check-input mt-0"&gt;
            &lt;/span&gt;
            &lt;span class="input-group-text"&gt;$&lt;/span&gt;
            &lt;input type="text" class="form-control"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-text"&gt;$&lt;/span&gt;
            &lt;span class="input-group-text"&gt;0.00&lt;/span&gt;
            &lt;input type="text" class="form-control"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Input Group with Multiple Addons

    Buttons Addons for Form Controls

    You can also prepend or append buttons to the form controls like text. Simply, place as many buttons as you like within the .input-group, as shown in the following example:

    Example

    <div class="row">
    
    &lt;div class="col-5"&gt;
        &lt;div class="input-group"&gt;
            &lt;input type="text" class="form-control" placeholder="Search..."&gt;
            &lt;button type="button" class="btn btn-secondary"&gt;
                &lt;i class="bi-search"&gt;&lt;/i&gt;
            &lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-7"&gt;
        &lt;div class="input-group"&gt;
            &lt;input type="text" class="form-control" placeholder="Type something..."&gt;
            &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
            &lt;button type="reset" class="btn btn-danger"&gt;Reset&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Input groups with Buttons

    Adding Button Dropdowns

    You can even add buttons with dropdowns to a form control if you want to perform more than one action from a button. Also, in case of input group you don’t need the .dropdown wrapper element, which is otherwise normally required. Let’s check out an example:

    Example

    <div class="row">
    
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;button type="button" class="btn btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown"&gt;Dropdown&lt;/button&gt;
            &lt;div class="dropdown-menu"&gt;
                &lt;a href="#" class="dropdown-item"&gt;Action&lt;/a&gt;
                &lt;a href="#" class="dropdown-item"&gt;Another action&lt;/a&gt;
            &lt;/div&gt;
            &lt;input type="text" class="form-control"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;input type="text" class="form-control"&gt;
            &lt;button type="button" class="btn btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown"&gt;Dropdown&lt;/button&gt;
            &lt;div class="dropdown-menu"&gt;
                &lt;a href="#" class="dropdown-item"&gt;Action&lt;/a&gt;
                &lt;a href="#" class="dropdown-item"&gt;Another action&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Input groups with Button Dropdowns

    Adding Segmented Dropdown Button Groups

    Similarly, you can define the segmented dropdown button group where dropdown button is placed besides the other buttons, as shown in the following example:

    Example

    <div class="row">
    
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;button type="button" class="btn btn-outline-secondary"&gt;Action&lt;/button&gt;
            &lt;button type="button" class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown"&gt;
                &lt;span class="visually-hidden"&gt;Toggle Dropdown&lt;/span&gt;
            &lt;/button&gt;
            &lt;div class="dropdown-menu"&gt;
                &lt;a href="#" class="dropdown-item"&gt;Action&lt;/a&gt;
                &lt;a href="#" class="dropdown-item"&gt;Another action&lt;/a&gt;
            &lt;/div&gt;
            &lt;input type="text" class="form-control"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-6"&gt;
        &lt;div class="input-group"&gt;
            &lt;input type="text" class="form-control"&gt;
            &lt;button type="button" class="btn btn-outline-secondary"&gt;Action&lt;/button&gt;
            &lt;button type="button" class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown"&gt;
                &lt;span class="visually-hidden"&gt;Toggle Dropdown&lt;/span&gt;
            &lt;/button&gt;
            &lt;div class="dropdown-menu"&gt;
                &lt;a href="#" class="dropdown-item"&gt;Action&lt;/a&gt;
                &lt;a href="#" class="dropdown-item"&gt;Another action&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Input groups with Split Button Dropdowns

    Height Sizing of Input Groups

    You can also add the relative form sizing classes such as .input-group-lg or .input-group-sm to the .input-group element itself to make it larger or smaller in height.

    The contents within the .input-group will automatically resize — there is no need for repeating the form control size classes on each element. Here’s an example:

    Example

    <!-- Larger input group -->
    <div class="input-group input-group-lg">
    
    &lt;span class="input-group-text"&gt;Large&lt;/span&gt;
    &lt;input type="text" class="form-control"&gt;
    </div>
        
    <!-- Default input group --> <div class="input-group mt-2">
    &lt;span class="input-group-text"&gt;Default&lt;/span&gt;
    &lt;input type="text" class="form-control"&gt;
    </div>
        
    <!-- Smaller input group --> <div class="input-group input-group-sm mt-2">
    &lt;span class="input-group-text"&gt;Small&lt;/span&gt;
    &lt;input type="text" class="form-control"&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Input Group Height Sizing
  • Bootstrap Custom Forms

    Creating Custom Form Controls

    Bootstrap enables you to customize the browser’s default form controls to create even more elegant form layouts. Now you can create completely customized and cross-browser compatible radio buttons and checkboxes, file inputs, select dropdowns, range inputs, and more.

    In the following sections, you’ll see how to create those custom form elements one by one.

    Creating Custom Checkboxes

    To create custom checkboxes wrap each checkbox <input> and their corresponding <label> in a <div> element, and apply the classes as shown in the following example:

    Example

    <div class="form-check">
    
    &lt;input type="checkbox" class="form-check-input" name="customCheck" id="customCheck1"&gt;
    &lt;label class="form-check-label" for="customCheck1"&gt;Custom checkbox&lt;/label&gt;
    </div> <div class="form-check">
    &lt;input type="checkbox" class="form-check-input" name="customCheck" id="customCheck2" checked&gt;
    &lt;label class="form-check-label" for="customCheck2"&gt;Another custom checkbox&lt;/label&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Custom Checkboxes

    Creating Custom Radio Buttons

    Similarly, you can create custom radio buttons using the Bootstrap like this:

    Example

    <div class="form-check">
    
    &lt;input type="radio" class="form-check-input" name="customRadio" id="customRadio1"&gt;
    &lt;label class="form-check-label" for="customRadio1"&gt;Custom radio&lt;/label&gt;
    </div> <div class="form-check">
    &lt;input type="radio" class="form-check-input" name="customRadio" id="customRadio2" checked&gt;
    &lt;label class="form-check-label" for="customRadio2"&gt;Another custom radio&lt;/label&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Custom Radio Buttons

    Inline Checkboxes and Radio Buttons

    You can also place these custom checkboxes and radio buttons inline by simply adding the class .form-check-inline on the wrapper .form-check element.

    The following example will display the checkboxes inline (i.e., in the same line).

    Example

    <div class="form-check form-check-inline">
    
    &lt;input type="checkbox" class="form-check-input" name="customCheck" id="customCheck1"&gt;
    &lt;label class="form-check-label" for="customCheck1"&gt;Custom checkbox&lt;/label&gt;
    </div> <div class="form-check form-check-inline">
    &lt;input type="checkbox" class="form-check-input" name="customCheck" id="customCheck2" checked&gt;
    &lt;label class="form-check-label" for="customCheck2"&gt;Another custom checkbox&lt;/label&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Custom Checkboxes Inline

    Similarly, you can place the radio buttons inline, as shown in the following example:

    Example

    <div class="form-check form-check-inline">
    
    &lt;input type="radio" class="form-check-input" name="customRadio" id="customRadio1"&gt;
    &lt;label class="form-check-label" for="customRadio1"&gt;Custom radio&lt;/label&gt;
    </div> <div class="form-check form-check-inline">
    &lt;input type="radio" class="form-check-input" name="customRadio" id="customRadio2" checked&gt;
    &lt;label class="form-check-label" for="customRadio2"&gt;Another custom radio&lt;/label&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Custom Radio Buttons Inline

    Disabling Custom Checkboxes and Radios

    Custom checkboxes and radio buttons can also be disabled. Just add the boolean attribute disabled to the <input> element, as shown in the following example:

    Example

    <div class="form-check">
    
    &lt;input type="checkbox" class="form-check-input" id="customCheck" disabled&gt;
    &lt;label class="form-check-label" for="customCheck"&gt;Disabled custom checkbox&lt;/label&gt;
    </div> <div class="form-check">
    &lt;input type="radio" class="form-check-input" id="customRadio" disabled&gt;
    &lt;label class="form-check-label" for="customRadio"&gt;Disabled custom radio&lt;/label&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Disabled Custom Checkboxes and Radio Buttons

    Creating Toggle Switches

    The switch markup is similar to custom checkbox—the only difference is—it uses the .form-switch class to render a toggle switch. Switches also support the disabled attribute.

    Let’s try out the following example to understand how it basically works:

    Example

    <div class="form-check form-switch">
    
    &lt;input class="form-check-input" type="checkbox" id="switchDefault"&gt;
    &lt;label class="form-check-label" for="switchDefault"&gt;Default switch checkbox&lt;/label&gt;
    </div> <div class="form-check form-switch">
    &lt;input class="form-check-input" type="checkbox" id="switchChecked" checked&gt;
    &lt;label class="form-check-label" for="switchChecked"&gt;Checked switch checkbox&lt;/label&gt;
    </div> <div class="form-check form-switch">
    &lt;input class="form-check-input" type="checkbox" id="switchDisabled" disabled&gt;
    &lt;label class="form-check-label" for="switchDisabled"&gt;Disabled switch checkbox&lt;/label&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Toggle Switch

    Creating Custom Select Menu

    You can also customize the select dropdown menus by simply adding the class .form-select on the <select> element. However, this custom styling is limited only to the initial appearance of the <select> and cannot modify the <option>s due to browser limitations.

    Example

    <select class="form-select">
    
    &lt;option selected&gt;Custom select menu&lt;/option&gt;
    &lt;option value="1"&gt;One&lt;/option&gt;
    &lt;option value="2"&gt;Two&lt;/option&gt;
    &lt;option value="3"&gt;Three&lt;/option&gt;
    </select>

    — The output of the above example will look something like this:

    Bootstrap Custom Select Menu

    You can also add the disabled attribute on a custom select to give it a grayed out appearance and remove pointer events, as shown in the following example:

    Example

    <select class="form-select" disabled>
    
    &lt;option selected&gt;Custom select menu&lt;/option&gt;
    &lt;option value="1"&gt;One&lt;/option&gt;
    &lt;option value="2"&gt;Two&lt;/option&gt;
    &lt;option value="3"&gt;Three&lt;/option&gt;
    </select>

    — The output of the above example will look something like this:

    Bootstrap Disabled Custom Select Menu

    You can also create large and small variant of the custom selects to match the height of similarly sized Bootstrap’s text inputs using the classes .form-select-lg and .form-select-sm on the <select> element, respectively. Let’s take a look at the following example:

    Example

    <select class="form-select form-select-lg">
    
    &lt;option selected&gt;Large custom select menu&lt;/option&gt;
    &lt;option value="1"&gt;One&lt;/option&gt;
    &lt;option value="2"&gt;Two&lt;/option&gt;
    &lt;option value="3"&gt;Three&lt;/option&gt;
    </select> <select class="form-select mt-3">
    &lt;option selected&gt;Default custom select menu&lt;/option&gt;
    &lt;option value="1"&gt;One&lt;/option&gt;
    &lt;option value="2"&gt;Two&lt;/option&gt;
    &lt;option value="3"&gt;Three&lt;/option&gt;
    </select> <select class="form-select form-select-sm mt-3">
    &lt;option selected&gt;Small custom select menu&lt;/option&gt;
    &lt;option value="1"&gt;One&lt;/option&gt;
    &lt;option value="2"&gt;Two&lt;/option&gt;
    &lt;option value="3"&gt;Three&lt;/option&gt;
    </select>

    — The output of the above example will look something like this:

    Bootstrap Custom Select Sizing

    Bootstrap custom select also supports multiple and size attributes like normal select:

    Example

    <select class="form-select" size="3" multiple>
    
    &lt;option value="1"&gt;One&lt;/option&gt;
    &lt;option value="2"&gt;Two&lt;/option&gt;
    &lt;option value="3"&gt;Three&lt;/option&gt;
    &lt;option value="4"&gt;Four&lt;/option&gt;
    </select>

    Creating Custom Range Inputs

    To create custom range inputs just apply the class .form-range to the <input type="range">.

    Example

    <label for="customRange">Custom range</label>
    <input type="range" class="form-range" id="customRange">

    — The output of the above example will look something like this:

    Bootstrap Custom Range

    You can also add disabled attribute on the range inputs to give it a grayed out appearance and remove pointer events, as shown in the following example:

    Example

    <label for="customRange">Disabled range</label>
    <input type="range" class="form-range" id="customRange" disabled>

    — The output of the above example will look something like this:

    Bootstrap Disabled Custom Range

    Setting the Min and Max Values

    By default range inputs have implicit values for min and max—0 and 100, respectively. But, you may specify new values for those using the min and max attributes.

    Also, range inputs “snap” to integer values by default. To change this, you can specify a step value. In the following example, we’ve doubled the number of steps by using the step="0.5".

    Example

    <label for="customRange">Custom range</label>
    <input type="range" class="form-range" min="0" max="10" step="0.5" id="customRange">
  • Bootstrap Forms

    Creating Forms with Bootstrap

    HTML forms are an integral part of the web pages and applications, but creating the form layouts or styling the form controls manually one by one using CSS are often boring and tedious. Bootstrap greatly simplifies the process of styling and alignment of form controls like labels, input fields, selectboxes, textareas, buttons, etc. through predefined set of classes.

    Bootstrap provides three different types of form layouts:

    • Vertical Form (default form layout)
    • Horizontal Form
    • Inline Form

    The following section will give you the detailed overview of all these form layouts as well as the various form related Bootstrap components one by one. Well let’s get started.

    Creating Vertical Form Layout

    To create vertical form layouts simply use the predefined margin utility classes for grouping the labels, form controls, optional form text, and form validation messages.

    Here’s an example in which form controls are vertically stacked with labels on the top.

    Example

    Try this code »

    <form>
    
    &lt;div class="mb-3"&gt;
        &lt;label class="form-label" for="inputEmail"&gt;Email&lt;/label&gt;
        &lt;input type="email" class="form-control" id="inputEmail" placeholder="Email"&gt;
    &lt;/div&gt;
    &lt;div class="mb-3"&gt;
        &lt;label class="form-label" for="inputPassword"&gt;Password&lt;/label&gt;
        &lt;input type="password" class="form-control" id="inputPassword" placeholder="Password"&gt;
    &lt;/div&gt;
    &lt;div class="mb-3"&gt;
        &lt;div class="form-check"&gt;
            &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
            &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
    </form>

    — The output of the above example will look something like this:

    Bootstrap Vertical Form Layout

    You will learn about custom checkboxes and other custom form controls in the next chapter.

    Note: All textual form controls, such as <input> and <textarea> requires the class
    .form-control, while <select> requires the class .form-select for general styling. These classes also makes the forms controls 100% wide. To change their width or use them inline, you can utilize the Bootstrap’s predefined grid classes.

    Tip: It is recommend to use margin-bottom utility classes (e.g., mb-2mb-3, etc.) to add vertical spacing between the form groups. As, using single direction margin throughout in the form prevent margin collapsing and create more consist form.


    Creating Horizontal Form Layout

    You can also create horizontal form layouts where labels and form controls are aligned side-by-side using the Bootstrap grid classes. To create a horizontal form layout add the class .row on form groups and use the .col-*-* grid classes to specify the width of your labels and controls.

    Also, be sure to apply the class .col-form-label on the <label> elements, so that they’re vertically centered with their associated form controls. Let’s check out an example:

    Example

    Try this code »

    <form>
    
    &lt;div class="row mb-3"&gt;
        &lt;label for="inputEmail" class="col-sm-2 col-form-label"&gt;Email&lt;/label&gt;
        &lt;div class="col-sm-10"&gt;
            &lt;input type="email" class="form-control" id="inputEmail" placeholder="Email"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label for="inputPassword" class="col-sm-2 col-form-label"&gt;Password&lt;/label&gt;
        &lt;div class="col-sm-10"&gt;
            &lt;input type="password" class="form-control" id="inputPassword" placeholder="Password"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;div class="col-sm-10 offset-sm-2"&gt;
            &lt;div class="form-check"&gt;
                    &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
                    &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="col-sm-10 offset-sm-2"&gt;
            &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </form>

    — The output of the above example will look something like this:

    Bootstrap Horizontal Form Layout

    Creating Inline Form Layout

    Sometimes you may want to display a series of form controls, and buttons in a single horizontal row to compact the layout. You can do this easily by using the Bootstrap’s grid classes.

    Let’s take a look at following example and see how it actually works:

    Example

    Try this code »

    <form>
    
    &lt;div class="row align-items-center g-3"&gt;
        &lt;div class="col-auto"&gt;
            &lt;label class="visually-hidden" for="inputEmail"&gt;Email&lt;/label&gt;
            &lt;input type="email" class="form-control" id="inputEmail" placeholder="Email"&gt;
        &lt;/div&gt;
        &lt;div class="col-auto"&gt;
            &lt;label class="visually-hidden" for="inputPassword"&gt;Password&lt;/label&gt;
            &lt;input type="password" class="form-control" id="inputPassword" placeholder="Password"&gt;
        &lt;/div&gt;
        &lt;div class="col-auto"&gt;
            &lt;div class="form-check"&gt;
                &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
                &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-auto"&gt;
            &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </form>

    — The output of the above example will look something like this:

    Bootstrap Inline Form Layout

    Check out the snippets section for examples of some beautifully designed Bootstrap forms.

    Tip: It is recommended to include a label for every form inputs otherwise screen readers will have trouble with your forms. However, in case of inline form layouts you can hide the labels using the .visually-hidden class, so that only screen readers can read it.


    Creating Responsive Form Layout

    You can also make your forms responsive through using the grid classes with specific breakpoints.

    The following example will create a form which laid out inline on medium devices and up (i.e., viewport width ≥768px), but will become vertically stacked on smaller viewports.

    Example

    Try this code »

    <form>
    
    &lt;div class="row align-items-center g-3"&gt;
        &lt;div class="col-md-auto col-12"&gt;
            &lt;label class="form-label d-md-none" for="inputEmail"&gt;Email&lt;/label&gt;
            &lt;input type="email" class="form-control" id="inputEmail" placeholder="Email"&gt;
        &lt;/div&gt;
        &lt;div class="col-md-auto col-12"&gt;
            &lt;label class="form-label d-md-none" for="inputPassword"&gt;Password&lt;/label&gt;
            &lt;input type="password" class="form-control" id="inputPassword" placeholder="Password"&gt;
        &lt;/div&gt;
        &lt;div class="col-md-auto col-12"&gt;
            &lt;div class="form-check"&gt;
                &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
                &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-md-auto col-12"&gt;
            &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </form>

    Creating Static Form Control

    There might be a situation when you just want to display a plain text value next to a form label instead of a working form control. You can do this easily by replacing the class .form-control with the .form-control-plaintext and applying the attribute readonly.

    The .form-control-plaintext class removes the default styling from the form field, but preserves the correct margin and padding. Let’s take a look at an example:

    Example

    Try this code »

    <form>
    
    &lt;div class="row mb-3"&gt;
        &lt;label for="inputEmail" class="col-sm-2 col-form-label"&gt;Email&lt;/label&gt;
        &lt;div class="col-sm-10"&gt;
            &lt;input type="email" readonly class="form-control-plaintext" id="inputEmail" value="[email protected]"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label for="inputPassword" class="col-sm-2 col-form-label"&gt;Password&lt;/label&gt;
        &lt;div class="col-sm-10"&gt;
            &lt;input type="password" class="form-control" id="inputPassword" placeholder="Password"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;div class="col-sm-10 offset-sm-2"&gt;
            &lt;div class="form-check"&gt;
                    &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
                    &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="col-sm-10 offset-sm-2"&gt;
            &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </form>

    — The output of the above example will look something like this:

    Bootstrap Static Form Control

    Column Sizing of Form Controls

    You can also match the size of your inputs, textareas and select boxes to the Bootstrap grid column sizes. Simply, place your form controls (i.e. <input><textarea>, and <select>) in grid columns.

    Let’s try out the following example to understand how it basically works:

    Example

    <div class="row g-3">
    
    &lt;div class="col-6"&gt;
        &lt;input type="text" class="form-control" placeholder="City"&gt;
    &lt;/div&gt;
    &lt;div class="col-4"&gt;
        &lt;select class="form-select"&gt;
            &lt;option&gt;State&lt;/option&gt;
        &lt;/select&gt;
    &lt;/div&gt;
    &lt;div class="col-2"&gt;
        &lt;input type="text" class="form-control" placeholder="Zip"&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Grid Sizing of Form Controls

    Placing Checkboxes and Radio Buttons Inline

    By default, any number of custom checkboxes and radio buttons that are immediate sibling will be vertically stacked and appropriately spaced with .form-check class.

    But, you can also place these custom checkboxes and radio buttons inline (i.e., in the same line) by simply adding the class .form-check-inline to .form-check element, like this:

    Example

    <div class="row">
    
    &lt;div class="col-12"&gt;
        &lt;div class="form-check form-check-inline"&gt;
            &lt;input type="checkbox" class="form-check-input" name="hobbies" id="checkMusic"&gt;
            &lt;label class="form-check-label" for="checkMusic"&gt;Music&lt;/label&gt;
        &lt;/div&gt;
        &lt;div class="form-check form-check-inline ms-3"&gt;
            &lt;input type="checkbox" class="form-check-input" name="hobbies" id="checkTravel" checked&gt;
            &lt;label class="form-check-label" for="checkTravel"&gt;Travel&lt;/label&gt;
        &lt;/div&gt;
        &lt;div class="form-check form-check-inline ms-3"&gt;
            &lt;input type="checkbox" class="form-check-input" name="hobbies" id="checkReading" checked&gt;
            &lt;label class="form-check-label" for="checkReading"&gt;Reading&lt;/label&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Placing Checkboxes Inline

    Similarly, you can place the radio buttons inline, as shown in the following example:

    Example

    <div class="row">
    
    &lt;div class="col-12"&gt;
        &lt;div class="form-check form-check-inline"&gt;
            &lt;input type="radio" class="form-check-input" name="gender" id="radioMale" checked&gt;
            &lt;label class="form-check-label" for="radioMale"&gt;Male&lt;/label&gt;
        &lt;/div&gt;
        &lt;div class="form-check form-check-inline ms-3"&gt;
            &lt;input type="radio" class="form-check-input" name="gender" id="radioFemale"&gt;
            &lt;label class="form-check-label" for="radioFemale"&gt;Female&lt;/label&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Placing Radio Buttons Inline

    Adding Help Text to Form Controls

    Placing help text for the form controls in an efficient way to guide users to enter the correct data in a form. You can place block level help text for a form control using the class .form-text. The block help text is typically displayed at the bottom of the control. Here’s an example:

    Example

    <label class="form-label" for="inputPassword">Password</label>
    <input type="password" class="form-control" id="inputPassword">
    <div class="form-text">
    
    Must be 8-20 characters long, contain letters, numbers and special characters, but must not contain spaces.
    </div>

    — The output of the above example will look something like this:

    Bootstrap Block Help Text

    Similarly, you can also place inline help text using the <small> element. No need to use .form-text in this case. The following example shows how to implement this:

    Example

    Try this code »

    <div class="row align-items-center g-3">
    
    &lt;div class="col-auto"&gt;
        &lt;label class="col-form-label" for="inputPassword"&gt;Password&lt;/label&gt;
    &lt;/div&gt;
    &lt;div class="col-auto"&gt;
        &lt;input type="password" class="form-control" id="inputPassword"&gt;
    &lt;/div&gt;
    &lt;div class="col-auto"&gt;
        &lt;span class="form-text"&gt;Must be 8-20 characters long.&lt;/span&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Inline Help Text

    Creating Disabled Form Controls

    To disable individual form controls such as <input><textarea><select> just add the attributes disabled to them and Bootstrap will do the rest. Here’s an example:

    Example

    <input type="text" class="form-control mb-3" placeholder="Disabled input" disabled>
    <textarea class="form-control mb-3" placeholder="Disabled textarea" disabled></textarea>
    <select class="form-select" disabled>
    
    &lt;option&gt;Disabled select&lt;/option&gt;
    </select>

    — The output of the above example will look something like this:

    Bootstrap Disabled Form Controls

    However, if you want to disable all controls within a <form> at once, place them inside a <fieldset> element and apply the attribute disabled on it, as shown in the following example:

    Example

    <form>
    
    &lt;fieldset disabled&gt;
        &lt;div class="row mb-3"&gt;
            &lt;label for="inputEmail" class="col-sm-2 col-form-label"&gt;Email&lt;/label&gt;
            &lt;div class="col-sm-10"&gt;
                &lt;input type="email" class="form-control" id="inputEmail" placeholder="Email"&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="row mb-3"&gt;
            &lt;label for="inputPassword" class="col-sm-2 col-form-label"&gt;Password&lt;/label&gt;
            &lt;div class="col-sm-10"&gt;
                &lt;input type="password" class="form-control" id="inputPassword" placeholder="Password"&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="row mb-3"&gt;
            &lt;div class="col-sm-10 offset-sm-2"&gt;
                &lt;div class="form-check"&gt;
                        &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
                        &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-sm-10 offset-sm-2"&gt;
                &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/fieldset&gt;
    </form>

    — The output of the above example will look something like this:

    Bootstrap Disabled Forms

    Creating Readonly Inputs

    You can also add the readonly boolean attribute on an input or textarea to prevent the modification of its value. Read-only inputs appear in lighter background (just like disabled inputs), but it retain the standard text cursor. Check out the following example to see how it works:

    Example

    Try this code »

    <input type="text" class="form-control mb-2" value="This input value cannot be changed." readonly>
    <textarea rows="3" class="form-control" readonly>This textarea value cannot be changed.</textarea>

    — The output of the above example will look something like this:

    Bootstrap Read-only Input and Textarea

    Height Sizing of Form Controls

    You can easily change the height of your text inputs and select boxes to match the button sizes.

    Use the form control height sizing classes such as .form-control-lg and .form-control-sm on the text inputs to create it’s larger or smaller sizes. Here’s an example:

    Example

    <div class="row mb-3">
    
    &lt;label class="col-sm-2 col-form-label col-form-label-lg"&gt;Email&lt;/label&gt;
    &lt;div class="col-sm-10"&gt;
        &lt;input type="email" class="form-control form-control-lg" placeholder="Large input"&gt;
    &lt;/div&gt;
    </div> <div class="row mb-3">
    &lt;label class="col-sm-2 col-form-label"&gt;Email&lt;/label&gt;
    &lt;div class="col-sm-10"&gt;
        &lt;input type="email" class="form-control" placeholder="Default input"&gt;
    &lt;/div&gt;
    </div> <div class="row">
    &lt;label class="col-sm-2 col-form-label"&gt;Email&lt;/label&gt;
    &lt;div class="col-sm-10"&gt;
        &lt;input type="email" class="form-control form-control-sm" placeholder="Small input"&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Text Input Height Sizing

    Tip: Be sure to apply the class .col-form-label-lg or .col-form-label-sm on the <label> or <legend> elements to correctly resize the label according to the form controls.

    Similarly, you can create larger and smaller variants of the select boxes using the .form-select-lg and .form-select-sm classes on the <select> element, like this:

    Example

    <div class="row mb-3">
    
    &lt;label class="col-sm-2 col-form-label col-form-label-lg"&gt;State&lt;/label&gt;
    &lt;div class="col-sm-10"&gt;
        &lt;select class="form-select form-select-lg"&gt;
            &lt;option&gt;Large select&lt;/option&gt;
        &lt;/select&gt;
    &lt;/div&gt;
    </div> <div class="row mb-3">
    &lt;label class="col-sm-2 col-form-label"&gt;State&lt;/label&gt;
    &lt;div class="col-sm-10"&gt;
        &lt;select class="form-select"&gt;
            &lt;option&gt;Default select&lt;/option&gt;
        &lt;/select&gt;
    &lt;/div&gt;
    </div> <div class="row">
    &lt;label class="col-sm-2 col-form-label col-form-label-sm"&gt;State&lt;/label&gt;
    &lt;div class="col-sm-10"&gt;
        &lt;select class="form-select form-select-sm"&gt;
            &lt;option&gt;Small select&lt;/option&gt;
        &lt;/select&gt;
    &lt;/div&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Select Box Height Sizing

    Bootstrap Form Validation

    Bootstrap provides an easy and quick way to validate web forms on client-side. It uses browser’s native form validation API to validate the form. Form validation styles are applied via CSS :invalid and :valid pseudo-classes. It applies to <input><select>, and <textarea> elements.

    Let’s try out the following example and see how it actually works:

    Example

    <form class="needs-validation" novalidate>
    
    &lt;div class="mb-3"&gt;
        &lt;label class="form-label" for="inputEmail"&gt;Email&lt;/label&gt;
        &lt;input type="email" class="form-control" id="inputEmail" placeholder="Email" required&gt;
        &lt;div class="invalid-feedback"&gt;Please enter a valid email address.&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="mb-3"&gt;
        &lt;label class="form-label" for="inputPassword"&gt;Password&lt;/label&gt;
        &lt;input type="password" class="form-control" id="inputPassword" placeholder="Password" required&gt;
        &lt;div class="invalid-feedback"&gt;Please enter your password to continue.&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="mb-3"&gt;
        &lt;div class="form-check"&gt;
            &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
            &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
    </form>

    Note: For custom Bootstrap form validation messages, you’ll need to disables the browser default feedback tooltips by adding the novalidate boolean attribute to the <form> element. However, it still provides access to the form validation APIs in JavaScript.

    Here’s the custom JavaScript code that displays error messages and disables form submission if there are invalid fields. See the JavaScript closures chapter to learn about self-executing function.

    Example

    <script>
    
    // Self-executing function
    (function() {
        'use strict';
        window.addEventListener('load', function() {
            // Fetch all the forms we want to apply custom Bootstrap validation styles to
            var forms = document.getElementsByClassName('needs-validation');
            // Loop over them and prevent submission
            var validation = Array.prototype.filter.call(forms, function(form) {
                form.addEventListener('submit', function(event) {
                    if (form.checkValidity() === false) {
                        event.preventDefault();
                        event.stopPropagation();
                    }
                    form.classList.add('was-validated');
                }, false);
            });
        }, false);
    })();
    </script>

    — The output of the above example will look something like this:

    Bootstrap Form Validation

    Tip: To reset the appearance of the form programmatically, remove the .was-validated class from the <form> element after submission. This class is applied automatically on the form by the Bootstrap when you click the submit button.

    If you require server-side validation, you can alternatively use the .is-invalid and .is-valid classes to indicate invalid and valid form fields. The .invalid-feedback and .valid-feedback are also supported with these classes. Let’s take a look at the following example:

    Example

    <form>
    
    &lt;div class="mb-3"&gt;
        &lt;label class="form-label" for="inputEmail"&gt;Email&lt;/label&gt;
        &lt;input type="email" class="form-control is-valid" id="inputEmail" placeholder="Email" value="[email protected]" required&gt;
        &lt;div class="valid-feedback"&gt;Good! Your email address looks valid.&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="mb-3"&gt;
        &lt;label class="form-label" for="inputPassword"&gt;Password&lt;/label&gt;
        &lt;input type="password" class="form-control is-invalid" id="inputPassword" placeholder="Password" required&gt;
        &lt;div class="invalid-feedback"&gt;Opps! You have entered an invalid password.&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="mb-3"&gt;
        &lt;div class="form-check"&gt;
            &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
            &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
    </form>

    — The output of the above example will look something like this:

    Bootstrap Server Side Form Validation

    You can alternatively swap the .{valid|invalid}-feedback classes for .{valid|invalid}-tooltip classes to display the validation feedback text in a tooltip style.

    Also, be sure to apply the style position: relative or class .position-relative on the parent element for proper feedback tooltip positioning. Here’s an example:

    Example

    <form>
    
    &lt;div class="mb-3 position-relative"&gt;
        &lt;label class="form-label" for="inputEmail"&gt;Email&lt;/label&gt;
        &lt;input type="email" class="form-control is-valid" id="inputEmail" placeholder="Email" value="[email protected]" required&gt;
        &lt;div class="valid-tooltip"&gt;Good! Your email address looks valid.&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="mb-3 position-relative"&gt;
        &lt;label class="form-label" for="inputPassword"&gt;Password&lt;/label&gt;
        &lt;input type="password" class="form-control is-invalid" id="inputPassword" placeholder="Password" required&gt;
        &lt;div class="invalid-tooltip"&gt;Opps! You have entered an invalid password.&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="mb-3"&gt;
        &lt;div class="form-check"&gt;
            &lt;input class="form-check-input" type="checkbox" id="checkRemember"&gt;
            &lt;label class="form-check-label" for="checkRemember"&gt;Remember me&lt;/label&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;button type="submit" class="btn btn-primary"&gt;Sign in&lt;/button&gt;
    </form>

    — The output of the above example will look something like this:

    Bootstrap Form Validation Feedback in Styled Tooltip

    Supported Form Controls in Bootstrap

    Bootstrap includes support for all standard HTML form controls as well as new HTML5 input types such as datetime, number, email, url, search, range, color, url, and so on. The following example will show you the usages of standard form controls with Bootstrap.

    Example

    <form>
    
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label" for="firstName"&gt;First Name:&lt;/label&gt;
        &lt;div class="col-sm-9"&gt;
            &lt;input type="text" class="form-control" id="firstName" placeholder="First Name" required&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label" for="lastName"&gt;Last Name:&lt;/label&gt;
        &lt;div class="col-sm-9"&gt;
            &lt;input type="text" class="form-control" id="lastName" placeholder="Last Name" required&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label" for="emailAddress"&gt;Email Address:&lt;/label&gt;
        &lt;div class="col-sm-9"&gt;
            &lt;input type="email" class="form-control" id="emailAddress" placeholder="Email Address" required&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label" for="phoneNumber"&gt;Mobile Number:&lt;/label&gt;
        &lt;div class="col-sm-9"&gt;
            &lt;input type="number" class="form-control" id="phoneNumber" placeholder="Phone Number" required&gt;
        &lt;/div&gt;
    &lt;/div&gt;        
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label"&gt;Date of Birth:&lt;/label&gt;
        &lt;div class="col-sm-3"&gt;
            &lt;select class="form-select" required&gt;
                &lt;option&gt;Date&lt;/option&gt;
            &lt;/select&gt;
        &lt;/div&gt;
        &lt;div class="col-sm-3"&gt;
            &lt;select class="form-select" required&gt;
                &lt;option&gt;Month&lt;/option&gt;
            &lt;/select&gt;
        &lt;/div&gt;
        &lt;div class="col-sm-3"&gt;
            &lt;select class="form-select"&gt;
                &lt;option&gt;Year&lt;/option&gt;
            &lt;/select&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label" for="postalAddress"&gt;Postal Address:&lt;/label&gt;
        &lt;div class="col-sm-9"&gt;
            &lt;textarea rows="3" class="form-control" id="postalAddress" placeholder="Postal Address" required&gt;&lt;/textarea&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label" for="ZipCode"&gt;Zip Code:&lt;/label&gt;
        &lt;div class="col-sm-9"&gt;
            &lt;input type="text" class="form-control" id="ZipCode" placeholder="Zip Code" required&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label" for="uploadImage"&gt;Upload Image:&lt;/label&gt;
        &lt;div class="col-sm-9"&gt;
            &lt;input type="file" class="form-control" id="uploadImage"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;label class="col-sm-3 col-form-label"&gt;Gender:&lt;/label&gt;
        &lt;div class="col-sm-9 mt-2"&gt;
            &lt;div class="form-check form-check-inline"&gt;
                &lt;input type="radio" class="form-check-input" name="gender" id="radioMale"&gt;
                &lt;label class="form-check-label" for="radioMale"&gt;Male&lt;/label&gt;
            &lt;/div&gt;
            &lt;div class="form-check form-check-inline"&gt;
                &lt;input type="radio" class="form-check-input" name="gender" id="radioFemale"&gt;
                &lt;label class="form-check-label" for="radioFemale"&gt;Female&lt;/label&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;div class="col-sm-9 offset-sm-3"&gt;
            &lt;div class="form-check"&gt;
                &lt;input class="form-check-input" type="checkbox" id="checkAgree" value="agree"&gt;
                &lt;label class="form-check-label" for="checkAgree"&gt;I agree to the &lt;a href="#"&gt;Terms and Conditions&lt;/a&gt;&lt;/label&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="row mb-3"&gt;
        &lt;div class="col-sm-9 offset-sm-3"&gt;
            &lt;input type="submit" class="btn btn-primary" value="Submit"&gt;
            &lt;input type="reset" class="btn btn-secondary ms-2" value="Reset"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </form>
  • Bootstrap List Groups

    Creating List Groups with Bootstrap

    The list groups are very useful and flexible component for displaying lists of elements in a beautiful manner. In most basic form a list group is simply an unordered list with the class .list-group whereas, the list items having the class .list-group-item.

    Example

    <ul class="list-group w-50">
    
    &lt;li class="list-group-item"&gt;Pictures&lt;/li&gt;
    &lt;li class="list-group-item"&gt;Documents&lt;/li&gt;        
    &lt;li class="list-group-item"&gt;Music&lt;/li&gt;
    &lt;li class="list-group-item"&gt;Videos&lt;/li&gt;
    </ul>

    — The output of the above example will look something like this:

    Bootstrap List Group

    Tip: List groups are 100% wide by default. To set their width explicitly you can either use the Bootstrap’s width utility classes (e.g. w-25w-50, etc.) or place them inside grid columns.


    Indicate Disabled and Active Items

    You can simply add the class .active to a .list-group-item to indicate the current active selection. Similarly, you can add .disabled to a .list-group-item to make it look like disabled.

    Example

    <ul class="list-group w-50">
    
    &lt;li class="list-group-item active"&gt;Pictures&lt;/li&gt;
    &lt;li class="list-group-item"&gt;Documents&lt;/li&gt;        
    &lt;li class="list-group-item"&gt;Music&lt;/li&gt;
    &lt;li class="list-group-item disabled"&gt;Videos&lt;/li&gt;
    </ul>

    — The output of the above example will look something like this:

    Bootstrap Disabled and Active Items within List Group

    Tip: In case of list group with linked items, the .disabled class only make the link look like disabled, but the link is still clickable. To actually disable links you need to remove the anchor’s href attribute either using the JavaScript or manually.


    Edge-to-Edge List Groups

    You can optionally add the class .list-group-flush to the list-group element to remove outer borders and rounded corners to create list groups that are edge-to-edge with their parent container.

    Let’s check out the following example to understand how it actually works:

    Example

    <ul class="list-group list-group-flush w-50">
    
    &lt;li class="list-group-item"&gt;Pictures&lt;/li&gt;
    &lt;li class="list-group-item"&gt;Documents&lt;/li&gt;        
    &lt;li class="list-group-item"&gt;Music&lt;/li&gt;
    &lt;li class="list-group-item"&gt;Videos&lt;/li&gt;
    </ul>

    — The output of the above example will look something like this:

    Bootstrap Edge-to-Edge List Group

    Creating Numbered List Groups

    You can also create list groups where items are numbered through simply adding the modifier class .list-group-numbered on the .list-group element, like this:

    Example

    <ol class="list-group list-group-numbered w-50">
    
    &lt;li class="list-group-item"&gt;An item&lt;/li&gt;
    &lt;li class="list-group-item"&gt;A second item&lt;/li&gt;
    &lt;li class="list-group-item"&gt;A third item&lt;/li&gt;
    &lt;li class="list-group-item"&gt;A fourth item&lt;/li&gt;
    </ol>

    — The output of the above example will look something like this:

    Bootstrap Numbered List Group

    Note: Numbers are generated via CSS (as opposed to <ol> element’s default browser styling) for better placement inside list group items and to allow for better customization. See the CSS counter-reset and counter-increment properties to learn more about it.


    List Group with Checkboxes and Radios

    You can also place Bootstrap’s custom checkboxes and radio buttons within the list group items.

    The following example will create a list group with custom checkboxes:

    Example

    <div class="list-group w-50">
    
    &lt;label class="list-group-item"&gt;
        &lt;input type="checkbox" class="form-check-input me-1" name="hobbies"&gt; Music
    &lt;/label&gt;
    &lt;label class="list-group-item"&gt;
        &lt;input type="checkbox" class="form-check-input me-1" name="hobbies"&gt; Travel &amp; Adventure
    &lt;/label&gt;
    &lt;label class="list-group-item"&gt;
        &lt;input type="checkbox" class="form-check-input me-1" name="hobbies"&gt; Reading
    &lt;/label&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap List Group with Checkboxes

    Similarly, you can place custom radio buttons within list group items, like this:

    Example

    <div class="list-group w-50">
    
    &lt;label class="list-group-item"&gt;
        &lt;input type="radio" class="form-check-input me-1" name="gender"&gt; Male
    &lt;/label&gt;
    &lt;label class="list-group-item"&gt;
        &lt;input type="radio" class="form-check-input me-1" name="gender"&gt; Female
    &lt;/label&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap List Group with Radio Buttons

    You’ll learn about custom checkboxes and radio buttons in the Bootstrap custom forms chapter.


    List Group with Linked Items

    You can also link list group items with the little change in HTML markup.

    Just replace the <li> with <a> tag and use <div> element as a parent instead of <ul>. You can also add icons and badges to this list group to make it more elegant. Here’s an example:

    Example

    <div class="list-group w-50">
    
    &lt;a href="#" class="list-group-item list-group-item-action active"&gt;
        &lt;i class="bi-house-fill"&gt;&lt;/i&gt; Home
    &lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action"&gt;
        &lt;i class="bi-camera-fill"&gt;&lt;/i&gt; Pictures
        &lt;span class="badge rounded-pill bg-primary float-end"&gt;145&lt;/span&gt;
    &lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action"&gt;
        &lt;i class="bi-music-note-beamed"&gt;&lt;/i&gt; Music
        &lt;span class="badge rounded-pill bg-primary float-end"&gt;50&lt;/span&gt;
    &lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action"&gt;
        &lt;i class="bi-film"&gt;&lt;/i&gt; Videos
        &lt;span class="badge rounded-pill bg-primary float-end"&gt;8&lt;/span&gt;
    &lt;/a&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap List Group with Linked Items

    Tip: You can use the Bootstrap list group component for creating the sidebar navigation menu, e.g. displaying the list of products or categories on your website.


    List Group with Custom Content

    You can also add nearly any HTML within the list groups with the help of flexbox utilities.

    Here’s an example of a linked list group with headings and paragraph.

    Example

    <div class="list-group">
    
    &lt;a href="#" class="list-group-item list-group-item-action"&gt;
        &lt;div class="d-flex w-100 justify-content-between"&gt;
            &lt;h4&gt;Asteroid detected near earth&lt;/h4&gt;
            &lt;small&gt;1 days ago&lt;/small&gt;
        &lt;/div&gt;        
        &lt;p&gt;Researchers have detected an asteroid of the size of a football field near earth. This asteroid does not pose any threat to the planet, but it is difficult to be tracked.&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action active"&gt;
        &lt;div class="d-flex w-100 justify-content-between"&gt;
            &lt;h4&gt;Scientists found massive black hole&lt;/h4&gt;
            &lt;small&gt;2 days ago&lt;/small&gt;
        &lt;/div&gt;
        &lt;p&gt;Scientists have found an ultra-bright, supermassive black hole at the center of a distant galaxy, whose mass is about a million times that of our Sun.&lt;/p&gt;
    &lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action"&gt;
        &lt;div class="d-flex w-100 justify-content-between"&gt;
            &lt;h4&gt;NASA launches solar probe&lt;/h4&gt;
            &lt;small&gt;3 days ago&lt;/small&gt;
        &lt;/div&gt;        
        &lt;p&gt;NASA launched a Parker space probe in 2018 with the mission of making observations of the outer corona of the Sun. It is the first-ever mission to "touch" the Sun.&lt;/p&gt;
    &lt;/a&gt;
    </div>

    — The output of the above example will look something like this:

    Bootstrap Linked List Group with Custom Content

    List Group with Contextual States

    Like most of the other components you can also use contextual classes on the list group items to apply extra emphasis on them. Here’s an example:

    Example

    <ul class="list-group w-50">
    
    &lt;li class="list-group-item"&gt;A simple default list group item&lt;/li&gt;
    &lt;li class="list-group-item list-group-item-primary"&gt;A simple primary list group item&lt;/li&gt;
    &lt;li class="list-group-item list-group-item-secondary"&gt;A simple secondary list group item&lt;/li&gt;
    &lt;li class="list-group-item list-group-item-success"&gt;A simple success list group item&lt;/li&gt;
    &lt;li class="list-group-item list-group-item-danger"&gt;A simple danger list group item&lt;/li&gt;
    &lt;li class="list-group-item list-group-item-warning"&gt;A simple warning list group item&lt;/li&gt;
    &lt;li class="list-group-item list-group-item-info"&gt;A simple info list group item&lt;/li&gt;
    &lt;li class="list-group-item list-group-item-light"&gt;A simple light list group item&lt;/li&gt;
    &lt;li class="list-group-item list-group-item-dark"&gt;A simple dark list group item&lt;/li&gt;
    </ul>

    — The output of the above example will look something like this:

    Bootstrap List Group with Contextual States

    Similarly, you can use these contextual classes to the linked list group items. You can also use the class .active to specify the active list group item. Here’s an example:

    Example

    <div class="list-group">
    
    &lt;a href="#" class="list-group-item list-group-item-action"&gt;A simple default list group item&lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action list-group-item-primary"&gt;A simple primary list group item&lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action list-group-item-secondary"&gt;A simple secondary list group item&lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action list-group-item-success"&gt;A simple success list group item&lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action list-group-item-danger"&gt;A simple danger list group item&lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action list-group-item-warning"&gt;A simple warning list group item&lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action list-group-item-info"&gt;A simple info list group item&lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action list-group-item-light"&gt;A simple light list group item&lt;/a&gt;
    &lt;a href="#" class="list-group-item list-group-item-action list-group-item-dark"&gt;A simple dark list group item&lt;/a&gt;
    </div>

    Please, check out the Bootstrap tabs chapter to learn how to create dynamic vertical tabs using the Bootstrap list group component without using any JavaScript code.

  • Bootstrap Lists

    Creating Lists With Bootstrap

    You can create three different types of HTML lists:

    • Unordered lists — A list of items in which the order does not explicitly matter. The list items in unordered lists are marked with bullets, e.g. ⚬, ●, etc.
    • Ordered lists — A list of items in which the order does explicitly matter. The list items in ordered lists are marked with numbers, e.g. 1, ⅵ, etc.
    • Definition list — A list of terms with their associated descriptions.

    See the tutorial on HTML lists, to learn more about the different lists types.


    Unstyled Ordered and Unordered Lists

    Sometimes you might need to remove the default styling form the list items. You can do this by simply applying the class .list-unstyled to the respective <ul> or <ol> elements.

    Example

    <ul class="list-unstyled">
    
    &lt;li&gt;Home&lt;/li&gt;
    &lt;li&gt;Products
        &lt;ul&gt;
            &lt;li&gt;Gadgets&lt;/li&gt;
            &lt;li&gt;Accessories&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;About Us&lt;/li&gt;
    &lt;li&gt;Contact&lt;/li&gt;
    </ul>

    — The output of the above example will look something like this:

    Bootstrap Unstyled List

    Note: The .list-unstyled class removes the default list-style and left padding only from the list items which are immediate children of the <ul> or <ol> element.


    Placing Ordered and Unordered List Items Inline

    If you want to create a horizontal menu using the ordered or unordered list you need to place all list items in a single line (i.e. side by side). You can do this simply by adding the class .list-inline to the <ul> or <ol>, and the class .list-inline-item to the child <li> elements.

    Example

    <ul class="list-inline">
    
    &lt;li class="list-inline-item"&gt;Home&lt;/li&gt;
    &lt;li class="list-inline-item"&gt;Products&lt;/li&gt;
    &lt;li class="list-inline-item"&gt;About Us&lt;/li&gt;
    &lt;li class="list-inline-item"&gt;Contact&lt;/li&gt;
    </ul>

    — The output of the above example will look something like this:

    Bootstrap Inline List

    Creating Horizontal Definition Lists

    The terms and descriptions in a definition list can also be aligned horizontally side-by-side using the Bootstrap grid system’s predefined classes. Here’s an example:

    Example

    <dl class="row">
    
    &lt;dt class="col-sm-3"&gt;User Agent&lt;/dt&gt;
    &lt;dd class="col-sm-9"&gt;An HTML user agent is any device that interprets HTML documents.&lt;/dd&gt;
    &lt;dt class="col-sm-3 text-truncate"&gt;Client-side Scripting&lt;/dt&gt;
    &lt;dd class="col-sm-9"&gt;Client-side scripting generally refers to the category of computer programs on the web that are executed by the user's web browser.&lt;/dd&gt;
    &lt;dt class="col-sm-3"&gt;Document Tree&lt;/dt&gt;
    &lt;dd class="col-sm-9"&gt;The tree of elements encoded in the source document.&lt;/dd&gt;
    </dl>

    — The output of the above example will look something like this:

    Bootstrap Horizontal Definition List