onerror event using background: url()

前端 未结 3 756
自闭症患者
自闭症患者 2020-12-23 20:46

Is there a way to show alternate image if source image is not found? I know to accomplish this is by doing something like below:



        
3条回答
  •  佛祖请我去吃肉
    2020-12-23 21:28

    In case myimage.gif isn't transparent, you could use multiple backgrounds:

    background: url('myimage.gif'), url('fallback.gif');
    

    This way fallback.gif will only be visible if myimage.gif isn't available.

    Note that fallback.gif may be downloaded even if myimage.gif is available.


    Alternatively, even though not widely supported, CSS Images 3 introduces the image() notation:

    background: image('myimage.gif', 'fallback.gif');
    

    Multiple s can be given separated by commas, in which case the function represents the first image that's not an invalid image.

提交回复
热议问题