kamal
kamal

Reputation: 237

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

Upvotes: 0

Views: 999

Answers (2)

adeneo
adeneo

Reputation: 318342

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');
});

Upvotes: 1

Nate B
Nate B

Reputation: 6344

Why use JQuery at all? CSS can easily center items, even with variable (and changing) widths.

Example here.

Upvotes: 3

Related Questions