Skip to content

Instantly share code, notes, and snippets.

@janily
Created December 9, 2012 16:23
Show Gist options
  • Save janily/4245878 to your computer and use it in GitHub Desktop.
Save janily/4245878 to your computer and use it in GitHub Desktop.
position center
(function($){
$.fn.extend ({
center:function() {
return this.each(function(){
var top = ($(window).height() - $(this).outerHeight())/2;
var left = ($(window).width() - $(this).outerWidth())/2;
$(this).css({position:'absolute',margin:0,top:(top>0 ? top:0)+'px',left:(left>0 ? left:0)+'px'});
});
}
});
})(jQuery);
$('.center').center();
$(window).resize(function(){
$('.center').center();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment