Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fallback background-image if default doesn't exist

People also ask

What is a fallback background?

Fallback color is used to specify the color for a situation when the browser doesn't support RGBA colors. Some browsers that doesn't support fallback colors are opera 9 and below, IE 8 and below etc.

How do I change the fallback image in CSS?

Place your image in a container (it might already be in one). Make the container have the same width and height as the image. Set the fallback image as the background image of the container. Set the remote image as the background image of your img tag.

What is fallback image?

By adding a fallback image, you can set a branded image to be used when no post thumbnail is found. This allows you to make sure that all your articles have a post thumbnail. Another way to deal with this problem is using the Require Featured Image plugin.

Why background image is not working in Div?

You need to make sure that it is showing up first. You may have an error in your code where you do not have the same amount of opening div tags as you do closing div tags, thus it won't actually display your centerBar2 div. Are you sure your image is in the right directory, properly named, etc.?


You can use multiple backgrounds if there is no transparency involved and they occupy all available space or have the same size:

div{   
     background-image: url('http://placehold.it/1000x1000'), url('http://placehold.it/500x500');
     background-repeat:no-repeat;
     background-size: 100%;
     height:200px;
     width:200px;
}
<div></div>

If the first doesn't exit, the second will be displayed.

div{   
     background-image: url('http://placehol/1000x1000'), url('http://placehold.it/500x500');
     background-repeat:no-repeat;
     background-size: 100%;
     height:200px;
     width:200px;
}
<div></div>

To specify multiple possible backgrounds, you could do:

  background-color: green;
  background-image: url('bg.png'), url('bg.jpg');

This will set the background to bg.png if it exists. If it doesn't exist, it will be set to bg.jpg. If none of those images exist, the background will be set to the static green color.

Note that it will prioritize whatever image is specified first. So if both images exist, it will choose the bg.png over the bg.jpg.

Check out the demo here. Test it by breaking any of the image urls'.


Although this solution does involve JS, it will download the first image using CSS, and will only use JS to download the fallback image if the main image failed to download.

First, set the main image with CSS as usual, for example:

.myImage {
    background-image = "main-image.png";
}

And add the following JS that will only act if loading the main image fails (otherwise, fallback image will never be downloaded).

var img = document.createElement("img");
img.onerror = function() {
    var style = document.createElement('style');
    style.innerHTML = '.myImage { background-image = url("http://fallback_image.png"); }';
    document.head.appendChild(style);
}
img.src = "main_image.png";

Note that you can add multiple rules to the CSS block added with javascript, for example if you need to do this with multiple elements.