Wednesday, July 27, 2011

How to center a div by jQuery



jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}


$(element).center();

http://stackoverflow.com/questions/210717/using-jquery-to-center-a-div-on-the-screen

No comments:

Post a Comment