Get an Images Native Width

Avatar of Chris Coyier
Chris Coyier on

If you select and image with jQuery and then use .width(), you’ll get the images current width, even if it’s been scaled (e.g. max-width: 100%;). You can access the images native width (even if it doesn’t have any attributes which declare it) like this:

// Get on screen image
var screenImage = $("#image");

// Create new offscreen image to test
var theImage = new Image();
theImage.src = screenImage.attr("src");

// Get accurate measurements from that.
var imageWidth = theImage.width;
var imageHeight = theImage.height;