Scale image with CSS but limit to orignial size
Asked Answered
I

4

8

For a responsive design, i would like to scale down some images:

img {
    width:100%;
    height:auto;
}

This works as expected, but if the width of the screen exceeds the original size of the image, it blows up the image.

Is there a way to limit this scaling to its original size? So that it only gets smaller if necessary?

Thanks in advance for your answer.

Willem

Isoline answered 16/12, 2014 at 13:34 Comment(0)
D
13

You could use max-width to prevent image width from becoming larger than original size.

img {
    width: auto;
    max-width: 100%;
    height: auto;
}
Delfeena answered 16/12, 2014 at 13:35 Comment(0)
I
5

Not answering for the scale bug for image size limit this could work:

img {
  max-height: max-content;
}
Imitation answered 28/10, 2019 at 17:16 Comment(0)
C
0

Put the images in a <div> and give that <div> a max-width.

CSS

#test {
    max-width: 400px;
}
img {
    width:100%;
    height:auto;
}

HTML

<div id="test">
    <img src="http://a.dilcdn.com/bl/wp-content/uploads/sites/8/2012/09/02-11.jpg" />
</div>

JSfiddle

Carton answered 16/12, 2014 at 13:37 Comment(0)
L
0

This worked for me.

CSS:

.captioned-image{
    margin: auto;
    text-align: center;
}

.captioned-image img{
    width: 100%; 
}

HTML

<div class="captioned-image">
    <img src="/img/my_image.jpg" style="width:max-content; max-width:max-content;">
</div>

You don't need to override the "width" property in the html like I did here -- I did it like that for my own project. I'm sure a css class like the one below would work, but I wanted to paste my exact code.

.img{
    max-width: max-content;
    width: max-content;
}
Lazaretto answered 30/4, 2023 at 0:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.