Bootstrap 4 CSS Tutorial (Part 2)

• 11 minutes READ

In the last article, we talked about Bootstrap 4 CSS. In today’s article we’re going to continue with some more CSS and the JavaScript options that are essential for every Bootstrap-based website.

Looking for a Bootstrap online builder?

Startup Bootstrap Builder

We’re going to discuss the following topics:

Video Tutorial

Getting Started

Navbar

The navbar is one of the prominent features of every Bootstrap-based website. The core code includes styling for a site logo and basic navigation links.

To create a navbar, follow these steps:

No-Code Email Template Builder

With Postcards Email Builder you can create and edit email templates online without any coding skills! Includes more than 100 components to help you create custom emails templates faster than ever before.

Free Email BuilderFree Email Templates
  1. Create a <nav> tag with a class of .navbar along with a .navbar-dark bg-inverse class (this will be the color scheme of the navbar).
  2. Inside the <nav> tag put an anchor tag with a class .navbar-brand. This will be the logo of the navbar.
  3. Add an unordered lists with a class .nav along with a .navbar-nav class.
  4. For each list item add a class .nav-item and a class .nav-link for each link inside.

You can check the sample code and results below.

&amp;lt;nav class=&amp;quot;navbar navbar-dark bg-inverse&amp;quot;&amp;gt;
&amp;lt;!-- Brand --&amp;gt;
&amp;lt;a class=&amp;quot;navbar-brand&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Logo&amp;lt;/a&amp;gt;

&amp;lt;!-- Links --&amp;gt;
&amp;lt;ul class=&amp;quot;nav navbar-nav&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/nav&amp;gt;

Putting these codes together will give you exactly the same result as the image.

Navbar

Note: You can use the class .active to specify a link as the current page and add a <span> with .sr-only class for screen readers.

Navbar Color Scheme

If you want to add a different background color to your navbar you can use the combination of a simple link color modifier class and background-color utilities (such .bg-primary, .bg-warning).

Here are some examples:

&amp;lt;nav class=&amp;quot;navbar navbar-light bg-faded&amp;quot;&amp;gt;
  &amp;lt;!-- Navbar content --&amp;gt;
&amp;lt;/nav&amp;gt;
&amp;lt;nav class=&amp;quot;navbar navbar-dark bg-primary&amp;quot;&amp;gt;
  &amp;lt;!-- Navbar content --&amp;gt;
&amp;lt;/nav&amp;gt;
&amp;lt;nav class=&amp;quot;navbar navbar-light&amp;quot; style=&amp;quot;background-color: #e74c3c;&amp;quot;&amp;gt;
  &amp;lt;!-- Navbar content --&amp;gt;
&amp;lt;/nav&amp;gt;

Using the following classes and styles with our previous code, you’ll get the following result.

Navbar Color Scheme

Low-Code Website Builders

With Startup App and Slides App you can build unlimited websites using the online website editor which includes ready-made designed and coded elements, templates and themes.

Try Startup App Try Slides AppOther Products

Navbar Alignment

If you want to align the elements of your navbar you can use the .pull-left or .pull-right class to achieve this.

Check out the code below.

&amp;lt;nav class=&amp;quot;navbar navbar-light bg-danger&amp;quot;&amp;gt;
&amp;lt;!-- Brand --&amp;gt;
&amp;lt;a class=&amp;quot;navbar-brand&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Logo&amp;lt;/a&amp;gt;

&amp;lt;!-- Links --&amp;gt;
&amp;lt;ul class=&amp;quot;nav navbar-nav pull-right&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item active&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile &amp;lt;span class=&amp;quot;sr-only&amp;quot;&amp;gt;(current)&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/nav&amp;gt;

The code above will display the image below.

Navbar Alignment

Navbar Dropdowns

Dropdowns menus are another important component of any navigation bar. You can add a dropdown to one of the li elements of the navbar-nav list by adding the .dropdown class. You also need to add the data attribute data-toggle=”dropdown” along with the aria-haspopup=”true” to indicate whether this element may display a pop-up window as well as the aria-expanded=”false” to indicate the state of a collapsible element. For each item of the dropdown link you must add the class .dropdown-item to add styles on it.

See the example code below.

&amp;lt;nav class=&amp;quot;navbar navbar-light bg-danger&amp;quot;&amp;gt;

&amp;lt;!-- Brand --&amp;gt;
&amp;lt;a class=&amp;quot;navbar-brand&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Logo&amp;lt;/a&amp;gt;

&amp;lt;!-- Links --&amp;gt;
&amp;lt;ul class=&amp;quot;nav navbar-nav pull-right&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item dropdown&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link dropdown-toggle&amp;quot; data-toggle=&amp;quot;dropdown&amp;quot; href=&amp;quot;#&amp;quot; role=&amp;quot;button&amp;quot; aria-haspopup=&amp;quot;true&amp;quot; aria-expanded=&amp;quot;false&amp;quot;&amp;gt;
Profile
&amp;lt;/a&amp;gt;
&amp;lt;div class=&amp;quot;dropdown-menu&amp;quot; aria-labelledby=&amp;quot;Preview&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;dropdown-item&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Dropdown Link 1&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;dropdown-item&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Dropdown Link 2&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;dropdown-item&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Dropdown Link 3&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/nav&amp;gt;

Check out the image output below.

Navbar Dropdowns

Fixed Navbar

You can also position your navbar in fix position either to the top or bottom of the screen upon mouse scroll by using the .navbar-fixed-top or .navbar-fixed-bottom. Check out the structure code below.

&amp;lt;nav class=&amp;quot;navbar navbar-fixed-top navbar-dark bg-primary&amp;quot;&amp;gt;
&amp;lt;!-- Content Here! --&amp;gt;
&amp;lt;/nav&amp;gt;

&amp;lt;nav class=&amp;quot;navbar navbar-fixed-bottom navbar-dark bg-danger&amp;quot;&amp;gt;
&amp;lt;!-- Content Here! --&amp;gt;
&amp;lt;/nav&amp;gt;

Responsive Navbar

To create a responsive navbar, you can use the .navbar-toggler class on the button along with the data attribute data-toggle=”collapse” and data-target with the id of the navbar. You can then add one of the .navbar-toggleable-* classes. This navbar will collapse at a given viewport width depending on the screen size you set on .navbar-toggleable-* class. You can use one of the following classes:

  • navbar-toggleable-xs
  • navbar-toggleable-sm
  • navbar-toggleable-md
  • navbar-toggleable-lg
  • navbar-toggleable-xl

Let’s see this in action.

&amp;lt;nav class=&amp;quot;navbar navbar-light bg-primary&amp;quot;&amp;gt;
&amp;lt;!-- Toggle Button --&amp;gt;
&amp;lt;button class=&amp;quot;navbar-toggler &amp;quot; type=&amp;quot;button&amp;quot; data-toggle=&amp;quot;collapse&amp;quot; data-target=&amp;quot;#nav-content&amp;quot;&amp;gt;
☰
&amp;lt;/button&amp;gt;

&amp;lt;!-- Nav Content --&amp;gt;
&amp;lt;div class=&amp;quot;collapse navbar-toggleable-lg&amp;quot; id=&amp;quot;nav-content&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;navbar-brand&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Logo&amp;lt;/a&amp;gt;
&amp;lt;ul class=&amp;quot;nav navbar-nav&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/nav&amp;gt;

See image result below.

Responsive Navbar

Navs

Bootstrap 4 provides a variety of options for styling navigation elements. All of them share the same markup and base .nav class  either within the <ul> or <nav> tag. You also need to add the class .nav-item for each list items and the .nav-link class for each anchor tags.

Bootstrap also provides a helper class, .active to specify the active page. Let’s try this on both <ul> and <nav> tag.

&amp;lt;!-- Using &amp;lt;ul&amp;gt; --&amp;gt;  
&amp;lt;ul class=&amp;quot;nav&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link active&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Portfolio&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;br/&amp;gt;
&amp;lt;!-- Using &amp;lt;nav&amp;gt; --&amp;gt; 
&amp;lt;nav class=&amp;quot;nav&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link active&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Portfolio&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/nav&amp;gt;

Putting this code together will display the image below.

Navs

Tabular Navigation

To create a tabbed navigation menu, you need to start with a basic unordered list with the class of .nav along with the .nav-tabs class. You also need to add the class .nav-item for each list items and the .nav-link class for each links. In addition, you can then add the class .active to specify the active page.

&amp;lt;ul class=&amp;quot;nav nav-tabs&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link active&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Portfolio&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

See image result below.

Tabular Navigation

Basic Pills Navigation

To create a pill navigation use .nav-pills instead of .nav-tabs.

&amp;lt;ul class=&amp;quot;nav nav-pills&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link active&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Portfolio&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

See image result below.

Basic Pills Navigation

Vertical Pills

To stack pills vertically you just need to add .nav-stacked to the class .nav-pills.

&amp;lt;ul class=&amp;quot;nav nav-pills nav-stacked&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link active&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Portfolio&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

With the code above you’ll get the image result below.

Vertical Pills

Dropdowns

Navigation menus can also have a similar syntax with dropdown menus. Just add .dropdown class to an <li> element followed by its classes and data-attributes. See the code below.

&amp;lt;ul class=&amp;quot;nav nav-tabs&amp;quot;&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link active&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Home&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Profile&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;li class=&amp;quot;nav-item dropdown&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link dropdown-toggle&amp;quot; data-toggle=&amp;quot;dropdown&amp;quot; href=&amp;quot;#&amp;quot; role=&amp;quot;button&amp;quot; aria-haspopup=&amp;quot;true&amp;quot; aria-expanded=&amp;quot;false&amp;quot;&amp;gt;
Portfolio
&amp;lt;/a&amp;gt;
&amp;lt;div class=&amp;quot;dropdown-menu&amp;quot; aria-labelledby=&amp;quot;Preview&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;dropdown-item&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Website&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;dropdown-item&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Photography&amp;lt;/a&amp;gt;
&amp;lt;a class=&amp;quot;dropdown-item&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Video&amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/li&amp;gt;
 &amp;lt;li class=&amp;quot;nav-item&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;nav-link&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;Contact&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

See the image result below.

Dropdowns

Forms

One of the Bootstrap 4’s great feature is the ability to create forms with ease. Bootstrap makes it easy by just using simple HTML markup and extended classes for different styles of forms.

To use Bootstrap forms, simply add the class .form-control class to any of the form components such as <input>, <textarea>, and <select> tag.

You also need to wrap each form component to a <fieldset> tag along with class .form-group.

Here’s the sample code.

&amp;lt;form&amp;gt;
&amp;lt;fieldset class=&amp;quot;form-group&amp;quot;&amp;gt;
&amp;lt;label for=&amp;quot;first_name&amp;quot;&amp;gt;Full Name&amp;lt;/label&amp;gt;
&amp;lt;input type=&amp;quot;text&amp;quot; class=&amp;quot;form-control&amp;quot; id=&amp;quot;full_name&amp;quot; name=&amp;quot;full_name&amp;quot;&amp;gt;
&amp;lt;/fieldset&amp;gt;
&amp;lt;fieldset class=&amp;quot;form-group&amp;quot;&amp;gt;
&amp;lt;label for=&amp;quot;last_name&amp;quot;&amp;gt;Email&amp;lt;/label&amp;gt;
&amp;lt;input type=&amp;quot;email&amp;quot; class=&amp;quot;form-control&amp;quot; id=&amp;quot;email&amp;quot; name=&amp;quot;email&amp;quot;&amp;gt;
&amp;lt;/fieldset&amp;gt;
&amp;lt;button type=&amp;quot;submit&amp;quot; class=&amp;quot;btn btn-primary&amp;quot;&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;

Basic Form Output:

Forms

Optional Form Styles

You can also add a color for each fieldset. There are three classes that you can use.

  • has-success
  • has-warning
  • has-error

These classes can be use inside a <fieldset>, <div>, any tag element. In addition you can also add icons by using .form-control-* class. Check the list here.

Let’s see it in action.

&amp;lt;fieldset class=&amp;quot;form-group has-success&amp;quot;&amp;gt;
&amp;lt;label class=&amp;quot;control-label&amp;quot; for=&amp;quot;username&amp;quot;&amp;gt;Success&amp;lt;/label&amp;gt;
&amp;lt;input type=&amp;quot;text&amp;quot; class=&amp;quot;form-control form-control-success&amp;quot;&amp;gt;
&amp;lt;/fieldset&amp;gt;
  
&amp;lt;fieldset class=&amp;quot;form-group has-error&amp;quot;&amp;gt;
&amp;lt;label class=&amp;quot;control-label&amp;quot; for=&amp;quot;phone&amp;quot;&amp;gt;Error&amp;lt;/label&amp;gt;
&amp;lt;input type=&amp;quot;text&amp;quot; class=&amp;quot;form-control form-control-error&amp;quot;&amp;gt;
&amp;lt;/fieldset&amp;gt;
  
&amp;lt;fieldset class=&amp;quot;form-group has-warning&amp;quot;&amp;gt;
&amp;lt;label class=&amp;quot;control-label&amp;quot; for=&amp;quot;password&amp;quot;&amp;gt;Warning&amp;lt;/label&amp;gt;
&amp;lt;input type=&amp;quot;email&amp;quot; class=&amp;quot;form-control form-control-warning&amp;quot;&amp;gt;
&amp;lt;/fieldset&amp;gt;

Optional Form Styles

Horizontal Form

Bootstrap comes with predefined horizontal form formats. This one stands apart from the others not only on its markup, but also in the display of the form.

To use Bootstrap horizontal form, use .form-horizontal class inside the <form> tag. You also need to add .form-control-label class to the <label> tag for it to appear vertically center.

Note: Don’t forget to specify how many columns each form component should span via .col-*-*.

See the example below.

&amp;lt;form action=&amp;quot;&amp;quot; class=&amp;quot;form-horizontal&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;container&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;form-group row&amp;quot;&amp;gt;
&amp;lt;label for=&amp;quot;full name&amp;quot; class=&amp;quot;col-xs-3 form-control-label&amp;quot;&amp;gt;Full Name&amp;lt;/label&amp;gt;
&amp;lt;div class=&amp;quot;col-xs-9&amp;quot;&amp;gt;
&amp;lt;input type=&amp;quot;text&amp;quot; class=&amp;quot;form-control&amp;quot; id=&amp;quot;full_name&amp;quot; name=&amp;quot;full_name&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;form-group row&amp;quot;&amp;gt;
&amp;lt;label for=&amp;quot;email&amp;quot; class=&amp;quot;col-xs-3 form-control-label&amp;quot;&amp;gt;Email&amp;lt;/label&amp;gt;
&amp;lt;div class=&amp;quot;col-xs-9&amp;quot;&amp;gt;
&amp;lt;input type=&amp;quot;email&amp;quot; class=&amp;quot;form-control&amp;quot; id=&amp;quot;email&amp;quot; name=&amp;quot;email&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;form-group row&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;col-xs-offset-3 col-xs-9&amp;quot;&amp;gt;
&amp;lt;button type=&amp;quot;submit&amp;quot; class=&amp;quot;btn btn-primary&amp;quot;&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/form&amp;gt;

The code above will have a result like this:

Horizontal Form

Inline Form

To create a form where all of the elements are inline and labels are left-aligned, simply add a class .form-inline to the <form> tag and use the .form-group class within a <div> or <fieldset> tag to organize each field set. See the code example below.

&amp;lt;form action=&amp;quot;&amp;quot; class=&amp;quot;form-inline&amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;form-group&amp;quot;&amp;gt;
&amp;lt;label for=&amp;quot;full_name&amp;quot;&amp;gt;Full Name&amp;lt;/label&amp;gt;
&amp;lt;input type=&amp;quot;text&amp;quot; class=&amp;quot;form-control&amp;quot; id=&amp;quot;full_name&amp;quot; name=&amp;quot;full_name&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
  
&amp;lt;div class=&amp;quot;form-group&amp;quot;&amp;gt;
&amp;lt;label for=&amp;quot;email&amp;quot;&amp;gt;Email&amp;lt;/label&amp;gt;
&amp;lt;input type=&amp;quot;email&amp;quot; class=&amp;quot;form-control&amp;quot; id=&amp;quot;email&amp;quot; name=&amp;quot;email&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;button type=&amp;quot;submit&amp;quot; class=&amp;quot;btn btn-primary&amp;quot;&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;

The codes above will have a result like this:

Inline Form

Media Objects

Social media websites such as Facebook and Twitter use the media object. The goal of the media object is to make the code for developing these blocks of information shorter.

Bootstrap Media Object align text and media objects next to each other. This gives the user the option to quickly float the object either to the right or to the left.

You can create a Media Object in three steps.

  • Create a <div> with a class .media.
  • Insert .media-object class to any element you want to float such as <img> and <video> For the alignment of the media element you can add one of the following class to the parent element to control the position of media objects (.media-middle, .media-bottom, .media-left, .media-right). By default the media-objects are set to top-aligned position.
  • Create another <div> inside the class .media and then insert .media-body class In addition, you need to add the .media-heading to any heading tag inside the .media-body <div>.

Let’s try an example:

&amp;lt;div class=&amp;quot;media&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;media-left&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;
&amp;lt;img class=&amp;quot;media-object&amp;quot; src=&amp;quot;https://goo.gl/sLxT6P&amp;quot; width=&amp;quot;100&amp;quot; alt=&amp;quot;&amp;quot;&amp;gt;
&amp;lt;/a&amp;gt;
&amp;lt;div class=&amp;quot;media-body&amp;quot;&amp;gt;
&amp;lt;h4 class=&amp;quot;media-heading&amp;quot;&amp;gt;Media Heading&amp;lt;/h4&amp;gt;
&amp;lt;p&amp;gt; Bootstrap is the most popular HTML, CSS, and JS framework in the world for building responsive, mobile-first projects on the web. &amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
	
&amp;lt;div class=&amp;quot;media&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;media-left&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;
&amp;lt;img class=&amp;quot;media-object&amp;quot; src=&amp;quot;https://goo.gl/sLxT6P&amp;quot; width=&amp;quot;100&amp;quot; alt=&amp;quot;&amp;quot;&amp;gt;
&amp;lt;/a&amp;gt;
&amp;lt;div class=&amp;quot;media-body&amp;quot;&amp;gt;
&amp;lt;h4 class=&amp;quot;media-heading&amp;quot;&amp;gt;Media Heading&amp;lt;/h4&amp;gt;
&amp;lt;p&amp;gt;Bootstrap is the most popular HTML, CSS, and JS framework in the world for building responsive, mobile-first projects on the web.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

With codes above, you’ll get exactly the same result like the image below.

Media Objects

Nested Media Objects

You can also nest media objects by simply adding another media object structure inside the <div> with a .media-body class. See the code below.

&amp;lt;div class=&amp;quot;media&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;media-left&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;
&amp;lt;img class=&amp;quot;media-object&amp;quot; src=&amp;quot;https://goo.gl/sLxT6P&amp;quot; width=&amp;quot;100&amp;quot; alt=&amp;quot;&amp;quot;&amp;gt;
&amp;lt;/a&amp;gt;
&amp;lt;div class=&amp;quot;media-body&amp;quot;&amp;gt;
&amp;lt;h4 class=&amp;quot;media-heading&amp;quot;&amp;gt;Media Object&amp;lt;/h4&amp;gt;
&amp;lt;p&amp;gt;Bootstrap is the most popular HTML, CSS, and JS framework in the world for building responsive, mobile-first projects on the web.&amp;lt;/p&amp;gt;
&amp;lt;!-- Nested Media Object --&amp;gt;
&amp;lt;div class=&amp;quot;media&amp;quot;&amp;gt;
&amp;lt;a class=&amp;quot;media-left&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt;
&amp;lt;img class=&amp;quot;media-object&amp;quot; src=&amp;quot;https://goo.gl/sLxT6P&amp;quot;  width=&amp;quot;80&amp;quot; alt=&amp;quot;&amp;quot;&amp;gt;
&amp;lt;/a&amp;gt;
&amp;lt;div class=&amp;quot;media-body&amp;quot;&amp;gt;
&amp;lt;h4 class=&amp;quot;media-heading&amp;quot;&amp;gt;Nested Media Object&amp;lt;/h4&amp;gt;
&amp;lt;p&amp;gt;Bootstrap is the most popular HTML, CSS, and JS framework in the world for building responsive, mobile-first projects on the web.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;!-- Nested Media Object --&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

Putting it all together, you’ll get a result like the image below.

Nested Media Objects

Progress Bar

The progress bar displays the assets that are loading, in progress, or that there is an action taking place within the page. The Bootstrap default progress bar has a light gray background and a blue progress bar.

To create a progress bar, use the html5 <progress> tag with a class of .progress. Inside, insert a value attribute from 0 to 100 to indicate where progress bar was at. See the code below.

<progress class=”progress” value=”75″ max=”100″>75%</progress>

See the image below.

Progress Bar

Color Variation

Bootstrap 4 Progress bars can use a variety of color classes which are the identical with button and alert classes for consistent styles using the .progress-* class. See the examples below.

&amp;lt;progress class=&amp;quot;progress progress-success&amp;quot; value=&amp;quot;10&amp;quot; max=&amp;quot;100&amp;quot;&amp;gt;10%&amp;lt;/progress&amp;gt;
&amp;lt;progress class=&amp;quot;progress progress-info&amp;quot; value=&amp;quot;30&amp;quot; max=&amp;quot;100&amp;quot;&amp;gt;30%&amp;lt;/progress&amp;gt;
&amp;lt;progress class=&amp;quot;progress progress-warning&amp;quot; value=&amp;quot;65&amp;quot; max=&amp;quot;100&amp;quot;&amp;gt;65%&amp;lt;/progress&amp;gt;
&amp;lt;progress class=&amp;quot;progress progress-danger&amp;quot; value=&amp;quot;100&amp;quot; max=&amp;quot;100&amp;quot;&amp;gt;100%&amp;lt;/progress&amp;gt;

Color Variation

Striped and Animated Progress bar

To create a striped progress bar just add an extra .progress-striped class to the <progress> tag. You can also create an animation using the .progress-animated to the same tag. See code example below.

&amp;lt;progress class=&amp;quot;progress progress-striped progress-animated&amp;quot; value=&amp;quot;25&amp;quot; max=&amp;quot;100&amp;quot;&amp;gt;25%&amp;lt;/progress&amp;gt;
&amp;lt;progress class=&amp;quot;progress progress-striped progress-animated progress-danger&amp;quot; value=&amp;quot;75&amp;quot; max=&amp;quot;100&amp;quot;&amp;gt;75%&amp;lt;/progress&amp;gt;

The code above will output the image below.

Striped and Animated Progress bar

Bootstrap JavaScript Plugins

JavaScript adds interactivity to every website on the web. Tooltips, image slideshows, drop-down menus and popovers are just some examples of popular website features that can be created by combining JavaScript with CSS.

In this part, we’ll learn how to use JavaScript plugins via Bootstrap 4 to create advanced website features.

There are two ways to include Bootstrap’s JavaScript plugins. The first is by downloading the boostrap.js or bootstrap.min.js file here. The second is by using the CDN and link to your HTML file. Let’s see both in action. Please take note that you need to add first the jQuery library link along with bootstrap.js file.

Use Bootstrap Source File

&amp;lt;script src=&amp;quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src=&amp;quot;js/bootstrap.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;

Use Bootstrap CDN Link

&amp;lt;script src=&amp;quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;

Carousel

Bootstrap 4 carousel is a flexible and responsive way to add a slider to your website. Aside from being responsive, it allows you to add images, videos or just about any type of content.

To create a carousel, follow these steps:

  • Create a wrapper <div> and add the .carousel and .slide with its own ID. You also need to data-ride=”carousel” attribute to start the carouse’s animation immediately when the page loads.
  • Create an ordered list for our indicator controls (the little circles at the bottom center) and add the carousel-indicators class on the <ol> Then we also need to add the data-target=”#id” along with the data-slide-to=”#” to specify which slide to go to when clicking a specific dot. Don’t forget to add class .active on the first element; this will be the starting item of the carousel.
  • Now create another <div> container for the scrollable contents (this where you will put your contents), add the .carousel-inner inside it and give each item a .carousel-item You can also add a new <div> element inside right after adding the images and give it a class .carousel-caption.
  • For Previous and Next buttons, we will use an anchor class with the class .left and carousel-control for the “Previous” button, and class .right and .carousel-control for the “Next” button.

The following code creates the Bootstrap carousel:

&amp;lt;div id=&amp;quot;carousel -generic&amp;quot; class=&amp;quot;carousel slide&amp;quot; data-ride=&amp;quot;carousel&amp;quot;&amp;gt;
  &amp;lt;ol class=&amp;quot;carousel-indicators&amp;quot;&amp;gt;
    &amp;lt;li data-target=&amp;quot;#carousel-generic&amp;quot; data-slide-to=&amp;quot;0&amp;quot; class=&amp;quot;active&amp;quot;&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li data-target=&amp;quot;#carousel-generic&amp;quot; data-slide-to=&amp;quot;1&amp;quot;&amp;gt;&amp;lt;/li&amp;gt;
    &amp;lt;li data-target=&amp;quot;#carousel-generic&amp;quot; data-slide-to=&amp;quot;2&amp;quot;&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;/ol&amp;gt;
  &amp;lt;div class=&amp;quot;carousel-inner&amp;quot; role=&amp;quot;listbox&amp;quot;&amp;gt;
    &amp;lt;div class=&amp;quot;carousel-item active&amp;quot;&amp;gt;
      &amp;lt;img src=&amp;quot;https://goo.gl/RKA5bV&amp;quot; alt=&amp;quot;First slide&amp;quot;&amp;gt;
      &amp;lt;div class=&amp;quot;carousel-caption&amp;quot;&amp;gt;
      &amp;lt;h3&amp;gt;This is a caption!&amp;lt;/h3&amp;gt;
      &amp;lt;p&amp;gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.&amp;lt;/p&amp;gt;
     &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class=&amp;quot;carousel-item&amp;quot;&amp;gt;
      &amp;lt;img src=&amp;quot;https://goo.gl/cLZ8gN&amp;quot; alt=&amp;quot;Second slide&amp;quot;&amp;gt;
       &amp;lt;div class=&amp;quot;carousel-caption&amp;quot;&amp;gt;
      &amp;lt;h3&amp;gt;This is a caption!&amp;lt;/h3&amp;gt;
      &amp;lt;p&amp;gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.&amp;lt;/p&amp;gt;
     &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class=&amp;quot;carousel-item&amp;quot;&amp;gt;
      &amp;lt;img src=&amp;quot;https://goo.gl/ZbqeTw&amp;quot; alt=&amp;quot;Third slide&amp;quot;&amp;gt;
      &amp;lt;div class=&amp;quot;carousel-caption&amp;quot;&amp;gt;
      &amp;lt;h3&amp;gt;This is a caption!&amp;lt;/h3&amp;gt;
      &amp;lt;p&amp;gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.&amp;lt;/p&amp;gt;
     &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
  &amp;lt;a class=&amp;quot;left carousel-control&amp;quot; href=&amp;quot;#carousel- generic&amp;quot; role=&amp;quot;button&amp;quot; data-slide=&amp;quot;prev&amp;quot;&amp;gt;
    &amp;lt;span class=&amp;quot;icon-prev&amp;quot; aria-hidden=&amp;quot;true&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;
    &amp;lt;span class=&amp;quot;sr-only&amp;quot;&amp;gt;Previous&amp;lt;/span&amp;gt;
  &amp;lt;/a&amp;gt;
  &amp;lt;a class=&amp;quot;right carousel-control&amp;quot; href=&amp;quot;#carousel- generic&amp;quot; role=&amp;quot;button&amp;quot; data-slide=&amp;quot;next&amp;quot;&amp;gt;
    &amp;lt;span class=&amp;quot;icon-next&amp;quot; aria-hidden=&amp;quot;true&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;
    &amp;lt;span class=&amp;quot;sr-only&amp;quot;&amp;gt;Next&amp;lt;/span&amp;gt;
  &amp;lt;/a&amp;gt;
&amp;lt;/div&amp;gt;

Carousel

You can manually call the carousel with JavaScript, using the following code:

$('.carousel').carousel()

Methods

The following methods are useful carousel code.

Options

Initializes the carousel with an optional options object and starts cycling through items:

$('.carousel').carousel({
 interval: 2000
})

Cycle

Cycles through the carousel items from left to right:

.carousel('cycle')

Pause

Stops the carousel from cycling through items:

.carousel('pause')

Number

Cycles the carousel to a particular frame (0-based, similar to an array):

.carousel('number')

Prev

Cycles to the previous item:

.carousel('prev')

Next

Cycles to the next item:

.carousel('next')

Tooltips and Popovers

Tooltips are used to describe a link or provide certain information about a word or of an abbreviation. The plugin was originally based on the jQuery.tipsy a plugin was written by Jason Frame. However, in Bootstrap 4, it moves to Tether, a third-party plugin. If you want to use this you can simply include tether.min.js just before the bootstrap.js.

To add a tooltip, add data-toggle=”tooltip” and title=”Some tooltip text!” to an anchor tag or button. The title of the anchor will be the text of a tooltip.

You also need to add the following JavaScript code inside a <script> to trigger tether.js:

$(function () {
  $('[data-toggle=&amp;amp;amp;amp;amp;amp;quot;tooltip&amp;amp;amp;amp;amp;amp;quot;]').tooltip()
})  

The following examples show how to do this in the Bootstrap and tether.js.

&amp;lt;button type=&amp;quot;button&amp;quot; class=&amp;quot;btn btn-secondary&amp;quot; data-toggle=&amp;quot;tooltip&amp;quot; data-placement=&amp;quot;top&amp;quot; title=&amp;quot;Tooltip on top&amp;quot;&amp;gt;
  Tooltip on top
&amp;lt;/button&amp;gt;
&amp;lt;button type=&amp;quot;button&amp;quot; class=&amp;quot;btn btn-secondary&amp;quot; data-toggle=&amp;quot;tooltip&amp;quot; data-placement=&amp;quot;right&amp;quot; title=&amp;quot;Tooltip on right&amp;quot;&amp;gt;
  Tooltip on right
&amp;lt;/button&amp;gt;
&amp;lt;button type=&amp;quot;button&amp;quot; class=&amp;quot;btn btn-secondary&amp;quot; data-toggle=&amp;quot;tooltip&amp;quot; data-placement=&amp;quot;bottom&amp;quot; title=&amp;quot;Tooltip on bottom&amp;quot;&amp;gt;
  Tooltip on bottom
&amp;lt;/button&amp;gt;

&amp;lt;a href=&amp;quot;#&amp;quot; data-toggle=&amp;quot;tooltip&amp;quot; title=&amp;quot;Some tooltip text!&amp;quot;&amp;gt;Text Link Default&amp;lt;/a&amp;gt;
&amp;lt;a href=&amp;quot;#&amp;quot; data-toggle=&amp;quot;tooltip&amp;quot; title=&amp;quot;Some tooltip text!&amp;quot; data-placement=&amp;quot;top&amp;quot;&amp;gt;Text Link Top&amp;lt;/a&amp;gt;

Tooltips and Popovers

For pop-over, you can simply add the following data-attributes to your button:

  • data-container=”body”
  • data-toggle=”popover”
  • data-placement=”top” – either top, bottom, left or right
  • data-content=”Your content here”

See example code:

&amp;lt;button type=&amp;quot;button&amp;quot; class=&amp;quot;btn btn-secondary&amp;quot; data-container=&amp;quot;body&amp;quot; data-toggle=&amp;quot;popover&amp;quot; data-placement=&amp;quot;top&amp;quot; data-content=&amp;quot;Vivamus sagittis lacus vel augue laoreet rutrum faucibus.&amp;quot;&amp;gt;
  Popover on top
&amp;lt;/button&amp;gt;

Modals

Modals

A modal is a pop-out window that is layered over its parent. The purpose is to display content from a separate source that can have some interaction or action without leaving the parent window.

To create a static modal window, check this code:

&amp;lt;!-- Button trigger modal --&amp;gt;
&amp;lt;button type=&amp;quot;button&amp;quot; class=&amp;quot;btn btn-primary btn-lg&amp;quot; data-toggle=&amp;quot;modal&amp;quot; data-target=&amp;quot;#myModal&amp;quot;&amp;gt;
  Click here to see the demo!
&amp;lt;/button&amp;gt;

&amp;lt;!-- Modal --&amp;gt;
&amp;lt;div class=&amp;quot;modal fade&amp;quot; id=&amp;quot;myModal&amp;quot; tabindex=&amp;quot;-1&amp;quot; role=&amp;quot;dialog&amp;quot; aria-labelledby=&amp;quot;myModalLabel&amp;quot; aria-hidden=&amp;quot;true&amp;quot;&amp;gt;
  &amp;lt;div class=&amp;quot;modal-dialog&amp;quot; role=&amp;quot;document&amp;quot;&amp;gt;
    &amp;lt;div class=&amp;quot;modal-content&amp;quot;&amp;gt;
      &amp;lt;div class=&amp;quot;modal-header&amp;quot;&amp;gt;
        &amp;lt;button type=&amp;quot;button&amp;quot; class=&amp;quot;close&amp;quot; data-dismiss=&amp;quot;modal&amp;quot; aria-label=&amp;quot;Close&amp;quot;&amp;gt;
          &amp;lt;span aria-hidden=&amp;quot;true&amp;quot;&amp;gt;&amp;amp;times;&amp;lt;/span&amp;gt;
          &amp;lt;span class=&amp;quot;sr-only&amp;quot;&amp;gt;Close&amp;lt;/span&amp;gt;
        &amp;lt;/button&amp;gt;
        &amp;lt;h4 class=&amp;quot;modal-title&amp;quot; id=&amp;quot;myModalLabel&amp;quot;&amp;gt;Modal title&amp;lt;/h4&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div class=&amp;quot;modal-body&amp;quot;&amp;gt;
  &amp;lt;!-- Your Content here! --&amp;gt;     
&amp;lt;p&amp;gt;This is where your content goes! &amp;lt;/p&amp;gt;
 &amp;lt;/div&amp;gt;
      &amp;lt;div class=&amp;quot;modal-footer&amp;quot;&amp;gt;
        &amp;lt;button type=&amp;quot;button&amp;quot; class=&amp;quot;btn btn-secondary&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;Close&amp;lt;/button&amp;gt;
        &amp;lt;button type=&amp;quot;button&amp;quot; class=&amp;quot;btn btn-primary&amp;quot;&amp;gt;Save changes&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

There are two data attributes that can trigger the modal via link or buttons.

  • data-toggle=”modal” – opens the modal window directly
  • data-target=”#myModal” – access to the id of the modal

Inside the modal part, we have a <div> which has an ID that is the same as the value of the data-target attribute used to trigger the modal, in this case its .myModal class.

Here are some more classes/attributes that were used:

  • .modal class – specify the content of the <div>
  • role=”dialog” attribute is for accessibility for people using screen readers
  • .modal-dialog class – creates the width and margin of the modal
  • .modal-header class – for modal header styles
  • .close class – styles the close button
  • .modal-body class – handles the style for the body of the modal
  • .modal-footer class – handles the style for the footer of the modal; this area is right aligned by default

Having all the code here, you’ll get a result like the image below.

Modals

Please take note that you can also control the size of the modal by adding either .modal-sm class for small modals or  .modal-lg class for large modals inside the <div> that has the .modal-dialog class.

Using JavaScript

You can also use Bootstrap modal window via JavaScript by calling the modal() Bootstrap method with the modal “id” or “class” selector. See the code below.

&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;
$(document).ready(function(){
    $(&amp;quot;.btn&amp;quot;).click(function(){
        $(&amp;quot;#myModal&amp;quot;).modal('show');
    });
});
&amp;lt;/script&amp;gt;

You can always refer to the documentation here if you want more options for modals.

Conclusion

Like other CSS frameworks, the power of Bootstrap comes not just from the creators but also from other external plugins that support it.

In this article, you’ve learned about the extra features that Bootstrap 4 offers including JavaScript. You can also customize the default CSS styles of every component here. However, it is recommended that you add an additional custom CSS or JavaScript file if you want to make changes instead of editing core files.

Please take note that at the time of this writing, Bootstrap 4 CSS is in alpha release; therefore, it is highly recommended to not use it in production yet. I hope you learned from this tutorial and let me know your thoughts in the comment section.

Sam Norton

Sam is an expert Full Stack Developer who loves making digital solutions that are meaningful and new. Sam is an expert in web design and development. He uses his knowledge of HTML/CSS, JavaScript, jQuery, Ruby, Ruby on Rails, WordPress, Node.js, React, Express.js, Gatsby.js, GraphQL, and Strapi.js to make custom websites that reflect clients' unique brands and serve their business niches. Committed to staying ahead of the curve, Sam harnesses the power of the latest technologies, CMS, and platforms to build cutting-edge websites that outperform competitors.

Posts by Sam Norton
🍪

We use cookies to ensure that we give you the best experience on our website. Privacy Statement.

  • I disagree
  • I agree