Resize image proportionally with CSS?

2018-12-31 21:20发布

Is there a way to resize (scale down) images proportionally using ONLY CSS?

I'm doing the JavaScript way, but just trying to see if this is possible with CSS.

18条回答
孤独总比滥情好
2楼-- · 2018-12-31 21:27

Try this:

div.container {
    max-width: 200px;//real picture size
    max-height: 100px;
}

/* resize images */
div.container img {
    width: 100%;
    height: auto;
}
查看更多
零度萤火
3楼-- · 2018-12-31 21:29

image_tag("/icons/icon.gif", height: '32', width: '32') I need to set height: '50px', width: '50px' to image tag and this code works from first try note I tried all the above code but no luck so this one works and here is my code from my _nav.html.erb:
<%= image_tag("#{current_user.image}", height: '50px', width: '50px') %>

查看更多
君临天下
4楼-- · 2018-12-31 21:30

We can resize image using CSS in the browser using media queries and the principle of responsive design.

    @media screen and (orientation: portrait) {
img.ri {
    max-width: 80%;
  }
}

@media screen and (orientation: landscape) {
  img.ri { max-height: 80%; }
}

查看更多
爱死公子算了
5楼-- · 2018-12-31 21:31
<img style="width: 50%;" src="..." />

worked just fine for me ... Or am I missing something?

Edit: But see Shawn's caveat about accidentally upsizing.

查看更多
妖精总统
6楼-- · 2018-12-31 21:32

Use this easy scaling technique

img {
    max-width: 100%;
    height: auto;
}
@media {
  img { 
    width: auto; /* for ie 8 */
  }
}
查看更多
十年一品温如言
7楼-- · 2018-12-31 21:33

To scale an image by keeping its aspect ratio

Try this,

img {
  max-width:100%;
  height:auto;
}
查看更多
登录 后发表回答