Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Bootstrap JS Modal


Modal CSS Classes

For a tutorial about Modals, read our Bootstrap Modal Tutorial.

Class Description Example
.modal Creates a modal Try it
.modal-content Styles the modal properly with border, background-color, etc. Use this class to add the modal's header, body, and footer Try it
.modal-dialog-centered Centers the modal vertically and horizontally within the page Try it
.modal-dialog-scrollable Adds a scrollbar inside the modal Try it
.modal-header Defines the style for the header of the modal Try it
.modal-body Defines the style for the body of the modal Try it
.modal-footer Defines the style for the footer in the modal. Note: This area is right-aligned by default. To change this, add the justify-content-start or justify-content-center together with the .modal-footer class Try it
.modal-sm Specifies a small modal Try it
.modal-lg Specifies a large modal Try it
.fade Adds an animation/transition effect which fades the modal in and out Try it

Trigger the Modal Via data-* Attributes

Add data-toggle="modal" and data-target="#modalID" to any element.

Note: For <a> elements, omit data-target, and use href="#modalID" instead:

Example

<!-- Buttons -->
<button type="button" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Links -->
<a data-toggle="modal" href="#myModal">Open Modal</a>

<!-- Other elements -->
<p data-toggle="modal" data-target="#myModal">Open Modal</p>
Try it Yourself »

Trigger Via JavaScript

Enable manually with:

Example

$("#myModal").modal()
Try it Yourself »

Modal Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-backdrop="".

Name Type Default Description Try it
backdrop boolean or the string "static" true Specifies whether the modal should have a dark overlay:

  • true - dark overlay
  • false - no overlay (transparent)

If you specify the value "static", it is not possible to close the modal when clicking outside of it

Using JS Using data
keyboard boolean true Specifies whether the modal can be closed with the escape key (Esc):

  • true - the modal can be closed with Esc
  • false - the modal cannot be closed with Esc
Using JS Using data
show boolean true Specifies whether to show the modal when initialized Using JS Using data

Modal Methods

The following table lists all available modal methods.

Method Description Try it
.modal(options) Activates the content as a modal. See options above for valid values Try it
.modal("toggle") Toggles the modal Try it
.modal("show") Opens the modal Try it
.modal("hide") Hides the modal Try it

Modal Events

The following table lists all available modal events.

Event Description Try it
show.bs.modal Occurs when the modal is about to be shown Try it
shown.bs.modal Occurs when the modal is fully shown (after CSS transitions have completed) Try it
hide.bs.modal Occurs when the modal is about to be hidden Try it
hidden.bs.modal Occurs when the modal is fully hidden (after CSS transitions have completed) Try it

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.