If the modal html is rendered into a hidden area of the page, it won't display either

Answer from pscheit on Stack Overflow
🌐
Bootstrap
getbootstrap.com › docs › 4.4 › components › modal
Modal · Bootstrap
It also adds .modal-open to the <body> to override default scrolling behavior and generates a .modal-backdrop to provide a click area for dismissing shown modals when clicking outside the modal.
🌐
Rick Strahl's Web Log
weblog.west-wind.com › posts › 2016 › sep › 14 › bootstrap-modal-dialog-showing-under-modal-background
Bootstrap Modal Dialog showing under Modal Background - Rick Strahl's Web Log
September 14, 2016 - If anything in that tree uses position: absolute or position:fixed - boom the problem shows up in modals. The problem is that the .modal-backdrop overlay sits in the root of the DOM tree while the other content is buried in a separate DOM node tree.
🌐
Drupal
drupal.org › project › bootstrap › issues › 3018531
Modal-backdrop doesn't disappear [#3018531] | Drupal.org
September 13, 2024 - We are experiencing this issue, and I do think it is an issue with the Bootstrap theme. Feel free to close the issue again if you feel this isn't related. In our case we are having issues with a modal generated by the autologout module. When the modal is closed autologout.js calls jquery modal's destroy function $(this).dialog("destroy") (Dialog destroy documentation) This closes the modal, but leaves the backdrop visible.
🌐
MDBootstrap
mdbootstrap.com › standard › modal backdrop
Bootstrap Modal Backdrop - free examples & tutorial
If you would like to get rid of the backdrop, you can set the data-mdb-backdrop attribute to false. ... <!-- Button trigger modal --> <button type="button" data-mdb-button-init data-mdb-ripple-init class="btn btn-primary" data-mdb-modal-init ...
🌐
GitHub
github.com › twbs › bootstrap › issues › 501
Setting backdrop prevents modal from showing. · Issue #501 · twbs/bootstrap
October 28, 2011 - When setting backdrop to static or true, backdrop shows, but modal never appears. The only thing I could find is that the 'in' class does not get applied to this element
Author   adetwiler
Find elsewhere
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › components › modal
Modal · Bootstrap v5.0
The modal plugin toggles your hidden content on demand, via data attributes or JavaScript. It also overrides default scrolling behavior and generates a .modal-backdrop to provide a click area for dismissing shown modals when clicking outside the modal.
🌐
cyberangles
cyberangles.org › blog › bootstrap-modal-only-showing-backdrop
Bootstrap Modal Only Showing Backdrop? Fix for Hidden Dialog Caused by Missing Closing Tag — CyberAngles.org
By inspecting your modal’s DOM ... working smoothly. Remember: The backdrop appearing without the dialog is almost always a sign of malformed HTML....
🌐
MDBootstrap
mdbootstrap.com › standard › material design for bootstrap 5 & vanilla javascript
Modal is not showing (only backdrop is showing) - Material Design for Bootstrap
Expected behavior modal.show() opens modal Actual behavior modal.show() set only backdrop and no modal added Resources (screenshots, code snippets etc.) I am show
🌐
ASPSnippets
aspsnippets.com › questions › 131531 › Solved-Bootstrap-Modal-backdrop-does-not-disappear-after-modal-close-in-ASPNet-MVC
Solved Bootstrap Modal backdrop does not disappear after modal close in ASPNet MVC
October 19, 2020 - @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/css/bootstrap.min.css" /> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/js/bootstrap.min.js"></script> <script> $(function () { $('#myModal2').on('show.bs.modal', function (e) { $('#myModal2').focus(); }) }); </script
Top answer
1 of 16
596

Make sure you're not replacing the container containing the actual modal window when you're doing the AJAX request, because Bootstrap will not be able to find a reference to it when you try to close it. In your Ajax complete handler remove the modal and then replace the data.

If that doesn't work you can always force it to go away by doing the following:

$('#your-modal-id').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
2 of 16
67

Make sure that your initial call to $.modal() to apply the modal behavior is not passing in more elements than you intended. If this happens, it will end up creating a modal instance, complete with backdrop element, for EACH element in the collection. Consequently, it might look like the backdrop has been left behind, when actually you're looking at one of the duplicates.

In my case, I was attempting to create the modal content on-the-fly with some code like this:

myModal = $('<div class="modal">...lots of HTML content here...</div><!-- end modal -->');
$('body').append(myModal);
myModal.modal();

Here, the HTML comment after the closing </div> tag meant that myModal was actually a jQuery collection of two elements - the div, and the comment. Both of them were dutifully turned into modals, each with its own backdrop element. Of course, the modal made out of the comment was invisible apart from the backdrop, so when I closed the real modal (the div) it looked like the background was left behind.

🌐
GitHub
github.com › cypress-io › cypress › issues › 25202
Bootstrap modal and backdrop issue · Issue #25202 · cypress-io/cypress
December 17, 2022 - Current behavior Having this bootstrap modal displayed, when clicking "Close", modal is hidden, but backdrop is always here (Working on Chrome or Safari). Currently I do this to avoid the issue: cy.get('.modal-backdrop.fade.show').then((...
Author   lugosium
🌐
W3Schools
w3schools.com › bootstrap › tryit.asp
Modal Options
The W3Schools online code editor allows you to edit code and view the result in your browser