CSS: How can I set image size relative to parent height?

css image size scale to fit
css object-fit
css transform scale to fit
the img element should be centered within its parent element.
css fit-content
set image to height of container
background image css
resize image css

I am trying to figure out how to re-size an image so that it keeps it ratio of width to height, but gets re-sized until the height of the image matches the height of the containing div. I have these images that are pretty large and long (screenshots), and I want to put them into a 200px width, 180px height div for display and without re-sizing the images manually. To make this look good, the sides of the image need to overflow and be hidden with the containing div. This is what I have so far:

http://jsfiddle.net/f9krj/2/

HTML

<a class="image_container" href="http://www.skintype.ca/assets/background-x_large.jpg">
    <img src="http://www.skintype.ca/assets/background-x_large.jpg" alt="" />
</a>

CSS

a.image_container {
    background-color: #999;
    width: 200px;
    height: 180px;
    display: inline-block;
    overflow: hidden;
}
a.image_container img {
    width: 100%;
}

As you can see, there is grey color showing on the images parent container which should not be shown at all. In order for that container to be filled completely, the width needs to be overflowed equally on both sides. Is this possible? Is it also possible to account for an image that is also too tall?

Tribute Page, responsively resize img relative to parent element , object-fit can be set with one of these five values: image in the process. none : image will ignore the height and width of the parent and retain  DIV is set to the relative position. This means all the child elements will get the starting coordinates (origins) from where this DIV starts. The image is set as a BLOCK element, min-width/height both set to 100% means to resize the image no matter of its size to be the minimum of 100% of it’s parent. min is the key. If by min-height, the image height exceeded the parent’s height, no problem.

Change your code:

a.image_container img {
    width: 100%;
}

To this:

a.image_container img {
    width: auto; // to maintain aspect ratio. You can use 100% if you don't care about that
    height: 100%;
}

http://jsfiddle.net/f9krj/5/

object-fit, How do I change the height and width of an image in a div? Actually using 100% will not make the image bigger if the image is smaller than the div size you specified. You need to set one of the dimensions, height or width in order to have all images fill the space. In my experience it's better to have the height set so each row is the same size, then all items wrap to next line properly.

Use max-width property of CSS, like this :

img{
  max-width:100%;
}

How do I auto-resize an image to fit a 'div' container?, How do I make a picture fit into a container? I can make an image scale responsively to a parent div on smaller screens but still limit to an explicit max width: img { max-width: 500px; width: 100%; } However, if the image is smaller than the parent div, it is still stretched to fill the div.

If you take answer's Shekhar K. Sharma, and it almost work, you need also add to your this height: 1px; or this width: 1px; for must work.

Simple Responsive Images With CSS Background Images , How do I make my background image fit the size of a div? Auto resize an image (img) to fit into a smaller Div can be achieved through simple CSS or CSS3. In this tutorial I will explain both CSS and CSS3 ways using simple html example.Add max-width,max-height CSS as 100% for img tag,then image will be auto resized.Use CSS3 object-fit:contain to resize img to fit into div.

Height Property, How can I make all images of different height and width the same via CSS? length - Defines the height/width in px, cm etc. % - Defines the height/width in percent of the containing block. initial - Sets the height/width to its default value. inherit - The height/width will be inherited from its parent value.

Fill an image to the height of a div - HTML & CSS, .wrap { height: auto; /* auto keyword */ height: 120px; /* length values value */ height: inherit; /* inherited value from parent element */ } Similarly, if the height of the container is set to a percentage value, Height can also be used as an animatable property. image-rendering · initial-letter · isolation. Hi, I have a parent div with height: auto. In this div, there are two other divs. One with an icon, one with some text in it. These divs both have height: auto as well so the text mainly decides

Relative div with absolute images - HTML & CSS, Hi, I have an image that I would like to fill the height of a div, but not sure how position:relative; overflow:hidden; background:url(images/a1-red.jpg) its height would have to increase which means it's width would increase,  One of the common CSS techniques that can be a bit tricky at first is being able to absolutely position a child div element within a parent div container, relative to the parent. There are numerous scenarios where you might require this sort of positioning for div and other HTML elements.

Comments
  • Have you tried using a CSS background image and setting background-size: cover;?
  • use max-height:100%; instead of width, fiddle
  • @CP510 ya you are right. I should have found this out! jsfiddle.net/f9krj/4
  • Possible duplicate of Make an image width 100% of parent div, but not bigger than its own width
  • It works but I don't really get it. Someone mind to explain?
  • This is CSS sorcery at its finest.
  • Since it has yet to be explained at all: Can someone please explain this? Feel free to edit the answer.
  • This cut down the image, rather than doing resize.
  • This is the perfect solution. Thanks! It sets the height and width of an image according to a parent element, rather than cut down the image. Thanks again!