Remove the break icon in the img element without deleting that element.

huangapple go评论58阅读模式
英文:

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:

&lt;img src=&quot;error.png&quot;&gt;

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:aftercontent属性结合使用。

这里您可以找到工作示例: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: &#39;&#39;;
}

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 -->

&lt;img src=&quot;wrong_link&quot;&gt;
&lt;img src=&quot;https://via.placeholder.com/200&quot;&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 19:45:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定