Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
maniqui opened this issue Feb 10, 2012 · 9 comments
Closed

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

maniqui opened this issue Feb 10, 2012 · 9 comments

Comments

@maniqui
Copy link

maniqui commented Feb 10, 2012

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.

@mdo
Copy link
Member

mdo commented Feb 12, 2012

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

@mdo mdo closed this as completed Feb 12, 2012
@johnnypeck
Copy link

I'm curious about the rationale for this declaration as well. Why use a style declaration for every image rather than setting height and width attributes in the markup where appropriate.

@tobydeh
Copy link

tobydeh commented Mar 9, 2012

Is there a reason for having img {height:auto} ?

@necolas
Copy link
Contributor

necolas commented Mar 9, 2012

Is there a reason for having img {height:auto} ?

When combined with max-width:100% it's needed to override any height attributes. Otherwise the height of the image won't scale down in proportion to the width.

@ShaggyDude
Copy link

this is true in some browsers. yes you need it.

@Charlie-Hua
Copy link

When combined with max-width:100% it's needed to override any height attributes. Otherwise the height of the image won't scale down in proportion to the width.

however, max-width:100% can be overwritten by max-width:none...
height:auto can't be overwritten and using style="height: 100px;" makes it not unobtrusive and hard to maintain...

@dmitry
Copy link

dmitry commented Mar 28, 2012

Already fixed in #2779, >= 2.0.3

@archonic
Copy link

Some of us deal with an environment that has thousands of images. Applying that style to all images with inline styling is unreasonable. Sometimes the images are produced from content submitted through tinyMCE as well.

@silvenon
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants