Is it possible to replace the standard broken image via CSS or using another technique? All my images are the same size and my have transparency.
I've tried to wrap all images with a div's background:
<div class="no_broken"> <img src="http://www.web.com/found.gif"/> </div> <div class="no_broken"> <img src="http://www.web.com/notfound.gif"/> </div>
CSS:
div.no_broken { background-image: url(standard.gif); } div.no_broken, div.no_broken img { width: 32px; height: 32px; display: block; }
But this will display two images on top of each other if the IMG is transparent.
Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.
To keep your background fixed, scroll, or local in CSS, we have to use the background-attachment property. Background-attachment: This property is used in CSS to set a background image as fixed or scroll. The default value of this property is scroll.
You can't change it with CSS, you need Javascript for that. But if you are trying to change the image on for example hover, you could remove the img-tags and use a div instead on which you set a background-image. In your CSS you can then create a block where you change the image on hover.
This works without CSS:
<img src="some.jpg" onerror="this.src='alternative.jpg';">
It seems to even work when Javascript is disabled.
Yes, you can replace an image with CSS, using a trick of pseudo selectors.
Basically, ::before
and ::after
only apply when the image fails to load. You can use absolute positioning to place the CSS pseudo element over the broken image placeholder:
img { position: relative; width: 200px; height: 200px; display: inline-block; vertical-align: text-bottom; } img::before { content: 'fall back'; position: absolute; top: 0; left: 0; bottom: 0; right: 0; line-height: 200px; background-color: #fd0; color: currentColor; text-align: center; border-radius: 2px; display: block; width: 200px; height: 200px; overflow: hidden; }
<img src="not-found"> <img src="https://via.placeholder.com/200">
This snippet works with no JS at all, but only works in newer browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With