11

Currently I am doing a very simple method of doing this with the code below.

<div style="width: 100%;">
    <img width="100%" height="250" src="http://i.imgur.com/lNB7QSt.jpg">
</div>

If I remove the height="250" portion of the code it prints the image to be the normal quality + normal size by I want it to be limited to 250 in height and return something like this image.

enter image description here

But instead it returns something which looks like the one displayed below.

enter image description here

7 Answers7

12

What you should do is simply use 'auto' for the CSS width attribute like this:

<img style="width:auto; height:250px" src="http://i.imgur.com/lNB7QSt.jpg">

That should do what you're expecting.

itsols
  • 5,406
  • 7
  • 51
  • 95
8

This is the only sane approach I can think of (using background-size: cover). You can probably do something tricky with the existing markup but I don't feel like it'll leave you with anything flexible enough.

.frame {
  height: 250px;
  background: transparent no-repeat center;
  background-size: cover;
}
<div class="frame" style="background-image: url('http://i.imgur.com/lNB7QSt.jpg')"></div>
Bill Criswell
  • 32,161
  • 7
  • 75
  • 66
3

I believe this can simply be done with object-fit.

<div style="width: 100%;">
 <img width="100%" height="250" src="http://i.imgur.com/lNB7QSt.jpg" style="object-fit: cover;">
</div>
Dieu-Donne
  • 31
  • 3
2

The issue is with setting width to 100%. Try removing width="100%", or set the width manually based on the aspect ratio.

0

Using the info from the link in the comment I made...

img {
    display: block;
    max-width:230px;
    max-height:250px;
    width: auto;
    height: auto;
}
<div style="width: 100%;">
    <img width="100%" height="250" src="http://i.imgur.com/lNB7QSt.jpg">
</div>
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
0

.image-name {

    display: block;
    max-width: 100%;
    max-height: 250px;

}

.image-name2 {

    display: block;
    width: auto;
    max-width: 100%;
    max-height: 250px;

}
<img class="image-name" width="100%" src="http://i.imgur.com/lNB7QSt.jpg?1">

Above is the image with no quality change + the width that you're looking for.


Below is the image with the height you're looking for but the width of the normal image.

<img class="image-name2" width="100%" src="http://i.imgur.com/lNB7QSt.jpg?1">
Slacks.
  • 199
  • 1
  • 15
0

How about something like this?

img {
  display: block;

  max-height: 200px;
  width: max-content;
}
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76