Centering a div vertically & horizontally using jQuery

后端 未结 9 1333
轮回少年
轮回少年 2020-12-02 08:44

I am using this script to center my div horizontally and vertically.

When the page loads the div gets centered vertically, not horizontally until I resize the brows

9条回答
  •  甜味超标
    2020-12-02 08:55

    use this to center:

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

提交回复
热议问题