Fallback image with CSS

Kamafeather picture Kamafeather · Nov 27, 2015 · Viewed 20.3k times · Source

I have an <img> that shows a remote image. I want it to fallback to another local image, in the case where the remote one is not reachable.

<img class="cc_image fallback" src="http://www.iconarchive.com/download/i82888/limav/flat-gradient-social/Creative-Commons.ico"> 

.cc_image {
    width: 256px;
    height: 256px;
}

.cc_image.fallback {
    /* this URL here is theoretically a local one (always reachable) */
    background-image: url('https://cdn2.iconfinder.com/data/icons/picons-basic-3/57/basic3-010_creative_commons-256.png');
}

It works so that when the src image is not found then the background image will be shown.

The drawbacks are:

  • it will always load the background image (additional HTTP request)
  • it shows a little not-found-icon (a question mark on Safari) at the place of te original image, that is displayed above the background-image (not a big issue, but I'd like to get rid of it)

How could I solve these issues? Or: are there other technics to achieve the same result?

I found this question but the given solutions rely on Javascript or on <object> (that seems to not work on Chrome). I would like a pure CSS/HTML solution, without Javascript if possible.

I know about the multiple background-image but am not sure whether it is a good option (browser support? and will it fallback with an unreachable image?). Or I was thinking about embedding a SVG image as data-uri.

Suggestions for the most flexible (and compatible) method?

Answer

beerwin picture beerwin · Nov 27, 2015

Unfortunately, you can't achieve both without Javascript or object tag.

You could do this to avoid the missing image icon:

Place your image in a container (it might already be in one). Make the container have the same width and height as the image.

  1. Set the fallback image as the background image of the container.
  2. Set the remote image as the background image of your img tag.
  3. Load an 1x1 pixel transparent png as the src of your image (see code for how that can be done without an extra HTTP request).

Code:

HTML

<!-- you could use any other tag, such as span or a instead of div, see css below -->
<div class="cc_image_container fallback">
    <img class="cc_image" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" style="background-image: url(*your remote image*)"/>
</div>

CSS

.fallback {
    background-image: url(*fallback image here*);
    display: inline-block; /*to ensure it wraps correctly around the image, even if it is a a or span tag*/
    min-width: specify a minimum width (could be the width of the fallback image) px; 
    min-height: specify a minimum height (could be the height of the fallback image) px;
    background-position: center center; // fallback for older browsers
    background-size: cover;
    background-repeat: no-repeat;
}

.cc_image {
    min-width: same as container px;
    min-height: same as container px;
    background-position: center center; // fallback for older browsers
    background-size: cover;
    background-repeat: no-repeat;
}
  • min-width and max-width make sure that the background images remain visible.
  • background-position makes sure that the central part of the images remains visible and is a graceful degradation for older browsers
  • background-size resizes the background image to fill the element background. The cover value means that the image will be resized so it will completely cover the element (some of the outer edges of the image may be cropped)
  • The base64 data in the img src tag is a transparent 1px png.
  • This will have an additional benefit that regular users and some bots may not be able to save your images (a rudimentary image protection)
  • The only drawback is, that you will still have one extra HTTP request for the fallback image.