centerElement

Description:
A jQuery plugin to position an element on the center of the screen.

Demo page.

The code:

(function($)
{
	$.fn.centerElement = function() {
        return this.each(function()
        {
			var el = this;
			$(el).remove().appendTo("body")
					.wrap("<div style='position:absolute;top:50%;left:50%;width:1px;height:1px;'></div>")
					.css({position: 'relative', left: el.offsetWidth / -2 + "px", top: el.offsetHeight / -2 + "px"});
		});
	};
})(jQuery);

License:
The plugin is available under both MIT and GPL licenses (as jQuery itself). This means that you can choose the license that best suits your project, and use it accordingly.

The plugin was successfully tested on Firefox 2, Opera 9.10 and IE 6.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Syntax Highlighting: You can use [js] .. [/js] and [html] .. [/html] for posting code snippets.