Skip to content

Instantly share code, notes, and snippets.

@tnip
Created July 9, 2012 03:02
Show Gist options
  • Save tnip/3074000 to your computer and use it in GitHub Desktop.
Save tnip/3074000 to your computer and use it in GitHub Desktop.
Vertically Centering Things in JS
function verticallyAlignTheDamnThing(){
var content_height = $(‘#centered-element’).height();
var window_height = $(document).height();
// Reposition the div vertically!
var reposition_top_y = (window_height - content_height) / 2;
if(reposition_top_y > 0){
$(‘#centered-element’).css(‘margin-top’, reposition_top_y + ‘px’);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment