Share Coding

Tutorials, Problems, Stuffs …

How to make an Element to be Absolute Center on jQuery

In the previous tutorial that I have posted about Absolute Center on CSS:
https://sharecoding.wordpress.com/2012/02/15/absolute-center-in-css-fitting-all-browsers/

However, it can be made by jQuery too. By using jQuery, it is easier to be used on any elements.

Click here for Demo

// Center Method
$.fn.center = function() {
	this.css('position', 'absolute');
	this.css('left', '50%');
	this.css('top', '50%');
	this.css('margin-left', -this.width() / 2 + 'px');
	this.css('margin-top', -this.height() / 2 + 'px');
	return this;
}

$(document).ready(function() {

    // Use it easily
    $('#centerDiv').center();
    
});
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: