Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

percentage max-height on image ignored in firefox

HTML:

<div class="container">
    <article class="article">
        <img class="article-img" src="bordeaux.jpg" />
    </article>
</div>

CSS:

.container{
    width:500px;
    height:200px;
}
.article{
    max-height:100%;
}
.article-img{
    max-height:100%;
}

see its jsfiddle in Firefox

http://jsfiddle.net/UETMr/4/

I needed to set .article to height:100% for image to shrink in Firefox

can someone explain how does firefox understand percentage max-height and its containers ?

thanks in advance !

like image 460
Ray Tsai Avatar asked Dec 10 '13 23:12

Ray Tsai


People also ask

What happens if you do not put a width and height on an image in HTML?

Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the image, and cannot reserve the appropriate space to it.

What does max-height auto do?

The max-height CSS property sets the maximum height of an element. It prevents the used value of the height property from becoming larger than the value specified for max-height .

How do you fix the width and height of an image?

The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. 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.

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.


1 Answers

It understands it the way the W3C specifications state. For max-height to work, something other than the content itself needs to be explicitly setting the height of its containing element. This is why when you set height: 100% it works, because now you are explicitly telling .article to take its height from its parent (rather than its content). But when you have .article set with max-height, then it is still the content driving its calculated height, just with the limitation of not sizing past the .container height. And in your case, the content is the img itself.

In this fiddle, you can see that the .article is in fact constraining itself to the height of .container, but is allowing the content of itself (the img) to overflow to a larger height. The img is not constrained by its max-height because .article does not have an explicit height set, but is in fact getting its height from the img (only it is limited that it cannot go past the height of its container).

like image 64
ScottS Avatar answered Oct 14 '22 07:10

ScottS