Scale image with CSS but limit to orignial size

前端 未结 3 1006
时光取名叫无心
时光取名叫无心 2021-01-19 10:13

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

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

This works as expected, but if th

相关标签:
3条回答
  • 2021-01-19 10:56

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

    img {
      max-height: max-content;
    }
    
    0 讨论(0)
  • 2021-01-19 10:59

    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

    0 讨论(0)
  • 2021-01-19 11:09

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

    img {
        width: auto;
        max-width: 100%;
        height: auto;
    }
    
    0 讨论(0)
提交回复
热议问题