Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional code for displaying PNG in place of SVG for older browsers?

I'm looking for a way to have older browsers display a PNG image in place of an SVG as a fallback when detected. The logo for my site is currently in SVG but older browsers, specifically IE 8 and below won't render it. I already have the logo in PNG. What's the best way to execute this?

Thanks

like image 424
Charlie Avatar asked Jan 20 '12 18:01

Charlie


People also ask

Can I use PNG instead of SVG?

PNGs and SVGs support transparency — so they're both excellent choices for online logos and graphics. It's worth noting that PNGs are one of the best choices for a raster-based transparent file. If you're working with pixels and transparency, PNGs are a better option than SVGs.

How does old browser support SVG?

Include the Modernizer javascript in the head and make sure it includes SVG detection. When adding javascript in the head, type="application/javascript" will not work in older browsers so when including javascript use type="text/javascript" or don't use the type attribute at all.

Can you use SVG as IMG SRC?

To embed an SVG via an <img> element, you just need to reference it in the src attribute as you'd expect. You will need a height or a width attribute (or both if your SVG has no inherent aspect ratio). If you have not already done so, please read Images in HTML.


2 Answers

Use HTML conditional comments.

<!--[if lte IE 8]><img src="logo.png" /><![endif]-->
<!--[if gt IE 8]><img src="logo.svg" /><![endif]-->
<!--[if !IE]> --><img src="logo.svg" /><!-- <![endif]-->

http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx

If you're also looking for a way to handle this for browsers other than IE, you should check the user agent with javascript or php.

like image 113
Rick Kuipers Avatar answered Sep 28 '22 10:09

Rick Kuipers


<object type="image/svg+xml" data="image.svg">
    <img src="image.png" alt="image"/>
</object>
like image 40
Spadar Shut Avatar answered Sep 28 '22 09:09

Spadar Shut