使用CSS按比例调整图像大小?

发布于 2024-07-17 06:25:36 字数 85 浏览 4 评论 0原文

有没有办法仅使用 CSS 按比例调整(缩小)图像大小?

我正在使用 JavaScript 方式,但只是想看看这是否可以用 CSS 实现。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

预谋 2024-07-24 06:25:36

要使用 CSS 按比例调整图像大小:

img.resize {
    width:540px; /* you can use % */
    height: auto;
}

To resize the image proportionally using CSS:

img.resize {
    width:540px; /* you can use % */
    height: auto;
}
春夜浅 2024-07-24 06:25:36

控制大小并保持比例:

#your-img {
    height: auto; 
    width: auto; 
    max-width: 300px; 
    max-height: 300px;
}

Control size and maintain proportion :

#your-img {
    height: auto; 
    width: auto; 
    max-width: 300px; 
    max-height: 300px;
}
水中月 2024-07-24 06:25:36

如果是背景图片,请使用background-size:contain。

示例CSS:

#your-div {
  background: url('image.jpg') no-repeat;
  background-size:contain;
}

If it's a background image, use background-size:contain.

Example css:

#your-div {
  background: url('image.jpg') no-repeat;
  background-size:contain;
}
疯了 2024-07-24 06:25:36

尝试

transform: scale(0.5, 0.5);
-ms-transform: scale(0.5, 0.5);
-webkit-transform: scale(0.5, 0.5);

Try

transform: scale(0.5, 0.5);
-ms-transform: scale(0.5, 0.5);
-webkit-transform: scale(0.5, 0.5);
鹿港小镇 2024-07-24 06:25:36

您可以使用object-fit属性:

.my-image {
    width: 100px;
    height: 100px;
    object-fit: contain;
}

这将适合图像,而不改变比例。

You can use object-fit property:

.my-image {
    width: 100px;
    height: 100px;
    object-fit: contain;
}

This will fit image, without changing the proportionally.

娇纵 2024-07-24 06:25:36

请注意,width:50% 会将其大小调整为图像的可用空间的 50%,而 max-width:50% 将调整大小图像自然尺寸的 50%。 在移动网页设计中使用此规则时,这一点非常重要,因此对于移动网页设计,应始终使用 max-width

更新:这可能是一个旧的 Firefox 错误,现在似乎已经修复。

Notice that width:50% will resize it to 50% of the available space for the image, while max-width:50% will resize the image to 50% of its natural size. This is very important to take into account when using this rules for mobile web design, so for mobile web design max-width should always be used.

UPDATE: This was probably an old Firefox bug, that seems to have been fixed by now.

心碎无痕… 2024-07-24 06:25:36

要通过保持图像的纵横比来缩放图像

试试这个,

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

To scale an image by keeping its aspect ratio

Try this,

img {
  max-width:100%;
  height:auto;
}
打小就很酷 2024-07-24 06:25:36

2015 年重新审视:

<img src="http://imageurl" style="width: auto; height: auto;max-width: 120px;max-height: 100px">

我重新审视了它,因为所有常见的浏览器现在都具有上面 Cherif 建议的工作自动功能,因此效果更好,因为您不需要知道图像是否更宽而不是更高。

旧版本:
例如,如果您受到 120x100 盒子的限制,您可以这样做

<img src="http://image.url" height="100" style="max-width: 120px">

Revisited in 2015:

<img src="http://imageurl" style="width: auto; height: auto;max-width: 120px;max-height: 100px">

I've revisited it as all common browsers now have working auto suggested by Cherif above, so that works even better as you don't need to know if image is wider than taller.

older version:
If you are limited by box of 120x100 for example you can do

<img src="http://image.url" height="100" style="max-width: 120px">
梦途 2024-07-24 06:25:36
<img style="width: 50%;" src="..." />

对我来说工作得很好......或者我错过了什么?

编辑:但是请参阅肖恩关于意外增大尺寸的警告。

<img style="width: 50%;" src="..." />

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

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

绝情姑娘 2024-07-24 06:25:36

css 属性 max-width 和 max-height 工作很好,但 IE6 不支持,我相信 IE7。 您可能希望在高度/宽度上使用此功能,这样您就不会意外放大图像。 您只想按比例限制最大高度/宽度。

The css properties max-width and max-height work great, but aren't supported by IE6 and I believe IE7. You would want to use this over height / width so you don't accidentally scale an image up. You would just want to limit the maximum height/width proportionately.

因为看清所以看轻 2024-07-24 06:25:36
img{
    max-width:100%;
    object-fit: scale-down;
}

对我有用。 它会缩小较大的图像以适合框,但保留较小的图像的原始大小。

img{
    max-width:100%;
    object-fit: scale-down;
}

works for me. It scales down larger images to fit in the box, but leaves smaller images their original size.

自演自醉 2024-07-24 06:25:36

我相信这是最简单的方法,也可以通过 标记中的内联 style 属性使用。

.scaled
{
  transform: scale(0.7); /* Equal to scaleX(0.7) scaleY(0.7) */
}

<img src="flower.png" class="scaled">

或者

<img src="flower.png" style="transform: scale(0.7);">

I believe this is the easiest way to do it, also possible using through the inline style attribute within the <img> tag.

.scaled
{
  transform: scale(0.7); /* Equal to scaleX(0.7) scaleY(0.7) */
}

<img src="flower.png" class="scaled">

or

<img src="flower.png" style="transform: scale(0.7);">
戏舞 2024-07-24 06:25:36

使用这种简单的缩放技术

img {
    max-width: 100%;
    height: auto;
}
@media {
  img { 
    width: auto; /* for ie 8 */
  }
}

Use this easy scaling technique

img {
    max-width: 100%;
    height: auto;
}
@media {
  img { 
    width: auto; /* for ie 8 */
  }
}
迷途知返 2024-07-24 06:25:36
img {
    max-width:100%;
}

div {
    width:100px;
}

通过这个片段,你可以以更有效的方式做到这一点

img {
    max-width:100%;
}

div {
    width:100px;
}

with this snippet you can do it in a more efficient way

醉城メ夜风 2024-07-24 06:25:36

我们可以使用媒体查询和响应式设计原理在浏览器中使用 CSS 来调整图像大小。

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

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

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%; }
}

驱逐舰岛风号 2024-07-24 06:25:36

总是需要这样的东西

html
{
    width: 100%;
    height: 100%;
}

你的 css 文件顶部

You always need something like this

html
{
    width: 100%;
    height: 100%;
}

at the top of your css file

一枫情书 2024-07-24 06:25:36

尝试这个:

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

/* resize images */
div.container img {
    width: 100%;
    height: auto;
}

Try this:

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

/* resize images */
div.container img {
    width: 100%;
    height: auto;
}
爱她像谁 2024-07-24 06:25:36

image_tag("/icons/icon.gif",高度:'32',宽度:'32')
我需要将 height: '50px', width: '50px' 设置为图像标签,此代码从第一次尝试开始工作,注意我尝试了上述所有代码,但没有运气,所以这个有效,这是我的 _nav.html 中的代码。 erb:
<%= image_tag("#{current_user.image}", 高度: '50px', 宽度: '50px') %>

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') %>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文