Search code examples
jqueryjquery-uidialogjquery-dialog

How can I position my jQuery dialog to center?


I have tried following code, but it only positions dialogs left upper corner position to center, and that makes element to be aligned to right. How can I center dialog to real center which counts elements width, so that center line will cut dialog to 50% 50% halfs?

$('.selector').dialog({ position: 'center' });

http://docs.jquery.com/UI/Dialog#option-position


Solution

  • I'm pretty sure you shouldn't need to set a position:

    $("#dialog").dialog();
    

    should center by default.

    I did have a look at the article, and also checked what it says on the official jquery-ui site about positioning a dialog : and in it were discussed 2 states of: initialise and after initialise.

    Code examples - (taken from jQuery UI 2009-12-03)

    Initialize a dialog with the position option specified.

    $('.selector').dialog({ position: 'top' });
    

    Get or set the position option, after init.

    //getter
    var position = $('.selector').dialog('option', 'position');
    //setter
    $('.selector').dialog('option', 'position', 'top');
    

    I think that if you were to remove the position attribute you would find it centers by itself else try the second setter option where you define 3 elements of "option" "position" and "center".