question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

img {height:auto} is overriding height attribute in <img />

See original GitHub issue

Prologue

A well-known trick for quicker page rendering and better user experience is to explicitly set the height attribute of an img (i.e. <img height="30" src="..." />). This will avoid nasty “jumps” while the page is rendering if the height of an image (being downloaded) is known.

Problem is: afaik, the following rule makes it impossible to take advantage of that technique.

img {
  max-width: 100%;
  height: auto;
  border: 0;
  -ms-interpolation-mode: bicubic;
}

Weird as it sounds, It will override the inline attribute value. I couldn’t find any value for height (the CSS property, not the img property) to “undo” this.

I’m not sure the rationale for this particular property in the above rule, so I wonder if someone could shed some light, and a workaround/fix for this issue.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
mdocommented, Feb 12, 2012

You can use the same trick with inline styles as the inline height attribute: style="height: 100px;".

1reaction
silvenoncommented, Jul 25, 2013

I agree, if we wanted height: auto we wouldn’t set the height attribute. I had a long discussion about it and this seems like the best solution:

img {
  max-width: 100%;
}

/* https://github.com/csswizardry/inuit.css/pull/111#issuecomment-12562366 */
img[width],
img[height] {
  max-width: none;
}

This solution doesn’t resort to :not(), so the support is fairly good.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image height and width is overridden by css - Stack Overflow
Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image...
Read more >
Setting Height And Width On Images Is Important Again
This is actually very easily fixed by adding a height: auto line to the CSS so the height attribute from the HTML is...
Read more >
Use CSS to override an image's width and height attributes
Use CSS to override an image's width and height attributes ... media="all"> img.ignore-attributes { width: auto; height: auto; } ...
Read more >
Img Height In HTML: How Not To Use The Height Attribute »
The Proper Use of height​​ The purpose of the height (and the width attribute as well) is to inform the browser of the...
Read more >
CSS height property - W3Schools
If height: auto; the element will automatically adjust its height to allow ... Note: The min-height and max-height properties override the height property....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found