The Modal Component in Bootstrap 4

The Bootstrap 4 Modals are built by using HTML, CSS, and JavaScript (jQuery). As a modal is displayed, the underneath content in the web page becomes “inactive” that a user cannot interact with until it is closed. The scroll bars are removed for underlying content so that modal content can be scrolled.

You may use Modal Bootstrap 4 for adding user notifications, lightboxes, marketing some important service or product, making announcements etc.

A few quick modal demos in this tutorial

vertically centered tooltip in modal small custom modal onload

In this tutorial, I will show you how to create modals by using Bootstrap 4 framework with live demos and complete code.

A simple modal example

Let me begin by creating a simple modal by using the built-in classes and data attributes. A modal generally has three sections:

  • The Modal header – Display a title/heading with controls like close option.
  • Modal body – Convey the message to the user, place a form, images, videos etc.
  • Modal footer – The optional component where you may place actionable stuff like “Subscription”, “Create Account” buttons etc.

See the example below with all three components in the Bootstrap 4 Modal:

Bootstrap 4 modal simple

See online demo and code

The markup for the example:

The requirements:

You are required to include the Bootstrap CSS file, jQuery, and popper JS files and finally Bootstrap JS file in order to make the modal component work. I have included all these by CDN:

<link rel=”stylesheet” href=”” integrity=”sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy” crossorigin=”anonymous”>


<script src=””></script>

<script src=””></script>

<script src=””></script>

How modal code works?

Step 1:

First of all, you can see a modal launch button that used the data-target attribute. Its value is matched to the ID of the main div containing modal i.e.

<div class=”modal fade” id=”Modal-simple-demo” tabindex=”-1″ role=”dialog” aria-labelledby=”Modal-simple-demo-label” aria-hidden=”true”>

Besides, the data-toggle=”modal” attribute is also used in the launch button.

Step 2:

The modal header is defined by using the .modal-header class in a div. In our example header, the modal contains the title and close button.

Step 3:

The body of the modal where you will write the content or display images/videos is defined by using a <div> that is given the .modal-body class.

Step 4:

The footer is set by using another div with .modal-footer class. There, I used a few Bootstrap 4 buttons classes.

Display modal vertically centered example

As you launched the above modal by pressing the button, you might notice the modal displayed upwards as per your screen. It was not vertically centered by default.

If you require displaying the modal aligned vertically centered then just add the .modal-dialog-centered class with .modal-dialog. Have a look at this demo:

See online demo and code

The markup:

Displaying tooltips in Modal dialog example

As mentioned earlier, you may display content as per the requirement in the modal body. The following example displays the tooltips by using Bootstrap 4 tooltip component classes in the modal body:

Bootstrap 4 modal tooltip

See online demo and code

The markup for the demo:

You also need to initiate the tooltip component via jQuery:

You can see, the tooltips are displayed towards left, right, top, and bottom directions.

A demo of displaying popovers

Similarly, you may use the popover component in the Modal dialog. Have a look at the demo:

Bootstrap 4 modal popover

See online demo and code

The markup:

The script for initializing the popover:

Create large and small-sized modal windows

Bootstrap 4 has classes to create small and large modal dialogs. In Bootstrap 3 modals, you had to customize the sizes by grabbing the modal classes and changing the height or width of modal as per the requirement. You can also do this in Bootstrap 4, however, it also has built-in classes for creating the standard small and large-sized modal windows.

The size of the modals will adjust as per the user’s device. Following classes can be used with .modal-dialog class:

  • modal-lg //For creating large sized modal
  • modal-sm //For small size modals

Have a look at this demo where you can see two buttons to launch large and small size modal dialogs:

modal sizes

See online demo and code

The markup for small and large size modals:

How to customize the height and width of Modal in Bootstrap 4?

Rather than using the default settings for height and width for Bootstrap modals (for normal, small or large sizes), you may specify your own as per requirement.

For that, you need to use the custom CSS after the reference of Bootstrap CSS file. The following example shows using the CSS in <style> section.

There, I used the .modal-dialog class and set the width as follows:

See online demo and code


Overriding the default colors demo

Similarly, by using the .modal-content property after the reference of Bootstrap 4 CSS, you may customize the color scheme of the content area and .modal-header for the header of the modal.

See a demonstration below where I changed the header and content default colors by overriding these classes:

modal customize

See online demo and code

The CSS for this demo:

The markup:

In the same way, you may get the other classes and override the default styles.

Related Tutorial: Customize Bootstrap 3 modals

Removing the animation as modal closes

In all above examples, you might have seen the usage of .fade class in the <div> with .modal class (the first div). That gives modals a fading effect as you open or close the modal window by pressing the cross sign or click somewhere in “backdrop”.

If you want to remove this effect, simply delete the fade class. See this example where modal dialog displays without any effect:

See online demo and code

Only this bit is changed to the main div:

<div class=”modal” id=”Modal-nofade-demo” tabindex=”-1″ role=”dialog” aria-labelledby=”Modal-nofade-demo-label” aria-hidden=”true”>

That is, the fade class is removed.

Display the modal as web page loads

In all above examples, the modal is launched as you pressed the button. In certain situation, you may require loading the modal dialog as the web page loads.

For that, you may use the modal’s show method in jQuery code as shown in the example below. Click the image or link below to load the example page and you will see modal is loaded:

See online demo and code

Just add the following script before the </body> tag:

Make sure using the correct modal ID in the jQuery code.

Disable the keyboard Escape key example

Again, in all above examples, the modal window is closed as you press the “Esc” key in the keyboard along with options of closing by clicking the cross button or clicking on “backdrop”.

You may disable the keyboard Esc key by using the keyboard: false in the jQuery code or by using data-attribute. Open the demo page and try closing by pressing the Esc key:

See online demo and code

The script:

See the complete code along with markup on the demo page.

Note: You may also use the data attribute for disabling the keyboard if you do not want to use the jQuery code. For example:


This markup will also work for above example without jQuery code:

<div class=”modal fade” id=”Modal-keyboard-demo” tabindex=”-1″ data-keyboard=”false” role=”dialog” aria-labelledby=”Modal-keyboard-demo-label” aria-hidden=”true”>

Disabling the backdrop in modal Bootstrap

Similarly, you may set the backdrop false via JavaScript or data attribute. In that case, the modal will not be closed by clicking anywhere outside of the modal. You may also disable keyboard and backdrop together, so a user may close the modal only by pressing the cross button or buttons in the modal.

Have a look at the demo below where I disabled keyboard Esc as well as the backdrop by using the data attributes.

modal keyboard backdrop

See online demo and code

The markup:

These two attributes are used:

data-keyboard=”false” data-backdrop=”false”

No JavaScript.