tags:

views:

141

answers:

3

I am having issues embedding SVG into a webpage. I have found the simplest method to just use an image tag. For example:

<img src="my_graphic.svg" height="100"/>

Works in web-kit. I do not need to explicitly set the width and the browser will maintain the aspect ratio. Very nice!

This doesn't work in Firefox though - it's not cross browser. So how about embedding as an object?

<object type="image/svg+xml" 
        height="100"
        width="554"
        data="my_graphic.svgz">
        <span/></object>

This time I'm using svgz and the mime type has been added and voila! It works in both firefox and webkit. However, in webkit I need to explicitly state the width or we get some nasty containing element scrollbars. But what's worse is the background is no longer transparent. It's rendered with a white background.

So one method works perfectly in webkit. The other works perfectly in mozilla. What can I do to get it working reliably in both?

Interested in a demonstration of this? See my link for reference: http://sumocreations.com/demo/svg/new_dttg.html

A: 

Place an <img> tag inside your object.

The object will render in firefox, and webkit should show the <img> tag.

Edit:

Also, what's up with the self closed span tag? <span> does not support self closing.

Chris Sobolewski
I don't think that will work as WebKit supports the <object> tag. Though it has a transparency bug as noted by @heycam.
Alexandre Jasmin
I have confirmed this will not render transparently but it is more concise. The self closing span was from a code sample I found elsewhere.
Jim Jeffers
+2  A: 

I don't believe it's currently possible for the <object> to have a transparent background in WebKit. There's a bug filed for this problem. I don't know of a workaround.

heycam
I have found a work around. Unfortunately, you must specify a single dimension of an img using an svg source. If it is greater than zero the browser (webkit) was able to render the object and we can hide the embed object. If not we can toggle the alternate as visible. I hope there is a cleaner way to do this in the future. For now this will have to do.
Jim Jeffers
There's also a [mozilla bug](https://bugzilla.mozilla.org/show_bug.cgi?id=276431) for supporting SVG with the <img> tag
Alexandre Jasmin
+1  A: 

The only work around I found was by detecting whether an img utilizing an SVG source is rendered properly. I do this by only specifying one dimension. Either the height or the width but not both. I can then test if the alternate dimension is greater than zero. If it is I hide the the object. If not I hide the image. Here is how to do it with jQuery:

<script type="text/javascript">
    $(document).ready( function() { 
        if($('img.logo').width() < 1) {
        $('img.logo').hide(); $('object.logo').show();
    } else {
         $('img.logo').show(); $('object.logo').hide();
        }
     });
</script>

See the demonstration: http://sumocreations.com/demo/svg/new_dttg.html

Jim Jeffers
I've created a jQuery plugin to automate this:http://github.com/jimjeffers/jQuery-SVG-Image-Plugin/and some quick documentation is available here:http://jimjeffers.github.com/jQuery-SVG-Image-Plugin/
Jim Jeffers