Last active
May 2, 2018 10:23
-
-
Save devudit/9127830fe2496c44c116fe600f9097bf to your computer and use it in GitHub Desktop.
Center bootstrap modal, Vertically and Horizontally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
"use strict"; | |
function $fixBootstrapModalPosition() { | |
$(this).css('display', 'block'); | |
var $dialog = $(this).find(".modal-dialog"), | |
offset = ($(window).height() - $dialog.height()) / 2, | |
bottomMargin = parseInt($dialog.css('marginBottom'), 10); | |
// Make sure you don't hide the top part of the modal w/ a negative margin if it's longer than the screen height, and keep the margin equal to the bottom margin of the modal | |
if(offset < bottomMargin) offset = bottomMargin; | |
$dialog.css("margin-top", offset); | |
} | |
$(document).on('show.bs.modal', '.modal', $fixBootstrapModalPosition); | |
$(window).on("resize", function () { | |
$('.modal:visible').each($fixBootstrapModalPosition); | |
}); | |
})(jQuery); | |
// Alternate:- You can add this css if you don't want to use js | |
/* | |
.modal { | |
position: fixed; | |
top: 50% !important; | |
left: 50%; | |
transform: translate(-50%, -50%); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment