How to center an element in any screen - Jquery

Center Element

Snippet Code


  
Rate this page :
  [ 0 votes]

This code can be used to center an element in any type of screen

jQuery.fn.center = function () { this.css('position','absolute'); this.css('top', ( $(window).height() - this.height() ) / $(window).scrollTop() 'px'); this.css('left', ( $(window).width() - this.width() ) / 2 $(window).scrollLeft() 'px');return this;} //Use the above function as: $(element).center();

Tags


Ask Questions

Ask Question