Display alternate image

前端 未结 4 819
遇见更好的自我
遇见更好的自我 2021-01-04 01:02

Is it possible to show an alternate image if the original source file is not found? I would like to achieve this only with css and html, no javascript (or jQuery and alike).

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 01:42

    You can do this using the CSS background-image property of the img element, i.e.

    img
    {
    background-image:url('default.png');
    }
    

    However, you have to give a width or height for this to work (when the img-src is not found):

    img
    {
    background-image:url('default.png');
    width:400px;
    }
    

提交回复
热议问题