August Karlstrom
August Karlstrom

Reputation: 11377

Specifying only width or height for an img element

If only one of the attributes height or width is set for an img element, most browsers seem to keep the proportions of the image.

This is from the HTML 4.01 reference:

When the object is an image, it is scaled. User agents should do their best to scale an object or image to match the width and height specified by the author.

http://www.w3.org/TR/html401/struct/objects.html#edef-IMG

Would it be wrong of a browser to scale the image non-uniformly, that is changing only the height or the width of an image?

Update 2015-08-12: Dillo (version 3.0.4) is an example of a browser which does not keep the proportions of an image if only height or only width is set.

Upvotes: 7

Views: 2877

Answers (2)

Mike
Mike

Reputation: 345

If you look at what Bootstrap does with the .img-responsive class, it only sets height and max-width. http://getbootstrap.com/css/#images-responsive

If a browser didn't auto scale the width based on the height, this widely used CSS package would fail.

Upvotes: 2

singpolyma
singpolyma

Reputation: 11241

I can't speak for the spec authors, but from my PoV as a web developer if you scale non-uniformly your user agent is broken and none of my sites will render properly for you. Why would I ever want a non-uniform scaling? Especially one where one side is just determined by whatever size the image happens to be?

Upvotes: 1

Related Questions