14

I am using bootbox.js from http://bootboxjs.com/examples.html and it is quite impressive. But I have an issue while setting up the location of the dialog box.

I want to place dialog box center of the screen. I am using this code but dialog box stays on top of the screen.

 bootbox.alert('Danger!!').find('.modal-content').css({
     'background-color': '#f99',
     'font-weight': 'bold',
     color: '#F00',
     'top': '50%',
     'margin-top': function() {
         return -(box.height() / 2);
     },
     'font-size': '2em',
     'font-weight': 'bold'
 });

Please advise how can set the dialog box center of the screen.

Sender
  • 6,660
  • 12
  • 47
  • 66
Roxx
  • 3,738
  • 20
  • 92
  • 155

4 Answers4

20

As of Bootbox 5, you can actually enable this via an option, centerVertical:

bootbox.alert({
    message: "I'm a vertically-centered dialog!",
    centerVertical: true
});
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
16

If you want a vertical centered bootbox modal via css3

.modal-open .modal {
    display: flex !important;
    align-items: center;
    justify-content: center;
}

You can have more flexibility here: https://www.kirupa.com/html5/centering_vertically_horizontally.htm https://css-tricks.com/snippets/css/a-guide-to-flexbox/

13

Modal is already centered in horizontal direction. If you want vertical centered modal, hope this will work for you

bootbox.alert('Danger!!' ).find('.modal-content').css({
    'background-color': '#f99',
    'font-weight' : 'bold',
    'color': '#F00',
    'font-size': '2em',
    'margin-top': function (){
        var w = $( window ).height();
        var b = $(".modal-dialog").height();
        // should not be (w-h)/2
        var h = (w-b)/2;
        return h+"px";
    }
});

Giving this answer according to your example.

Haris ur Rehman
  • 2,593
  • 30
  • 41
M B Parvez
  • 808
  • 6
  • 16
3

This question is quite old, but another short option would be to use the corresponding bootstrap class:

bootbox.alert('Danger!!') .find(".modal-dialog") .addClass("modal-dialog-centered");

This works with Bootstrap v4.0.0 or higher.

Richy5
  • 3
  • 1
anste
  • 118
  • 1
  • 3
  • 11