【发布时间】:2011-05-22 14:43:04
【问题描述】:
我有一个 48x48 的 div,里面有一个 img 元素,我想在不丢失任何部分的情况下将其放入 div 中,同时保持比例,是否可以使用 html 和 css 实现?
【问题讨论】:
我有一个 48x48 的 div,里面有一个 img 元素,我想在不丢失任何部分的情况下将其放入 div 中,同时保持比例,是否可以使用 html 和 css 实现?
【问题讨论】:
对 div 内的图片使用max-height:100%; max-width:100%;。
【讨论】:
overflow:scroll div 上,它最终也会尝试滚动正文。好奇的。更奇怪的是:Max 的解决方案实际上修复了它。我真的受够了 CSS。曾经很强大很酷。现在实现只是在错误不一致之后产生错误不一致。
不幸的是 max-width + max-height 不能完全覆盖我的任务...... 所以我找到了另一个解决方案:
要在缩放时保存图像比例,您还可以使用object-fit CSS3 属性。
有用的文章:Control image aspect ratios with CSS3
img {
width: 100%; /* or any custom size */
height: 100%;
object-fit: contain;
}
坏消息:不支持 IE (Can I Use)
【讨论】:
object-fit: cover; 为我工作。这里:*.com/questions/9071830/…
如果您在编写 css 时不知道图像的尺寸,则需要一些 JavaScript 来防止裁剪。
<div id="container">
<img src="something.jpg" alt="" />
</div>
<script type="text/javascript">
(function() {
var img = document.getElementById('container').firstChild;
img.onload = function() {
if(img.height > img.width) {
img.height = '100%';
img.width = 'auto';
}
};
}());
</script>
#container {
width: 48px;
height: 48px;
}
#container img {
width: 100%;
}
如果您使用 JavaScript 库,您可能想利用它。
【讨论】:
仅使用 CSS:
div > img {
width: auto;
height : auto;
max-height: 100%;
max-width: 100%;
}
【讨论】:
<div>
<img src="something.jpg" alt="" />
</div>
div {
width: 48px;
height: 48px;
}
div img {
display: block;
width: 100%;
}
这将使图像扩展以填充其父级,其大小在div CSS 中设置。
【讨论】:
height: 100%,这会扭曲图像比例——OP希望保持比例。
我在解决这个问题时遇到了很多问题,我发现的每一个解决方案似乎都不起作用。
我意识到我必须将 div 显示设置为 flex,所以基本上这是我的 CSS:
div{
display: flex;
}
div img{
max-height: 100%;
max-width: 100%;
}
【讨论】:
试试 CSS:
img {
object-fit: cover;
height: 48px;
}
【讨论】:
对我来说,以下 CSS 有效(在 Chrome、Firefox 和 Safari 中测试)。
有很多东西一起工作:
max-height: 100%;、max-width: 100%; 和 height: auto;、width: auto; 使 img 缩放到首先达到 100% 的维度,同时保持纵横比position: relative; 和子容器中的position: absolute; 以及top: 50%; 和left: 50%; 使容器中img 的左上角居中transform: translate(-50%, -50%); 将 img 移回左侧和顶部大小的一半,从而使 img 在容器中居中CSS:
.container {
height: 48px;
width: 48px;
position: relative;
}
.container > img {
max-height: 100%;
max-width: 100%;
height: auto;
width: auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
【讨论】:
将照片设置为背景图片可以让我们更好地控制大小和位置,让 img 标签用于不同的目的...
下面,如果我们想让 div 和照片长宽比一样,那么 placeholder.png 就是一个和 photo.jpg 长宽比一样的小透明图片。如果照片是正方形,则为 1px x 1px 占位符,如果照片是视频缩略图,则为 16x9 占位符,等等。
针对问题,使用 1x1 占位符来保持 div 的正方形比例,背景图像使用background-size 来保持照片的纵横比。
我使用了background-position: center center;,所以照片将在 div 中居中。 (将照片垂直居中或底部对齐会与 img 标签中的照片变得难看。)
div {
background: url(photo.jpg) center center no-repeat;
background-size: contain;
width: 48px; // or a % in responsive layout
}
img {
width: 100%;
}
<div><img src="placeholder.png"/></div>
为避免额外的 http 请求,convert the placeholder image to a data: URL。
<img src="data:image/png;base64,..."/>
【讨论】:
您可以将类 "img-fluid" 用于较新的版本,即 Bootstrap v4。
并且可以将类 "img-responsive" 用于旧版本,例如 Bootstrap v3。
用法:-
img 标记:-
类=“img-fluid”
src=“...”
【讨论】:
对我有用的是:
<div style='display: inline-flex; width: 80px; height: 80px;'>
<img style='max-width: 100%; max-height: 100%' src='image file'>
</div>
需要 inline-flex 以防止图像超出 div。
【讨论】:
这是一个全 JavaScript 方法。它逐渐缩小图像,直到它正确适合。您可以选择每次失败时缩小多少。此示例在每次失败时将其缩小 10%:
let fit = function (el, w, h, percentage, step)
{
let newH = h;
let newW = w;
// fail safe
if (percentage < 0 || step < 0) return { h: h, w: w };
if (h > w)
{
newH = el.height() * percentage;
newW = (w / h) * newH;
if (newW > el.width())
{
return fit(el, w, h, percentage - step, step);
}
}
else
{
newW = el.width() * percentage;
newH = (h / w) * newW;
if (newH > el.height())
{
return fit(el, w, h, percentage - step, step);
}
}
return { h: newH, w: newW };
};
img.bind('load', function ()
{
let h = img.height();
let w = img.width();
let newFit = fit($('<img-wrapper-selector>'), w, h, 1, 0.1);
img.width(newFit.w);
img.height(newFit.h);
});
随意直接复制粘贴。
【讨论】:
太晚了,但是,试试这个解决方案:
.parent {
display: flex;
min-height: 400px; /* if you prefer */
}
.parent > img {
display: block;
width: 100%;
max-width: 100%;
object-fit: cover;
}
【讨论】: