Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

img { max-height: 100%; } causes img to exceed bounds

Is this a Chrome bug?

Here's the HTML:

<div><img src="test.png"></div>

Here's the CSS:

* { box-sizing: border-box; }
div { height: 200px; padding: 75px 0 60px; }
img { max-height: 100%; }

Expected result: The img should have a height of 65px.

Result in Chrome (v. 27.0.1453.116) on Mac OS (v. 10.6.8): The img has height of 135px and "bleeds" into the parent div's padding. If I change the padding of the div to 50px 0, oddly it renders properly.

Play with this in a codepen: http://codepen.io/anon/pen/jhbKz

Screenshots:

First block has padding of 50px 0. Second block has padding of 75px 0 60px.

Firefox (correct result)

Firefox

Chrome (wrong?)

Chrome

like image 683
charles Avatar asked Jun 26 '13 19:06

charles


People also ask

Why is my Div bigger than my image?

The reason behind your issue is that you did not specify the width of the container but, in the same time, you set a width: 100%; for the image.

How do I make an image 100 width in CSS?

if the image is taller than it is wide, I would use height=100% and width=auto, if the image is wider than it is tall, I would use width=100%, height=auto.

How do you stop an image from growing in CSS?

Replace the img tag with a div and set the image as the background image, and add this to the styling: Background-size:contain; This will allow the image to maintain its aspect ratio but fill the entire space of the div as much as possible.


1 Answers

Try adding a container to your Image with width and height of 100%. This will give you the same output on chrome and FF.

<div class="b">
    <div style='height:100%;width:100%;'>
        <img src="some image url">
    </div>
</div>

I cannot explain why this fix works currently, but I myself am trying to reason with it.

like image 75
Pav Avatar answered Nov 09 '22 23:11

Pav