3

I have a problem when resizing images I have set up in admin panel.

.users-list>li img {
    border-radius: 50%;
    max-width: 100%;
    height: auto;
    width: 100px;
    height: 100px;
}

When maximized, the images are looking great:

enter image description here

If I however resize browser, they all shrink together:

enter image description here

And then I tried by deleting the height: 100px property which seems to do the trick, but one image is for some reason off:

enter image description here

Norgul
  • 4,613
  • 13
  • 61
  • 144

3 Answers3

4

If you do not want your images to stretch out, you should pin down one dimension and allow the other dimension as auto. (this preserves the aspect ratio of the image)

See the example below where width is kept constant while height auto-adjusts:

img {
  display: block;
}
.correct,
.incorrect {
  border: 1px solid red;
  display: inline-block;
}
.incorrect img {
  max-width: 100%;
  width: 100px;
  height: 100px;
}
.correct img {
  max-width: 100%;
  width: 200px;
  height: auto;
}
<div>This one stretches out:</div>
<div class="incorrect">
  <img src="https://via.placeholder.com/150x50" />
</div>

<div>This will preserve aspect ratio and look right:</div>
<div class="correct">
  <img src="https://via.placeholder.com/150x50" />
</div>

See the example below where height is kept constant while width auto-adjusts:

img {
  display: block;
}
.correct,
.incorrect {
  border: 1px solid red;
  display: inline-block;
}
.incorrect img {
  max-height: 100%;
  height: 100px;
  width: 100px;
}
.correct img {
  max-height: 100%;
  height: 200px;
  width: auto;
}
<div>This one stretches out:</div>
<div class="incorrect">
  <img src="http://placehold.it/150x50" />
</div>

<div>This will preserve aspect ratio and look right:</div>
<div class="correct">
  <img src="http://placehold.it/150x50" />
</div>
extempl
  • 2,987
  • 1
  • 26
  • 38
kukkuz
  • 41,512
  • 6
  • 59
  • 95
  • 2
    This makes a lot of sense...then my formatting is as good as it gets. Back-end needs to retrieve same dimension images in order to do what I want – Norgul Sep 02 '16 at 10:20
0

Just remove

height: 100px;

i.e

.users-list>li img {
    border-radius: 50%;
    max-width: 100%;
    height: auto;
    width: 100px;
}
0

You can also use height attribute in <img> tag.

Like this <img src="/path/to/image" height="40"> without any CSS

Prateek Gupta
  • 538
  • 2
  • 10