CSS强制调整图像大小并保持宽高比

CSS

宝儿Near

2020-03-17

我正在处理图像,但遇到宽高比问题。

<img src="big_image.jpg" width="900" height="600" alt="" />

如您所见,height并且width已经指定。我为图像添加了CSS规则:

img {
  max-width:500px;
}

但是big_image.jpg,我收到width=500height=600如何设置图像以调整尺寸,同时保持其纵横比。

第1943篇《CSS强制调整图像大小并保持宽高比》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

16个回答
Stafan小小猿 2020.03.17

I would suggest for a responsive approach the best practice would be using the Viewport units and min/max attributes as follows:

img{
  display: block;
  width: 12vw;
  height:12vw;
  max-width:100%;
  min-width:100px;
  min-height:100px;
  object-fit:contain;
}
小哥JinJin 2020.03.17

使用伪元素进行垂直对齐怎么样?这个较少的代码是用于轮播的,但是我想它可以在每个固定大小的容器上使用。它将保持宽高比,并在最短尺寸的顶部/底部或左侧/左侧插入@灰色深条。同时,图像在水平方向上通过文本对齐居中,在垂直方向上通过伪元素居中。

    > li {
      float: left;
      overflow: hidden;
      background-color: @gray-dark;
      text-align: center;

      > a img,
      > img {
        display: inline-block;
        max-height: 100%;
        max-width: 100%;
        width: auto;
        height: auto;
        margin: auto;
        text-align: center;
      }

      // Add pseudo element for vertical alignment of inline (img)
      &:before {
        content: "";
        height: 100%;
        display: inline-block;
        vertical-align: middle;
      }
    }
老丝番长 2020.03.17

全屏演示:

img[data-attribute] {height: 100vh;}

请记住,如果视口高度大于图像,则图像会自然地相对于差异降低。

路易Harry 2020.03.17

这是一个解决方案:

img {
   width: 100%;
   height: auto;
   object-fit: cover;
}

这样可以确保图像始终覆盖整个父对象(上下缩放),并保持相同的宽高比。

猿蛋蛋凯 2020.03.17

您可以使用此:

img { 
    width: 500px; 
    height: 600px; 
    object-fit: contain; 
    position: relative; 
    top: 50%; 
    transform: translateY(-50%); 
}
小小逆天 2020.03.17

要强制使用适合大小的图像,无需编写太多代码。很简单

img{
    width: 200px;
    height: auto;
    object-fit: contain; /* Fit logo in the image size */
        -o-object-fit: contain; /* Fit logo fro opera browser */
    object-position: top; /* Set logo position */
        -o-object-position: top; /* Logo position for opera browser */
    }
<img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png" alt="Logo">

村村蛋蛋 2020.03.17

https://jsfiddle.net/sot2qgj6/3/

如果要放置宽度固定的图像,而不是宽度固定的像素,这是答案

这在处理不同尺寸的屏幕时很有用

窍门是

  1. 使用padding-top设置从宽度的高度。
  2. 使用position: absolute把图像中的填充空间。
  3. 使用max-height and max-width以确保图像不会在父元素。
  4. 使用display:block and margin: auto居中图像。

我也评论了小提琴里面的大多数技巧。


我还发现了实现此目标的其他方法。HTML中将没有真实的图像,因此当我需要HTML中的“ img”元素时,我个人会提出最佳答案。

通过使用背景 http://jsfiddle.net/4660s79h/2/实现简单的CSS

顶部带有单词的背景图片 http://jsfiddle.net/4660s79h/1/

使用位置绝对值的概念来自此处 http://www.w3schools.com/howto/howto_css_aspect_ratio.asp

卡卡西神奇 2020.03.17

有保存用于与图像的纵横比没有标准的方式widthheightmax-width指定到一起。

因此,我们被迫在加载图像时指定widthheight防止页面“跳转”,或者使用max-width而不指定图像尺寸。

仅指定width(不带height)通常没有多大意义,但是您可以尝试height通过IMG {height: auto; }在样式表中添加类似规则来覆盖HTML属性

另请参阅相关的Firefox 错误392261

GoldenGG 2020.03.17

要在保持图像响应性的同时仍使图像具有一定的纵横比,可以执行以下操作:

HTML:

<div class="ratio2-1">
   <img src="../image.png" alt="image">
</div>

和SCSS:

.ratio2-1 {
  overflow: hidden;
  position: relative;

  &:before {
    content: '';
    display: block;
    padding-top: 50%; // ratio 2:1
  }

  img {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
  }
}

无论作者上传的图像大小如何,都可以使用它来强制执行一定的宽高比。

感谢@Kseso,网址http://codepen.io/Kseso/pen/bfdhg检查此URL以获取更多比率和有效示例。

StafanDavaid 2020.03.17

将图像容器标签的CSS类设置为image-class

<div class="image-full"></div>

并将其添加到您的CSS样式表中。

.image-full {
    background: url(...some image...) no-repeat;
    background-size: cover;
    background-position: center center;
}
老丝蛋蛋 2020.03.17

只需将其添加到您的CSS中,它将自动缩小和扩展并保持原始比例。

img {
    display: block;
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
}
Gil番长 2020.03.17

删除“高度”属性。

<img src="big_image.jpg" width="900" alt=""/>

通过同时指定两者,可以更改图像的纵横比。仅设置一个将调整大小,但保留宽高比。

(可选)限制尺寸过大:

<img src="big_image.jpg" width="900" alt="" style="max-width:500px; height:auto; max-height:600px;"/>
Stafan猪猪 2020.03.17

与此处的某些答案非常相似,但就我而言,我的图像有时更高,有时更大。

这种样式就像一种魅力一样,可以确保所有图像都使用所有可用空间,保持比例而不是裁切:

.img {
   object-fit: contain;
   max-width: 100%;
   max-height: 100%;
   width: auto;
   height: auto;
}
古一Gil 2020.03.17

我一直在努力地解决这个问题,最终得出了一个简单的解决方案:

object-fit: cover;
width: 100%;
height: 250px;

您可以调整宽度和高度以适合您的需求,object-fit属性将为您进行裁剪。

干杯。

Gil逆天 2020.03.17

下面的解决方案将允许放大和缩小图像,具体取决于父盒的宽度。

所有图像都有一个宽度固定的父容器,仅用于演示目的在生产中,这将是父盒的宽度。

最佳实践(2018):

该解决方案告诉浏览器以最大可用宽度渲染图像,并将高度调整为该宽度的百分比。

.parent {
  width: 100px;
}

img {
  display: block;
  width: 100%;
  height: auto;
}
<p>This image is originally 400x400 pixels, but should get resized by the CSS:</p>
<div class="parent">
  <img width="400" height="400" src="https://placehold.it/400x400">
</div>

更好的解决方案:

使用更高级的解决方案,无论大小如何,您都可以裁剪图像,并添加背景色以补偿裁剪。

.parent {
  width: 100px;
}

.container {
  display: block;
  width: 100%;
  height: auto;
  position: relative;
  overflow: hidden;
  padding: 34.37% 0 0 0; /* 34.37% = 100 / (w / h) = 100 / (640 / 220) */
}

.container img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}
<p>This image is originally 640x220, but should get resized by the CSS:</p>
<div class="parent">
  <div class="container">
    <img width="640" height="220" src="https://placehold.it/640x220">
  </div>
</div>

对于指定填充的行,您需要计算图像的长宽比,例如:

640px (w) = 100%
220px (h) = ?

640/220 = 2.909
100/2.909 = 34.37%

因此,顶部填充= 34.37%。

乐Mandy 2020.03.17

img {
  display: block;
  max-width:230px;
  max-height:95px;
  width: auto;
  height: auto;
}
<p>This image is originally 400x400 pixels, but should get resized by the CSS:</p>
<img width="400" height="400" src="http://i.stack.imgur.com/aEEkn.png">

如果图像在指定区域过大,则会缩小图像(不利的是,它不会放大图像)。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android