html - div 内的 img 应填充宽度或高度

标签 html css image

我有一个包含图像的div

<div class="logo-wrapper">
    <img src="img/logo.jpg">
</div>

和以下 CSS:

.logo-wrapper {
   width: 197px;
   height: 78px;
   position: relative; 
}

img {
   bottom: 0;
   display: block;
   height: 100%; /* this */
   width: 100%; /* or this, depending on image ratio */
   margin: auto;
   position: absolute;
   top: 0
}

.logo-wrapper 中的图像是动态生成的,每个图像都有不同的比例。我打算做的是根据图像的尺寸填充 img 是否在其父级的 heightwidth 中。我可以用背景图片代替,但我不想在旧的 IE 上遇到麻烦。那么有没有人有一个解决方案,img 根据其比例采用高度或宽度?

最佳答案

如果图片大于.logo-wrapper,可以使用

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

如果没有,它就不会增长,但这样你就不会有模糊的图像。

关于html - div 内的 img 应填充宽度或高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26576592/

相关文章:

javascript - 在 SWF 上放置图像

c++ - 无法从压缩纹理数据创建图像 (S3TC)

c++ - 使用 C++ 读取和显示图像

html - 创建响应式电子邮件时设置 outlook 的表格宽度

javascript - 使用 innerHTML 时 CSS 属性被覆盖

javascript - 如何以弹出窗口或新选项卡的形式打开从传单类绘制的 map ?

javascript - 如何在不将用户重定向到该 URL 页面的情况下发送 GET 值?

css - 将模态放在屏幕左侧

html - 如何在 <td> 中制作相同尺寸的图像,但图像尺寸不同

html - CssClass ="table table-striped"将透明条改为蓝色