The CSS Working Group is working on a solution to this problem.
The HTML for web developers will look like this:
<img src="dog.jpg" width="400" height="300">
The UA-stylesheets of all browsers will add this code:
img, video {
aspect-ratio: attr(width) / attr(height);
}
This will calculate an aspect ratio, based on the width & height attributes — before the image has loaded — and have that information available at the very beginning of layout calculation. The moment an image is told to be width: 100%, for example, the aspect ratio is used to calculate the height.
There is no need for a web developer to do anything else, or anything new (besides return to the habit of using width and height attributes in HTML for a performance gain). It'll just happen.
We are implementing this in Firefox as an experiment, in part to make sure such a solution will not break existing websites.