Skip to content

Instantly share code, notes, and snippets.

@jshaw
Created August 9, 2012 20:01
Show Gist options
  • Save jshaw/3307577 to your computer and use it in GitHub Desktop.
Save jshaw/3307577 to your computer and use it in GitHub Desktop.
Vertically aligning images in a div (Fix for IE8 & 9)
// WORKS FF, Chrome, Safari, IE 8 & 9 etc...
$('.extra-content .graphics img').load(function() {
$(this).css('marginTop', $(this).parents('.graphics').height() / 2 - $(this).height() / 2 );
});
// DOESN'T WORK IE8 & 9
// With multiple images on a page this script seems unable to compute the marginTop var.
// However placing it directly into the CSS method works as shown above.
$('.extra-content .graphics img').load(function() {
var marginTop = $(this).parents('.graphics').height() / 2 - $(this).height() / 2;
$(this).css('marginTop', marginTop);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment