Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max-height attribute makes image unproportional wide in chrome?

I have set an image with the css set to a max-height of 220px and a height of 100%.

That should set (this) image width to 175px and height to 220px. Which works fluently in Firefox and Internet explorer but in Chrome (desktop, tablet & smartphone) it sets the height to 220px but the width(!) to 220px as well. Why is this, is this some kind of bug in Chrome or am I just missing something here.

Weird part is, that if you'll remove the height:100% part so you are only left with the max-height:220px, this problem does not occur.

See a more detailed example below Detailed example

figure {
    width: 100%;
    max-height: 220px;
}

a {
    width: 100%;
    display: inline-block;
    text-align: center;
}

img {
    height: 100%;
    max-height:220px;
}

http://jsfiddle.net/be5jT/ JS Fiddle Example

like image 357
user007 Avatar asked Jul 28 '14 13:07

user007


People also ask

Which attribute increase the height of the image?

The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels. Example 1: In this example, we will set the width and height of an image.

Why I can't change the height of image in HTML?

There is no command for changing an image size. Image dimensions are 'properties' which can be expressed in either the HTML <img> element, as width="150" height="100" attributes in the tag; or, the CSS as style rules applying to specific images.

How do I change the height and width of an external image in CSS?

We can resize the image by specifying the width and height of an image. A common solution is to use the max-width: 100%; and height: auto; so that large images do not exceed the width of their container. The max-width and max-height properties of CSS works better, but they are not supported in many browsers.


2 Answers

What's Going On:

If you use the inspector tool, the browsers are adding width:auto;, because no width rules are declared. I've researched a bit and I can't find any reason as to WHY, but it comes down the fact that Chrome and Firefox calculated "width:auto" differently. Firefox is calculating based on proportional, and Chrome is displaying native.

enter image description here

enter image description here

I've checked the CSS2.1 Width spec, and since we are talking about an image which is inline, we have a large number of conditions to check for. The one that I think applies here is:

Otherwise, if 'width' has a computed value of 'auto', and the element has an intrinsic width, then that intrinsic width is the used value of 'width'.

Source - http://www.w3.org/TR/CSS2/visudet.html#inline-replaced-width

If I'm reading it right, that means that Chrome is technically correct, even though Firefox's method ends up looking better.

Alternative Fix Method:

lili2311's answer will work, but then you'd have to declare the width, which means that you'd have to use images which are the same proportions. You could also remove the height:``00%, which you already know. A third method would be to give the a a height:100%, change the max-height:220px to height:220px on the figure, and then remove the max-height from the img. This lets you only declare 220px once.

Code:

figure {
    width: 100%;
    height: 220px;
}
a {
    width: 100%;
    height:100%;
    display: inline-block;
    text-align: center;
}
img {
    height: 100%;
    width:auto;
}

Working Demo: jsFiddle

like image 60
Andy Mercer Avatar answered Jan 02 '23 11:01

Andy Mercer


You no need to add height, set max-height only

DEMO

img {
    max-height:220px;
}
like image 26
amol Avatar answered Jan 02 '23 13:01

amol