Author: Saim Khalid

  • 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
  • Bootstrap Tables

    What is Table?

    The HTML tables are used to present data in grid manner like row and columns. Using Bootstrap you can greatly improve the appearance of table in a quick and easy way.

    See the tutorial on HTML tables to learn more about tables.

    Creating a Simple Table with Bootstrap

    You can create tables with basic styling that has horizontal dividers and small cell padding (8px by default), by just adding the Bootstrap’s class .table to the <table> element.

    Example

    <table class="table">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt; 
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                   
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Simple Table

    Creating Accented Tables

    Bootstrap even provides a handful of contextual classes such as .table-primary.table-secondary.table-success.table-danger.table-warning.table-info.table-light and .table-dark to color tables, table rows or individual cells.

    For example, you can create a dark version of the table (i.e. table with light text on dark backgrounds) by adding the contextual class .table-dark to the .table base class, like this:

    Example

    <table class="table table-dark">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt; 
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                   
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Dark Table

    Similarly, you can use other contextual classes. For instance, the following example uses the class .table-success on the .table to create green colored variant of a table.

    Example

    <table class="table table-success">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt; 
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                   
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Accented Table

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

    Tip: You can use these contextual classes on the .table base class to create colored version of any table such as stripped, hoverable, bordered, compact table, and so on.

    Similar to the tables you can also use these contextual classes to emphasize the rows within a table. Here’s an example of a table with emphasized rows, let’s take a look:

    Example

    <table class="table">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;Bill&lt;/th&gt;
            &lt;th&gt;Payment Date&lt;/th&gt;
            &lt;th&gt;Payment Status&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr class="table-primary"&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Credit Card&lt;/td&gt;
            &lt;td&gt;04/07/2021&lt;/td&gt;
            &lt;td&gt;Waiting for statement&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class="table-secondary"&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Insurance&lt;/td&gt;
            &lt;td&gt;02/07/2021&lt;/td&gt;
            &lt;td&gt;Cancelled&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class="table-success"&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;Water&lt;/td&gt;
            &lt;td&gt;01/07/2021&lt;/td&gt;
            &lt;td&gt;Paid&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class="table-info"&gt;
            &lt;td&gt;4&lt;/td&gt;
            &lt;td&gt;Internet&lt;/td&gt;
            &lt;td&gt;05/07/2021&lt;/td&gt;
            &lt;td&gt;Change plan&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class="table-warning"&gt;
            &lt;td&gt;5&lt;/td&gt;
            &lt;td&gt;Electricity&lt;/td&gt;
            &lt;td&gt;03/07/2021&lt;/td&gt;
            &lt;td&gt;Pending&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class="table-danger"&gt;
            &lt;td&gt;6&lt;/td&gt;
            &lt;td&gt;Telephone&lt;/td&gt;
            &lt;td&gt;06/07/2021&lt;/td&gt;
            &lt;td&gt;Due&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class="table-light"&gt;
            &lt;td&gt;7&lt;/td&gt;
            &lt;td&gt;Car Service&lt;/td&gt;
            &lt;td&gt;08/07/2021&lt;/td&gt;
            &lt;td&gt;Call in to confirm&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class="table-dark"&gt;
            &lt;td&gt;8&lt;/td&gt;
            &lt;td&gt;Gas&lt;/td&gt;
            &lt;td&gt;06/07/2021&lt;/td&gt;
            &lt;td&gt;Payment failed&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Table with Accented Rows

    Creating Tables with Striped Rows

    You can also add zebra-striping to the table rows within the <tbody> by simply adding an additional class .table-striped to the .table base class, as shown below:

    Example

    <table class="table table-striped">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;  
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                  
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Zebra Striped Table

    Creating Bordered Tables

    You can add borders on all sides of the table and cells by adding the modifier class .table-bordered to the .table base class, as shown in the following example:

    Example

    <table class="table table-bordered">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                    
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Table with Borders

    Creating Borderless Tables

    You can also create borderless tables using the class .table-borderless on the .table element.

    Example

    <table class="table table-borderless">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                    
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Borderless Table

    Enabling Hover State on Table Rows

    You can also enable a hover state on table rows within a <tbody> element by adding the modifier class .table-hover to the .table base class. Let’s try out the following example:

    Example

    <table class="table table-hover">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                    
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Table with Hover States

    Creating Small or Compact Tables

    You can also make your tables more compact and save the space through adding the modifier class .table-sm to the .table base class. The .table-sm class makes the table compact by cutting all cell padding in half. Let’s take a look at the following example:

    Example

    <table class="table table-sm">
    
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                    
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Compact Table

    Setting Table Head Colors

    Similar to light and dark tables, you can use the modifier classes .table-light or .table-dark on the <thead> element to make it appear in light or dark gray.

    The following example will create a table with light gray background head.

    Example

    <table class="table">
    
    &lt;thead class="table-light"&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                    
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Table with Light Head

    The following example will create a table with dark gray background head.

    Example

    <table class="table">
    
    &lt;thead class="table-dark"&gt;
        &lt;tr&gt;
            &lt;th&gt;#&lt;/th&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Last Name&lt;/th&gt;
            &lt;th&gt;Email&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Clark&lt;/td&gt;
            &lt;td&gt;Kent&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Peter&lt;/td&gt;
            &lt;td&gt;Parker&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;John&lt;/td&gt;
            &lt;td&gt;Carter&lt;/td&gt;
            &lt;td&gt;[email protected]&lt;/td&gt;
        &lt;/tr&gt;                    
    &lt;/tbody&gt;
    </table>

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

    Bootstrap Table with Dark Head

    Creating Responsive Tables with Bootstrap

    You can also create responsive tables to enable horizontal scrolling on small devices.

    To make any table responsive just place it inside a <div> element and apply the .table-responsive class on it. You can also specify when the table should have a scrollbar, based on the viewport width (i.e. breakpoints), using the classes .table-responsive{-sm|-md|-lg|-xl}.

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

    Example

    <div class="table-responsive"> 
    
    &lt;table class="table"&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;#&lt;/th&gt;
                &lt;th&gt;First Name&lt;/th&gt;
                &lt;th&gt;Last Name&lt;/th&gt;
                &lt;th&gt;Email&lt;/th&gt;
                &lt;th&gt;Biography&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
  • Bootstrap Typography

    Working with Headings

    You can define all HTML headings, <h1> through <h6> — In the same way you define in simple HTML document. You can also utilize the heading classes .h1 through .h6 on other elements, if you want to apply the style on element’s text same as headings.

    Example

    <h1>h1. Bootstrap heading</h1>
    <h2>h2. Bootstrap heading</h2>
    <h3>h3. Bootstrap heading</h3>
    <h4>h4. Bootstrap heading</h4>
    <h5>h5. Bootstrap heading</h5>
    <h6>h6. Bootstrap heading</h6>

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

    Bootstrap Headings

    Customizing Headings

    Additionally, you can use the <small> tag with .text-muted class to display the secondary text of any heading in a smaller and lighter variation. Here’s an example:

    Example

    <h2>
    
    Fancy display heading
    &lt;small class="text-muted"&gt;With faded secondary text&lt;/small&gt;
    </h2>

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

    Bootstrap Headings with Secondary Text

    Display Headings

    Bootstrap also provides display headings that can be used when you need a heading to stand out. Display headings are displayed in larger font-size but lighter font-weight.

    Six different display headings are available. Here’s is an example:

    Example

    <h1 class="display-1">Display Heading 1</h1>
    <h1 class="display-2">Display Heading 2</h1>
    <h1 class="display-3">Display Heading 3</h1>
    <h1 class="display-4">Display Heading 4</h1>
    <h1 class="display-5">Display Heading 5</h1>
    <h1 class="display-6">Display Heading 6</h1>

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

    Bootstrap Display Headings

    Working with Paragraphs

    Bootstrap’s global default font-size is 1rem (typically 16px), with a line-height of 1.5 (typically 24px), which is applied to the <body> element as well as all the paragraphs i.e. the <p> elements. In addition to that margin-bottom of 1rem is also applied to all the paragraphs.

    You can also make a paragraph stand out by adding the class .lead on it.

    Example

    <p>This is how a normal paragraph looks like in Bootstrap.</p>
    <p class="lead">This is how a paragraph stands out in Bootstrap.</p>

    — The HTML code in the above examples will produce the following result:

    Bootstrap Paragraphs

    Tip: In CSS rem stands for “root em”. 1rem is equal to the font size of the root element (i.e. the <html> element), which is 16px in most browsers by default.


    Text Alignment

    You can easily align text to left, right, and center using the text alignment classes.

    Example

    <p class="text-start">Left aligned text on all viewport sizes.</p>
    <p class="text-center">Center aligned text on all viewport sizes.</p>
    <p class="text-end">Right aligned text on all viewport sizes.</p>

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

    Bootstrap Text Alignment

    You can also align text based on screen size using the responsive text alignment classes. These classes use the same viewport width breakpoints as the grid system.

    Example

    <p class="text-sm-center">Text will be center aligned on small sized (sm) viewports and up.</p>
    <p class="text-md-center">Text will be center aligned on medium sized (md) viewports and up.</p>
    <p class="text-lg-center">Text will be center aligned on large sized (lg) viewports and up.</p>
    <p class="text-xl-center">Text will be center aligned on extra-large sized (xl) viewports and up.</p>

    Text Formatting

    You are free to use text formatting tags like <strong><i><small> to make your text bold, italic, small and so on, in the same way you do in simple HTML page. Here’s an example:

    Example

    <p><b>This is bold text</b></p>
    <p><code>This is computer code</code></p>
    <p><em>This is emphasized text</em></p>
    <p><i>This is italic text</i></p>
    <p><mark>This is highlighted text</mark></p>
    <p><small>This is small text</small></p>
    <p><strong>This is strongly emphasized text</strong></p>
    <p>This is <sub>subscript</sub> and <sup>superscript</sup></p>
    <p><ins>This text is inserted to the document</ins></p>
    <p><del>This text is deleted from the document</del></p>

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

    Bootstrap Text Formatting

    Text Transformation

    You can also transform the text to lowercase, uppercase or make them capitalize.

    Example

    <p class="text-lowercase">The quick brown fox jumps over the lazy dog.</p>
    <p class="text-uppercase">The quick brown fox jumps over the lazy dog.</p>
    <p class="text-capitalize">The quick brown fox jumps over the lazy dog.</p>

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

    Bootstrap Text Transformation Classes

    Text Coloring

    Colors are the powerful method of conveying important information in website design.

    Bootstrap has handful of emphasis utility classes that can be used for this purpose such as showing success message in green color, warning or error message in red color, etc.

    Example

    <p class="text-primary">Primary: Please read the instructions carefully before proceeding.</p>
    <p class="text-secondary">Secondary: This is featured has been removed from the latest version.</p>
    <p class="text-success">Success: Your message has been sent successfully.</p>
    <p class="text-info">Info: You must agree with the terms and conditions to complete the sign up process.</p>
    <p class="text-warning">Warning: There was a problem with your network connection.</p>
    <p class="text-danger">Danger: An error has been occurred while submitting your data.</p>
    <p class="text-muted">Muted: This paragraph of text is grayed out.</p>

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

    Bootstrap Text Emphasis Classes

    Please, check out the Bootstrap helper classes chapter to learn about other text coloring and background coloring classes, as well as various other utility classes.


    Styling Blockquotes

    You can also give pretty look to your blockquotes — Just define the blockquotes using the standard <blockquote> element and bootstrap’s style sheet will do the rest.

    Example

    <blockquote class="blockquote">
    
    &lt;p&gt;Imagination is more important than knowledge.&lt;/p&gt;
    </blockquote>

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

    Bootstrap Blockquotes

    When providing attribution, wrap your <blockquote> in a <figure> element and use a <figcaption> or a block level element (e.g., <p>) with the .blockquote-footer class, like this:

    Example

    <figure>
    
    &lt;blockquote class="blockquote"&gt;
        &lt;p&gt;The world is a dangerous place to live; not because of the people who are evil, but because of the people who don't do anything about it.&lt;/p&gt;
    &lt;/blockquote&gt;
    &lt;figcaption class="blockquote-footer"&gt;by &lt;cite&gt;Albert Einstein&lt;/cite&gt;&lt;/figcaption&gt;
    </figure>

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

    Bootstrap Blockquote with Attribution

    You can also align blockquotes to the right or center by simply applying the text alignment classes .text-end or .text-center on the <blockquote> or <figure> element.


    Truncating Long Text

    For longer text, you can use the class .text-truncate to truncate the text with an ellipsis. The display property value of the element must be inline-block or block.

    It is particularly helpful in a situation where you want to display a piece of text in a single line but there is no enough space available. Let’s try out an example and see how it works:

    Example

    <!-- Block level element -->
    <div class="row">
    
    &lt;div class="col-2 text-truncate"&gt;
        The quick brown fox jumps over the lazy dog.
    &lt;/div&gt;
    </div> <!-- Inline level element --> <span class="d-inline-block text-truncate" style="max-width: 100px;">
    The quick brown fox jumps over the lazy dog.
    </span>

    Text wrapping and Overflow

    You can use the class .text-wrap to wrap the text within an element by overwriting its white-space property if it is set to pre or nowrap, such as Bootstrap badge components.

    Similarly, you can use the class .text-nowrap to prevent text from wrapping within an element.

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

    Example

    <div class="badge bg-primary text-wrap" style="width: 6rem;">
    
    This text will wrap.
    </div> <div class="bg-warning text-nowrap" style="width: 6rem;">
    This text will overflow the element's box.
    </div>

    Wrapping Long Word

    You can use the class .text-break to prevent long word from breaking your layout.

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

    Example

    <div class="row">
    
    &lt;div class="col-2"&gt;
        &lt;p class="text-break"&gt;veryveryveryveryveryveryverylongword&lt;/p&gt;
    &lt;/div&gt;
    </div>
  • Bootstrap Responsive Layout

    What is Responsive Web Design

    Responsive web design is a process of designing and building websites to provide better accessibility and optimal viewing experience to the user by optimizing it for different devices.

    With the growing trend of smart phones and tablets, it has become almost unavoidable to ignore the optimization of sites for mobile devices. Responsive web design is a preferable alternative and an efficient way to target a wide range of devices with much less efforts.

    Responsive layouts automatically adjust and adapts to any device screen size, whether it is a desktop, a laptop, a tablet, or a mobile phone. See the following Illustration.

    Bootstrap Responsive Design Illustration

    Creating Responsive Layout with Bootstrap

    With the Bootstrap powerful mobile first flexbox grid system creating the responsive and mobile friendly websites and applications has become much easier.

    Bootstrap is responsive and mobile friendly from the start. Its six tier grid classes provides better control over the layout as well as how it will be rendered on different types of devices like mobile phones, tablets, laptops and desktops, large screen devices, and so on.

    The following example will create a responsive layout that is rendered as 4 column layout in extra-large devices (viewport ≥ 1200px), and 3 column layout in large devices (992px ≤ viewport < 1200px), whereas 2 column layout in medium devices (768px ≤ viewport < 992px), and 1 column layout in small and extra-small devices (viewport < 768px). Let’s try it out and see how it works:

    Example

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Responsive Layout Example</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    
    &lt;div class="container-fluid"&gt;
        &lt;a href="#" class="navbar-brand"&gt;Tutorial Republic&lt;/a&gt;
        &lt;button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbarCollapse"&gt;
  • Bootstrap Fluid Layout

    Creating Fluid Layout with Bootstrap

    In Bootstrap you can use the class .container-fluid to create fluid layouts to utilize the 100% width of the viewport across all devices (extra small, small, medium, large, extra large, and extra-extra large).

    The class .container-fluid simply applies the width: 100% instead of different width for different viewport sizes. However, the layout will still responsive and you can use the grid classes as usual. See the tutorial on Bootstrap grid system to learn more about grid classes.

    The following example will create a fluid layout that covers 100% width of the screen.

    Example

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Fluid Layout Example</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/bootstrap.bundle.min.js"></script>
    </head>
    <body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    
    &lt;div class="container-fluid"&gt;
        &lt;a href="#" class="navbar-brand"&gt;Tutorial Republic&lt;/a&gt;
        &lt;button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbarCollapse"&gt;
            &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt;
        &lt;/button&gt;
        &lt;div class="collapse navbar-collapse" id="navbarCollapse"&gt;
            &lt;div class="navbar-nav"&gt;
                &lt;a href="#" class="nav-item nav-link active"&gt;Home&lt;/a&gt;
                &lt;a href="#" class="nav-item nav-link"&gt;Services&lt;/a&gt;
                &lt;a href="#" class="nav-item nav-link"&gt;About&lt;/a&gt;
                &lt;a href="#" class="nav-item nav-link"&gt;Contact&lt;/a&gt;
            &lt;/div&gt;
            &lt;div class="navbar-nav ms-auto"&gt;  
                &lt;a href="#" class="nav-item nav-link"&gt;Register&lt;/a&gt;            	
                &lt;a href="#" class="nav-item nav-link"&gt;Login&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    </nav> <div class="container-fluid">
    &lt;div class="p-5 my-4 bg-light rounded-3"&gt;
        &lt;h1&gt;Learn to Create Websites&lt;/h1&gt;
        &lt;p class="lead"&gt;In today's world internet is the most popular way of connecting with the people. At &lt;a href="https://www.tutorialrepublic.com" class="text-success" target="_blank"&gt;tutorialrepublic.com&lt;/a&gt; you will learn the essential web development technologies along with real life practice examples, so that you can create your own website to connect with the people around the world.&lt;/p&gt;
        &lt;p&gt;&lt;a href="https://www.tutorialrepublic.com" target="_blank" class="btn btn-success btn-lg"&gt;Get started today&lt;/a&gt;&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class="row"&gt;
        &lt;div class="col-md-4"&gt;
            &lt;h2&gt;HTML&lt;/h2&gt;
            &lt;p&gt;HTML is the standard markup language for describing the structure of the web pages. Our HTML tutorials will help you to understand the basics of latest HTML5 language, so that you can create your own web pages or website.&lt;/p&gt;
            &lt;p&gt;&lt;a href="https://www.tutorialrepublic.com/html-tutorial/" target="_blank" class="btn btn-success"&gt;Learn More &amp;raquo;&lt;/a&gt;&lt;/p&gt;
        &lt;/div&gt;
        &lt;div class="col-md-4"&gt;
            &lt;h2&gt;CSS&lt;/h2&gt;
            &lt;p&gt;CSS is used for describing the presentation of web pages. CSS can save a lot of time and effort. Our CSS tutorials will help you to learn the essentials of latest CSS3, so that you can control the style and layout of your website.&lt;/p&gt;
            &lt;p&gt;&lt;a href="https://www.tutorialrepublic.com/css-tutorial/" target="_blank" class="btn btn-success"&gt;Learn More &amp;raquo;&lt;/a&gt;&lt;/p&gt;
        &lt;/div&gt;
        &lt;div class="col-md-4"&gt;
            &lt;h2&gt;Bootstrap&lt;/h2&gt;
            &lt;p&gt;Bootstrap is a powerful front-end framework for faster and easier web development. Our Bootstrap tutorials will help you to learn all the features of latest Bootstrap 4 framework so that you can easily create responsive websites.&lt;/p&gt;
            &lt;p&gt;&lt;a href="https://www.tutorialrepublic.com/twitter-bootstrap-tutorial/" target="_blank" class="btn btn-success"&gt;Learn More &amp;raquo;&lt;/a&gt;&lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;hr&gt;
    &lt;footer&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-md-6"&gt;
                &lt;p&gt;Copyright &amp;copy; 2021 Tutorial Republic&lt;/p&gt;
            &lt;/div&gt;
            &lt;div class="col-md-6 text-md-end"&gt;
                &lt;a href="#" class="text-dark"&gt;Terms of Use&lt;/a&gt; 
                &lt;span class="text-muted mx-2"&gt;|&lt;/span&gt; 
                &lt;a href="#" class="text-dark"&gt;Privacy Policy&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/footer&gt;
    </div> </body> </html>

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

    Bootstrap Fluid Layout