如何将bootstrap模式在页面上水平居中?

问题描述 投票:0回答:1

我想让我的bootstrap模式出现后在页面上水平居中,但我似乎只能让代码垂直居中。这是我目前的代码。

$(document).ready(function () {
            function alignModal() {
                var modalDialog = $(this).find(".modal-dialog");

                // Applying the top margin on modal dialog to align it vertically center
                modalDialog.css("margin-top", Math.max(0, ($(window).height() - modalDialog.height()) / 2));
            }
            // Align modal when it is displayed
            $(".modal").on("shown.bs.modal", alignModal);

            // Align modal when user resize the window
            $(window).on("resize", function () {
                $(".modal:visible").each(alignModal);
            });
        });
javascript html jquery css bootstrap-modal
1个回答
2
投票

你的意思是居中 纵向? 因为你写了两次 "横向"。如果是竖着写。从 Bootstrap的文档页 :

增加 .modal-dialog-centered 到.modal-dialog,使modal垂直居中。

© www.soinside.com 2019 - 2024. All rights reserved.