Center an element on screen

Submitted by:Jhon Smith

Date added:22 August, 2011

Category:jQuery

Here shown how to center an element on screen

Tags: center element

Code Snippet:

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();
 
 

Comments