Search code examples
jquerycsshtmlcenter

Center div with jquery with unknown width


I have a div in fixed position that i like to make it center of my browser..

Of course it can be done with css, but the thing is i`m changing element's width in several times (loading something by ajax, so the width will change!) ...

So i want centering the div after changing its width by jquery.

For centering i use this code: http://manos.malihu.gr/center-div-with-jquery

But when i changing div's width, it doesn't become in center even after calling CenterItem again!

Bottom line, i want something like this: http://www.dynamicdrive.com/dynamicindex4/lightbox2/index.htm


Solution

  • If using the script in your link, call the Centeritem function on the resize event on the element as well as on the window.

    $('#ElementsID').resize(function() {
        CenterItem('.centered');
    });