Code Snippet > JS - jQuery
Center an element on the screen using jQuery
This code snippet add a new function called center to the jQuery. Really handy to call the function and apply it to the element you want to center it on screen.
JS - jQuery
This add a function called center to the jQuery.
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
JS - jQuery
This is how you use it
$('#window').center();
Leave a comment
Have something to say? Drop a comment! No HTML tags are allowed in the comment textfield.





















2 comments