1

I'm using centered imgs to act as backgrounds for some tiles. I'm trying to have these images scale with their parent div's height and if they are wider then their parent's for them to hide the overflow.

Example:

* I've got it working now. Answers are below, I'm updating this code to display all I needed to use to get it to work *

HTML

<div class="container">
    <img class="derp" src="http://gridiculo.us/images/kitty02.jpg">
</div>

CSS:

.container {
    height:250px;
    width:50%;
}
.derp{
  object-fit: cover;
  height: 100%;
  width: 100%;
}

Here's a near-example: http://codepen.io/chriscoyier/pen/myPMGB

The difference would be that I'm using s and not background-image, and that instead of the img filling the div completely it would fit to the height and hide the width overflow.

I'm trying to avoid using background-image since I'm using a lot of these tiles and making CSS rules for every one isn't going to work.

acwr
  • 59
  • 1
  • 7
  • If it's a background, why not use CSS backgrounds (with background-scale, background-position, etc). – GolezTrol Aug 14 '15 at 22:04
  • @marc Each element will use a different URL and I'm trying to avoid using background-images since they'll had a high number of CSS rules. – acwr Aug 14 '15 at 22:38

3 Answers3

1

In order to scale it with the div's height, I'd change the height from px to % - this way, the larger's the div, the larger's the picture. In order to certain the image, i'd use margin in the image css. That'd look like so:

    .derp{
        height:80%;
        width:80%;
        margin:10%;
     }
.container {
    height:250px;
    width:50%; /* needed */
    /* inner img is centered horizontally */
    vertical-align:top;
    text-align:center;
    overflow-x:hidden;
}
<div class="container" style="background-color:gray"> <!-- The background is there so you could see the image relative to the div -->
    <img class="derp" src="http://gridiculo.us/images/kitty02.jpg">
</div>
A. Abramov
  • 1,823
  • 17
  • 45
0

Depending on how many browsers you need to support, I'd suggest you use object-fit! Support for it is okay if you can ignore IE, but in case your project qualifies, I see no problem with using it today. Also, there is always a polyfill.

You can find a nice summary on CSS-Tricks.com about the property. It basically works similarly to background-size, but for <img> tags. In your case, object-fit: cover; does the trick.

I made a little demo on CodePen that shows you how it works.

img {
  height: 100%;
  object-fit: fill;
  width: 100%;
}
pentzzsolt
  • 1,001
  • 1
  • 9
  • 18
0

The best way to keep the aspect ratio of the image is to set the width to auto (and it's the default behavior so you don't need to set explicitly). And with a simple overflow:hidden it works almost as you want it.

The hard part is centering horizontally. You can try this answer :css to center a image horizontally.

However if all your images aren't the same size, you will need to make one rule per image. And in this case putting the image as background-img would be better for semantic and accessibility (because your image doesn't have a sense in the page, it doesn't convey any information, it's decoration). An <img> would be read by a screen reader (the alt attribute), and in your case it wouldn't help a blind people.

Community
  • 1
  • 1
florian-s
  • 96
  • 4