显示替代图像 [英] Display alternate image

查看:73
本文介绍了显示替代图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果未找到原始源文件,是否可以显示替代图像?
我想实现这个只有css和html,没有javascript(或jQuery和类似)。
这个想法是仍然显示一个图像,而不是alt测试或IE的默认(丑陋)十字。
如果不可能没有javascript我将然后宁愿检查img src与php与一个基本的if-then-else。

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). The idea is to still show an image instead of the "alt" test or default (ugly) cross of IE. If not possible without javascript I will then rather check the img src with php with a basic if-then-else.

推荐答案

您可以使用img元素的CSS background-image属性,即

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

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

但是,你必须给出一个宽度或高度才能工作未找到img-src):

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

这篇关于显示替代图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆