Fallback background-image if default doesn't exist

后端 未结 5 715
忘掉有多难
忘掉有多难 2020-11-29 09:43

I want to set an image as a background, however the image name might be either bg.png or bg.jpg.

Is there any non-javascript way to create

5条回答
  •  無奈伤痛
    2020-11-29 10:24

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

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

提交回复
热议问题