如何强制 占用宽度,即使图像未加载
我正在加载一堆 img,即使它们尚未加载或尚未完成加载,我也希望它们占用文档上的空间。
我尝试指定宽度和高度(既作为属性本身,又在样式属性内),但令人沮丧的是,如果图像不加载,图像将不会占用空间。
当然,必须有一种方法可以强制图像达到特定尺寸,**即使该图像无法加载。
谢谢
I'm loading a bunch of img's, and I'd like them to take up space on the document even if they are not loaded, or have not completed loading yet.
I tried specifying width and height (both as attributes themselves, and within a style attribute), and find it frustrating that the images will not take up space if they don't load.
Surely, there must be a way to force an img to specific dimensions, **even if that image fails to load.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然还有:
此外,在
style="/* css */"
属性中指定宽度和高度不起作用的原因是因为默认情况下图像是内联元素。如果您指定了display: block
,则图像将接受宽度
和高度
值。如果添加到 css/style:
它应该可以工作。我不确定为什么 Firefox 不尊重宽度/高度属性,但仍然如此。即使是具有定义的
doctype
的 IE 也应该遵循display: inline-block
,因为img
元素默认情况下都是内联的。There certainly is:
Also, the reason specifying the width and height within the
style="/* css */"
attribute didn't work is because images are, by default, in-line elements. Had you specifieddisplay: block
the image would've accepted thewidth
andheight
values.If you add, to the css/style:
It should work. I'm not sure why Firefox doesn't respect the width/height attributes, but still. Even IE, with a defined
doctype
should respectdisplay: inline-block
, sinceimg
elements are, by default, in-line anyway.简单的答案:将图像包装在具有固定宽度的
中。
如果将 padding、border 等设置为
0
,您将不会注意到 div 在那里。The simple answer: wrap the image in a
<div>
with a fixed width.If you set padding, border etc to
0
, you won't notice that the div is there.