Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery plugin for centering vertically and/or horizontally
(function( $ ) {
$.fn.vcenter = function () {
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
return this;
}
$.fn.hcenter = function () {
this.css("position","absolute");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
return this;
}
}( jQuery ));
@sardbaba

This comment has been minimized.

Copy link
Owner Author

@sardbaba sardbaba commented Dec 11, 2015

Usage:

$('#myelement').vcenter();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.