Skip to content

Instantly share code, notes, and snippets.

@jjmu15
Created January 27, 2014 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jjmu15/8646161 to your computer and use it in GitHub Desktop.
Save jjmu15/8646161 to your computer and use it in GitHub Desktop.
Get image max width - This will give us image’s max-width: 100% value in pixels and it’s supported in IE9, Chrome, Firefox, Safari and Opera. We can also take this further and add support for older browsers by loading the image into an in-memory object:
// Get image's max-width:100%; in pixels
function getMaxWidth(img) {
var maxWidth;
// Check if naturalWidth is supported
if (img.naturalWidth !== undefined) {
maxWidth = img.naturalWidth;
// Not supported, use in-memory solution as fallback
} else {
var image = new Image();
image.src = img.src;
maxWidth = image.width;
}
// Return the max-width
return maxWidth;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment