lyosha wrote:I didn't know this either, but it is completely logical.
I know now why I hadn't heard of it before - because Firefox doesn't support it yet, although it is actively being looked at, and should be there in the time of a few releases. Opera/Web-kit do have support.
lyosha wrote:One question though: how can you fall back to a raster format using this method?
Yeah, this is the stumbling block. The fall-back mechanism is one of the great features of the
<object> tag (although even that doesn't work properly in all browsers. Links2 (a text-based browser) for example doesn't show the fall-back for PNG images, but does, strangely enough, for SVG.
Anyway, after some testing with SVG in the
<img> element, I've found that the
alt attribute works as expected in Firefox and Links2, giving a text fall-back - great. Internet Explorer shows an [
X] with the
alt text inside - not so.
Nesting
<img> elements however
does work! It's not standards-compliant of course, although I believe the HTML5 WG are looking at allowing content inside the element.
So, using nested tags, you could do :
- Code: Select all
<img src="SVGimage.svg" alt="SVG alt text">
<img class="PNG" src="PNGimage.png" alt="PNG alt text"/>
</img>
With a conditional CSS rule for IE, and one for everything else :
- Code: Select all
<style type="text/css">.PNG {display:none;}</style>
<!--[if IE]><style type="text/css">img {display:none;} .png {display:inline;}</style><![endif]-->
This means that firstly the inner PNG is hidden on every browser.
Then, if the browser is IE, the outer
<img> element is hidden. The inner content is still displayed though (thankfully!) even when the containing element is hidden - probably because it's a non-standard use of the element. Finally, the fall-back inner PNG is displayed again.
Sadly (but in-line with the spec) CSS child selectors don't work on
<img>, meaning we can't just do :
- Code: Select all
<style type="text/css">img:first-child {display:none;}</style>
So it has to be done by means of a class on the inner fall-back element.
The only problem with this method, is that a text-based browser (or any browser without CSS enabled for that matter) will display the content of the
alt attribute of
both <img>'s. Not much that can be done about that.
Example page showing the above method