英文:
Remove the break icon in the img element without deleting that element
问题
我试图在目标网址不存在时,不删除img元素的情况下删除img元素中的损坏图标:
HTML:
<img src="error.png">
CSS:
img {
width: 100px;
height: 100px;
background-image: url(url.png);
background-size: cover;
}
我希望不删除img元素,而是显示没有损坏图标的背景图像。
我希望有人能帮助我,我真的需要这个。
英文:
I tried to remove the broken icon in the img element when the destination url doesn't exist, without deleting the img element :
HTML:
<img src="error.png">
CSS:
img {
width: 100px;
height: 100px;
background-image: url(url.png);
background-size: cover;
}
Instead of deleting the img element, I prefer to bring up the background-img without a broken icon.
I hope someone help me, I really need this.
答案1
得分: 1
根据我在Stack Overflow上找到的答案,尝试使用img:after
与content
属性结合使用。
这里您可以找到工作示例:https://codepen.io/filipalbert/pen/QWZxPMp
英文:
According to the answer I found on Stack Overflow, try using img:after
in combination with the content
property.
https://stackoverflow.com/questions/22051573/how-to-hide-image-broken-icon-using-only-css-html#answer-37192970
img:after {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
background-image: url(url.png);
content: '';
}
Here you can find the working example: https://codepen.io/filipalbert/pen/QWZxPMp
答案2
得分: 0
你可以使用 ::before
和 ::after
来实现这个效果。
类似这样:
img {
position: relative;
width: 200px;
height: 200px;
}
img::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url(https://via.placeholder.com/200);
background-size: cover;
}
<img src="wrong_link">
<img src="https://via.placeholder.com/200">
英文:
You can use ::before
and ::after
for this.
Something like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
img {
position: relative;
width: 200px;
height: 200px;
}
img::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url(https://via.placeholder.com/200);
background-size: cover;
}
<!-- language: lang-html -->
<img src="wrong_link">
<img src="https://via.placeholder.com/200">
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论