CSS图像调整大小失真

标签 css image resize

当图像小于 400px 时,图像会按比例调整大小,并在 400px 时停止垂直缩放,但宽度会继续缩放(从而扭曲图像)。有没有什么办法解决这一问题?任何帮助将不胜感激。

CSS:

#gallery{
    height:100%;
    min-height:400px;
    max-height:800px;
    min-width:400px;
    text-align:center;
    width:100%;
}
#gallery .section {
    height:80%;
    min-height:400px;
    max-height:800px;
}
#gallery .section img{
    height:100%;
    min-height:400px;
    max-height:800px;
    width:auto;
}

HTML:

<div id="gallery">
    <div class="section">
        <img src="images/1.jpg" alt="">
    </div>
</div>

最佳答案

要保持纵横比,您必须让高度或宽度领先 - 另一个是自动。

你的规则在互相争斗。

我建议对图像使用包装器。

.image-w {
  max-width: 200px;
}

.image-w img {
  display: block;
  /*for many reasons - but mainly to get rid of the little margin-bottom that happens by default */
  width: 100%;
  height: auto;
}
<div class="image-w">
  <img src="http://placehold.it/400x400" alt="" />
</div>

关于CSS图像调整大小失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22243752/

相关文章:

html - 如何仅使用 css 在 iphone 经典风格中设置聊天气泡的样式

javascript - 在 div 中裁剪和居中任意大小的图像

python - 将 Matplotlib 图转换为没有任何边框/框架或轴的 NumPy 数组

jquery - 从 ajax 调用时 DataTable 不应用样式

css - 网页 |移动和 CSS 媒体类型

html - 我看不到 .png 文件作为图像,但作为背景图像它可以工作

javascript - 有什么方法类似于 <img> 来显示 .txt 文件的内容

php - Yii:有什么方法可以以压缩形式保存图像吗?

wpf - 在 WPF 中调整另一个网格行的大小

javascript - 如何限制幻灯片的大小 (JS)