Fallback background-image if default doesn't exist
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'.