When I use image tags in html, I try to specify its width and height in the img
tag, so that the browser will reserve the space for them even before the images are loaded, so when they finish loading, the page does not reflow (the elements do not move around). For example:
当我在html中使用图像标签时,我尝试在img标签中指定其宽度和高度,以便浏览器甚至在加载图像之前为它们保留空间,因此当它们完成加载时,页面不会重排(元素不会四处移动)。例如:
<img width="600" height="400" src="..."/>
The problem is now I want to create a more "responsive" version, where for the "single column case" I'd like to do this:
问题是现在我想创建一个更“响应”的版本,对于“单列案例”,我想这样做:
<img style="max-width: 100%" src="..."/>
but, if I mix this with explicitly specified width and height, like:
但是,如果我将其与明确指定的宽度和高度混合使用,例如:
<img style="max-width: 100%" width="600" height="400" src="..."/>
and the image is wider than the available space, then the image is resized ignoring the aspect ratio. I understand why this happens (because I "fixed" the height of the image), and I would like to fix this, but I have no idea how.
并且图像比可用空间宽,然后忽略宽高比调整图像大小。我理解为什么会发生这种情况(因为我“修复了”图像的高度),我想解决这个问题,但我不知道怎么做。
To summarize: I want to be able to specify max-width: 100%
, and also somehow make sure the content is not reflowed when the images are loaded.
总结一下:我希望能够指定max-width:100%,并且还可以确保在加载图像时内容不会被重排。
5 个解决方案
#1
6
I'm also looking for the answer to this problem. With max-width
, width=
and height=
, the browser has enough data that it should be able to leave the right amount of space for an image but it just doesn't seem to work that way.
我也在寻找这个问题的答案。使用max-width,width =和height =,浏览器有足够的数据,它应该能够为图像留出适当的空间,但它似乎不会那样工作。
I worked around this with a jQuery solution for now. It requires you to provide the width=
and height=
for your <img>
tags.
我现在用jQuery解决方案来解决这个问题。它要求您为标记提供width =和height =。
CSS:
CSS:
img { max-width: 100%; height: auto; }
HTML:
HTML:
<img src="image.png" width="400" height="300" />
jQuery:
jQuery的:
$('img').each(function() {
var aspect_ratio = $(this).attr('height') / $(this).attr('width') * 100;
$(this).wrap('<div style="padding-bottom: ' + aspect_ratio + '%">');
});
This automatically applies the technique seen on: http://andmag.se/2012/10/responsive-images-how-to-prevent-reflow/
这会自动应用以下技术:http://andmag.se/2012/10/responsive-images-how-to-prevent-reflow/
#2
2
At first I would like to write about the answer from october 2013. This was incomplete copied and because of them it is not correct. Do not use it. Why? We can see it in this snippet (scroll the executed snippet to the bottom):
起初我想写一下2013年10月的答案。这是不完整的复制,因为它们是不正确的。不要使用它。为什么?我们可以在此代码段中看到它(将已执行的代码段滚动到底部):
$('img').each(function() {
var aspect_ratio = $(this).attr('height') / $(this).attr('width') * 100;
$(this).wrap('<div style="padding-bottom: ' + aspect_ratio + '%">');
});
img { max-width: 100%; height: auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width:300px;border:1px solid red">
<img width="400" height="300" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAIAAABi1XKVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAf3SURBVHhe7do9chtXGoVhb2Ryr0TllSjwLpx7CY6cusrZBFqAEydegPMpL2NEUhTvPf0HkBBxDDxVT6BXajbB7r5fNyB+992v/wD8O2QD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboF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ottom. What is in this example incomplete/ incorrect? I will show it with correct example with pure JavaScript (we do not need to download jQuery for that).
我们可以从底部看到文本远远不够。此示例中的内容不完整/不正确?我将使用纯JavaScript(我们不需要为此下载jQuery)以正确的示例显示它。
Correct example with pure JavaScript
使用纯JavaScript的正确示例
Please scroll the executed snippet to the bottom.
请将已执行的代码段滚动到底部。
var imgs = document.querySelectorAll('img');
for(var i = 0; i < imgs.length; i++)
{
var aspectRatio = imgs[i].getAttribute('height') /
imgs[i].getAttribute('width') * 100;
var div = document.createElement('div');
div.style.paddingBottom = aspectRatio + '%';
imgs[i].parentNode.insertBefore(div, imgs[i]);
div.appendChild(imgs[i]);
}
.restrict-container div{position:relative}
img
{
position:absolute;
max-width:100%;
top:0; left:0;
height:auto
}
<div class="restrict-container" style="width:300px;border:1px solid red">
<img width="400" height="300" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAIAAABi1XKVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAf3SURBVHhe7do9chtXGoVhb2Ryr0TllSjwLpx7CY6cusrZBFqAEydegPMpL2NEUhTvPf0HkBBxDDxVT6BXajbB7r5fNyB+992v/wD8O2QD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY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ALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgE6/frP/wEiNOVSBSWikgAAAABJRU5ErkJggg=="/>
Some text<br>
<img width="400" height="300" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAIAAABi1XKVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAf3SURBVHhe7do9chtXGoVhb2Ryr0TllSjwLpx7CY6cusrZBFqAEydegPMpL2NEUhTvPf0HkBBxDDxVT6BXajbB7r5fNyB+992v/wD8O2QD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxsu5ref/vef/z775Y8/FxvAmbLhYgwsLi377n3/x4evayxW2g9/PeRPn6bt2WZgcWnZd2w5qgYfPn76+ZenP//98/eLrwXeQ/a92p1W4ccfFl8OvIfs+/Tpx3EkxZuXp3eCAwMLriT7Lo0jaf0jqmmiGVhwJdn36M+Pfz8Po83Pp4ZtDgfW78+fdr348PH3xWYrz26fx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28OaXCzUacHkw+COi6+Q7R9nx8OkuPBx2D8LG2d5cWaX+9+yNrAeXPNc3K7sO/Tlwnq5aNZuj+dcUmvX+tfnoNHW0nrFk8XsYAR8Iw1PWM+WR+BlJK18u623qyun8mk/wwP1wSG9zrm4WdnAsdMHFheVDRwzsK4kGzgyvvldfbPPt5IN7Jo+qjv8XQ0uKxt4tPVfAQO/PPXusoEHh79YZ1pdQzbwaOe3NHxudTXZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgE6/frP/wEiNOVSBSWikgAAAABJRU5ErkJggg=="/>
Some text
</div>
The mistake from answer from october 2013: the image should be placed absolute (position:absolute
) to the wrapped container but it is not so placed.
从2013年10月回答的错误:图像应该被放置在绝对的位置(位置:绝对)到包裹的容器,但它不是那么放置。
This is the end of my answer to this question.
这是我对这个问题的答案的结束。
For further information read more about:
欲了解更多信息,请阅读更多:
- What could we do with the new HTML5 technology for responsive images too? (this is previous extended version of my posting here. See there the second part).
- 我们还可以使用新的HTML5技术来处理响应式图像吗? (这是我在这里发布的先前扩展版本。请参见第二部分)。
#3
2
For a css only solution, you can wrap the img
in a container where the padding-bottom
percentage reserves space on the page until the image loads, preventing reflow.
对于仅css解决方案,您可以将img包装在容器中,其中填充底部百分比在页面上保留空间直到图像加载,从而防止重排。
Unfortunately, this approach does require you to include the image aspect ratio in your css (but no need for inline styles) by calculating (or letting css calculate for you) the padding-bottom
percentage based on the image height and width.
不幸的是,这种方法确实要求您在css中包含图像宽高比(但不需要内联样式),方法是根据图像的高度和宽度计算(或让css计算)填充底部百分比。
If many of your images can be grouped into a few standard aspect ratios, then you could create a class for each aspect ratio to apply the appropriate padding-bottom
percentage to all images with that aspect ratio. This may save you a little time and effort if you are not dealing with a wide variety of image aspect ratios.
如果您的许多图像可以分组为几个标准宽高比,那么您可以为每个宽高比创建一个类,以将适当的填充底部百分比应用于具有该宽高比的所有图像。如果您不处理各种图像宽高比,这可能会节省您一点时间和精力。
Following is some example html and css for an image with a 2:1 aspect ratio:
以下是具有2:1宽高比的图像的一些示例html和css:
HTML
HTML
<div class="container">
<img id="image" src="https://via.placeholder.com/300x150" />
</div>
CSS
CSS
.container {
display: block;
position: relative;
padding-bottom: 50%; /* calc(100%/(300/150)); */
height: 0;
}
.container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
The snippet below adds some extra html, css and javascript to create some visual top and bottom reference points and mimic a very slow loading image so you can visually see how the reflow is prevented with this approach.
下面的代码片段添加了一些额外的html,css和javascript来创建一些可视的顶部和底部参考点,并模拟一个非常慢的加载图像,这样您就可以直观地看到这种方法如何防止回流。
const image = document.getElementById('image');
const source = 'https://via.placeholder.com/300x150';
const changeSource = () => image.src = source;
setTimeout(changeSource, 3000);
.container {
display: block;
position: relative;
padding-bottom: 50%; /* calc(100%/(300/150)); */
height: 0;
}
.container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.top, .bottom {
background-color: green;
width: 100%;
height: 20px;
}
<div class="top"></div>
<div class="container">
<img id="image" src="" />
</div>
<div class="bottom"></div>
#4
2
From this blog post by Jonathan Hollin: add the image's height and width as part of an inline style. This reserves space for the image, preventing reflow when the image loads, but it's also responsive.
在Jonathan Hollin的博客文章中:添加图像的高度和宽度作为内联样式的一部分。这为图像保留了空间,防止了图像加载时的回流,但它也具有响应性。
HTML
HTML
<figure style="padding-bottom: calc((400/600)*100%)">
<img src="/images/kitten.jpg" />
</figure>
CSS
CSS
figure {
position: relative;
}
img {
max-width: 100%;
position: absolute;
}
The figure
can be replaced with a div
or any other container of your choice. This solution relies on CSS calc() which has pretty wide browser support.
该图可以替换为div或您选择的任何其他容器。这个解决方案依赖于CSS calc(),它具有相当广泛的浏览器支持。
Working Codepen can be seen here.
在这里可以看到工作Codepen。
UPDATE:
更新:
I found a much cleverer alternate version of this: http://cssmojo.com/aspect-ratio-using-custom-properties-and-calc/. This still requires a wrapper element and it requires CSS custom properties, but I think it's much more elegant. Codepen example is here (credit to Chris Coyier's original).
我找到了一个更聪明的替代版本:http://cssmojo.com/aspect-ratio-using-custom-properties-and-calc/。这仍然需要一个包装元素,它需要CSS自定义属性,但我认为它更优雅。 Codepen的例子在这里(归功于Chris Coyier的原创)。
#5
0
If I understand the requirements ok, you want to be able to set an image size, where this size is known only on content (HTML) generation, so it can be set as inline styles.
如果我理解了这些要求,那么您希望能够设置图像大小,此大小仅在内容(HTML)生成时已知,因此可以将其设置为内联样式。
But this has to be independent of the CSS, and also prior to image loading, so also independent from this image sizes.
但这必须独立于CSS,并且在图像加载之前,因此也独立于此图像大小。
I have come to a solution tha involves wrapping the image in a div, and including in this div an svg that can be set to have proportions directly as an inline style.
我已经找到了一个解决方案,涉及将图像包装在div中,并在此div中包含一个svg,可以将其设置为直接作为内联样式的比例。
Obviously this is not much semantic, but at least it works
显然这不是很多语义,但至少它是有效的
The containing div has a class named img to show that it , well, should be an img
包含div有一个名为img的类,以表明它应该是一个img
To try to reproduce the loading stage, the images have a broken src
为了尝试重现加载阶段,图像具有破坏的src
.container {
margin: 10px;
border: solid 1px black;
width: 200px;
height: 400px;
position: relative;
}
.img {
border: solid 1px red;
width: fit-content;
max-width: 100%;
position: relative;
}
svg {
max-width: 100%;
background-color: lightgreen;
opacity: 0.1;
}
#ct2 {
width: 500px;
}
.img img {
position: absolute;
width: 100%;
height: 100%;
max-height: 100%;
max-width: 100%;
top: 0px;
left: 0px;
box-shadow: inset 0px 0px 10px blue;
}
<div class="container" id="ct1">
<div class="img">
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 400 300" width="400">
</svg>
<img width="400" height="300" src="missing.jpg">
</div>
</div>
<div class="container" id="ct2">
<div class="img">
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 40 30" width="400">
</svg>
<img width="400" height="300" src="missing.jpg">
</div>
</div>
#1
6
I'm also looking for the answer to this problem. With max-width
, width=
and height=
, the browser has enough data that it should be able to leave the right amount of space for an image but it just doesn't seem to work that way.
我也在寻找这个问题的答案。使用max-width,width =和height =,浏览器有足够的数据,它应该能够为图像留出适当的空间,但它似乎不会那样工作。
I worked around this with a jQuery solution for now. It requires you to provide the width=
and height=
for your <img>
tags.
我现在用jQuery解决方案来解决这个问题。它要求您为标记提供width =和height =。
CSS:
CSS:
img { max-width: 100%; height: auto; }
HTML:
HTML:
<img src="image.png" width="400" height="300" />
jQuery:
jQuery的:
$('img').each(function() {
var aspect_ratio = $(this).attr('height') / $(this).attr('width') * 100;
$(this).wrap('<div style="padding-bottom: ' + aspect_ratio + '%">');
});
This automatically applies the technique seen on: http://andmag.se/2012/10/responsive-images-how-to-prevent-reflow/
这会自动应用以下技术:http://andmag.se/2012/10/responsive-images-how-to-prevent-reflow/
#2
2
At first I would like to write about the answer from october 2013. This was incomplete copied and because of them it is not correct. Do not use it. Why? We can see it in this snippet (scroll the executed snippet to the bottom):
起初我想写一下2013年10月的答案。这是不完整的复制,因为它们是不正确的。不要使用它。为什么?我们可以在此代码段中看到它(将已执行的代码段滚动到底部):
$('img').each(function() {
var aspect_ratio = $(this).attr('height') / $(this).attr('width') * 100;
$(this).wrap('<div style="padding-bottom: ' + aspect_ratio + '%">');
});
img { max-width: 100%; height: auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width:300px;border:1px solid red">
<img width="400" height="300" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAIAAABi1XKVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAf3SURBVHhe7do9chtXGoVhb2Ryr0TllSjwLpx7CY6cusrZBFqAEydegPMpL2NEUhTvPf0HkBBxDDxVT6BXajbB7r5fNyB+992v/wD8O2QD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxsu5ref/vef/z775Y8/FxvAmbLhYgwsLi377n3/x4evayxW2g9/PeRPn6bt2WZgcWnZd2w5qgYfPn76+ZenP//98/eLrwXeQ/a92p1W4ccfFl8OvIfs+/Tpx3EkxZuXp3eCAwMLriT7Lo0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fzMD3vB1l7OLr6ubhZ2ffn6yX+efEcXe4Ht8HD1TIPu9V7/qPHNbDc28OaXCzUacHkw+COi6+Q7R9nx8OkuPBx2D8LG2d5cWaX+9+yNrAeXPNc3K7sO/Tlwnq5aNZuj+dcUmvX+tfnoNHW0nrFk8XsYAR8Iw1PWM+WR+BlJK18u623qyun8mk/wwP1wSG9zrm4WdnAsdMHFheVDRwzsK4kGzgyvvldfbPPt5IN7Jo+qjv8XQ0uKxt4tPVfAQO/PPXusoEHh79YZ1pdQzbwaOe3NHxudTXZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgE6/frP/wEiNOVSBSWikgAAAABJRU5ErkJggg=="/>
Some text
</div>
And we can see the text is afar from bottom. What is in this example incomplete/ incorrect? I will show it with correct example with pure JavaScript (we do not need to download jQuery for that).
我们可以从底部看到文本远远不够。此示例中的内容不完整/不正确?我将使用纯JavaScript(我们不需要为此下载jQuery)以正确的示例显示它。
Correct example with pure JavaScript
使用纯JavaScript的正确示例
Please scroll the executed snippet to the bottom.
请将已执行的代码段滚动到底部。
var imgs = document.querySelectorAll('img');
for(var i = 0; i < imgs.length; i++)
{
var aspectRatio = imgs[i].getAttribute('height') /
imgs[i].getAttribute('width') * 100;
var div = document.createElement('div');
div.style.paddingBottom = aspectRatio + '%';
imgs[i].parentNode.insertBefore(div, imgs[i]);
div.appendChild(imgs[i]);
}
.restrict-container div{position:relative}
img
{
position:absolute;
max-width:100%;
top:0; left:0;
height:auto
}
<div class="restrict-container" style="width:300px;border:1px solid red">
<img width="400" height="300" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAIAAABi1XKVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAf3SURBVHhe7do9chtXGoVhb2Ryr0TllSjwLpx7CY6cusrZBFqAEydegPMpL2NEUhTvPf0HkBBxDDxVT6BXajbB7r5fNyB+992v/wD8O2QD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxsu5ref/vef/z775Y8/FxvAmbLhYgwsLi377n3/x4evayxW2g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OU5WtvmURzJaZSs7WQ5a0rPxU3Ivj/LgbVv4ylseQlueVkkW9/6aQUu/vXxC3MxvCzXHIvH1pbrG5z/Ar4czAseh3PP5hfzMD3vB1l7OLr6ubhZ2ffn6yX+efEcXe4Ht8HD1TIPu9V7/qPHNbDc28OaXCzUacHkw+COi6+Q7R9nx8OkuPBx2D8LG2d5cWaX+9+yNrAeXPNc3K7sO/Tlwnq5aNZuj+dcUmvX+tfnoNHW0nrFk8XsYAR8Iw1PWM+WR+BlJK18u623qyun8mk/wwP1wSG9zrm4WdnAsdMHFheVDRwzsK4kGzgyvvldfbPPt5IN7Jo+qjv8XQ0uKxt4tPVfAQO/PPXusoEHh79YZ1pdQzbwaOe3NHxudTXZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgE6/frP/wEiNOVSBSWikgAAAABJRU5ErkJggg=="/>
Some text<br>
<img width="400" height="300" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsCAIAAABi1XKVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsM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AQO/PPXusoEHh79YZ1pdQzbwaOe3NHxudTXZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgFqZQPUygaolQ1QKxugVjZArWyAWtkAtbIBamUD1MoGqJUNUCsboFY2QK1sgFrZALWyAWplA9TKBqiVDVArG6BWNkCtbIBa2QC1sgE6/frP/wEiNOVSBSWikgAAAABJRU5ErkJggg=="/>
Some text
</div>
The mistake from answer from october 2013: the image should be placed absolute (position:absolute
) to the wrapped container but it is not so placed.
从2013年10月回答的错误:图像应该被放置在绝对的位置(位置:绝对)到包裹的容器,但它不是那么放置。
This is the end of my answer to this question.
这是我对这个问题的答案的结束。
For further information read more about:
欲了解更多信息,请阅读更多:
- What could we do with the new HTML5 technology for responsive images too? (this is previous extended version of my posting here. See there the second part).
- 我们还可以使用新的HTML5技术来处理响应式图像吗? (这是我在这里发布的先前扩展版本。请参见第二部分)。
#3
2
For a css only solution, you can wrap the img
in a container where the padding-bottom
percentage reserves space on the page until the image loads, preventing reflow.
对于仅css解决方案,您可以将img包装在容器中,其中填充底部百分比在页面上保留空间直到图像加载,从而防止重排。
Unfortunately, this approach does require you to include the image aspect ratio in your css (but no need for inline styles) by calculating (or letting css calculate for you) the padding-bottom
percentage based on the image height and width.
不幸的是,这种方法确实要求您在css中包含图像宽高比(但不需要内联样式),方法是根据图像的高度和宽度计算(或让css计算)填充底部百分比。
If many of your images can be grouped into a few standard aspect ratios, then you could create a class for each aspect ratio to apply the appropriate padding-bottom
percentage to all images with that aspect ratio. This may save you a little time and effort if you are not dealing with a wide variety of image aspect ratios.
如果您的许多图像可以分组为几个标准宽高比,那么您可以为每个宽高比创建一个类,以将适当的填充底部百分比应用于具有该宽高比的所有图像。如果您不处理各种图像宽高比,这可能会节省您一点时间和精力。
Following is some example html and css for an image with a 2:1 aspect ratio:
以下是具有2:1宽高比的图像的一些示例html和css:
HTML
HTML
<div class="container">
<img id="image" src="https://via.placeholder.com/300x150" />
</div>
CSS
CSS
.container {
display: block;
position: relative;
padding-bottom: 50%; /* calc(100%/(300/150)); */
height: 0;
}
.container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
The snippet below adds some extra html, css and javascript to create some visual top and bottom reference points and mimic a very slow loading image so you can visually see how the reflow is prevented with this approach.
下面的代码片段添加了一些额外的html,css和javascript来创建一些可视的顶部和底部参考点,并模拟一个非常慢的加载图像,这样您就可以直观地看到这种方法如何防止回流。
const image = document.getElementById('image');
const source = 'https://via.placeholder.com/300x150';
const changeSource = () => image.src = source;
setTimeout(changeSource, 3000);
.container {
display: block;
position: relative;
padding-bottom: 50%; /* calc(100%/(300/150)); */
height: 0;
}
.container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.top, .bottom {
background-color: green;
width: 100%;
height: 20px;
}
<div class="top"></div>
<div class="container">
<img id="image" src="" />
</div>
<div class="bottom"></div>
#4
2
From this blog post by Jonathan Hollin: add the image's height and width as part of an inline style. This reserves space for the image, preventing reflow when the image loads, but it's also responsive.
在Jonathan Hollin的博客文章中:添加图像的高度和宽度作为内联样式的一部分。这为图像保留了空间,防止了图像加载时的回流,但它也具有响应性。
HTML
HTML
<figure style="padding-bottom: calc((400/600)*100%)">
<img src="/images/kitten.jpg" />
</figure>
CSS
CSS
figure {
position: relative;
}
img {
max-width: 100%;
position: absolute;
}
The figure
can be replaced with a div
or any other container of your choice. This solution relies on CSS calc() which has pretty wide browser support.
该图可以替换为div或您选择的任何其他容器。这个解决方案依赖于CSS calc(),它具有相当广泛的浏览器支持。
Working Codepen can be seen here.
在这里可以看到工作Codepen。
UPDATE:
更新:
I found a much cleverer alternate version of this: http://cssmojo.com/aspect-ratio-using-custom-properties-and-calc/. This still requires a wrapper element and it requires CSS custom properties, but I think it's much more elegant. Codepen example is here (credit to Chris Coyier's original).
我找到了一个更聪明的替代版本:http://cssmojo.com/aspect-ratio-using-custom-properties-and-calc/。这仍然需要一个包装元素,它需要CSS自定义属性,但我认为它更优雅。 Codepen的例子在这里(归功于Chris Coyier的原创)。
#5
0
If I understand the requirements ok, you want to be able to set an image size, where this size is known only on content (HTML) generation, so it can be set as inline styles.
如果我理解了这些要求,那么您希望能够设置图像大小,此大小仅在内容(HTML)生成时已知,因此可以将其设置为内联样式。
But this has to be independent of the CSS, and also prior to image loading, so also independent from this image sizes.
但这必须独立于CSS,并且在图像加载之前,因此也独立于此图像大小。
I have come to a solution tha involves wrapping the image in a div, and including in this div an svg that can be set to have proportions directly as an inline style.
我已经找到了一个解决方案,涉及将图像包装在div中,并在此div中包含一个svg,可以将其设置为直接作为内联样式的比例。
Obviously this is not much semantic, but at least it works
显然这不是很多语义,但至少它是有效的
The containing div has a class named img to show that it , well, should be an img
包含div有一个名为img的类,以表明它应该是一个img
To try to reproduce the loading stage, the images have a broken src
为了尝试重现加载阶段,图像具有破坏的src
.container {
margin: 10px;
border: solid 1px black;
width: 200px;
height: 400px;
position: relative;
}
.img {
border: solid 1px red;
width: fit-content;
max-width: 100%;
position: relative;
}
svg {
max-width: 100%;
background-color: lightgreen;
opacity: 0.1;
}
#ct2 {
width: 500px;
}
.img img {
position: absolute;
width: 100%;
height: 100%;
max-height: 100%;
max-width: 100%;
top: 0px;
left: 0px;
box-shadow: inset 0px 0px 10px blue;
}
<div class="container" id="ct1">
<div class="img">
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 400 300" width="400">
</svg>
<img width="400" height="300" src="missing.jpg">
</div>
</div>
<div class="container" id="ct2">
<div class="img">
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMinYMin meet" viewBox="0 0 40 30" width="400">
</svg>
<img width="400" height="300" src="missing.jpg">
</div>
</div>