Posts Tagged ‘inline’

Inline SVG Fallback

Monday, March 2nd, 2009

As you may already know, IE doesn’t support SVG unless you install a plugin like the ASV, that is unfortunately not supported anymore by Adobe.

As a web developer willing to use inline SVG, your best bet is thus to provide fallback mechanisms to display IE visitors at least a degraded version of what you’re trying to express/demonstrate, and teach them what they’re missing by sticking with evil IE.

Luckily, IE will simply ignore any markup it does’nt know, so you usually don’t need to bother masking or removing the SVG markup when serving pages to your IE visitors.

This is however not true if you insert plain text if your SVG … Let’s take an example.

When you insert the following SVG in your page:

<svg xmlns="http://www.w3.org/2000/svg"
       version="1.1" wiewBox="0 0 200 100">
   <text x="0" y="0"
         font-family="Verdana"
	 font-size="55" fill="blue" >
        Hello, out there
   </text>
</svg>

IE will ignore all SVG markup and formatting, and actually display the plain text with the current style:

Hello, out there

Now, what happens when you insert HTML markup inside your SVG using the foreignObject tag ?

<svg xmlns="http://www.w3.org/2000/svg"
       version="1.1" wiewBox="0 0 200 100">
   <foreignObject width="200" height="100">
      <span style="color:blue;">
        Hello, out there
      </span>
   </foreignObject>
</svg>

Well, IE ignores all SVG and displays only the HTML markup embedded in the foreignObject tag:

Hello, out there

Based on this behavior, the guys at Sheperd interactive have imagined a very elegant inline SVG fallback mechanism that uses SVG conditional processing.

In a nutshell, the actual SVG and the IE fallback are embedded in an SVG switch:

  • SVG aware browsers will recognize the conditional directive, and render the first alternative they understand (the actual SVG), ignoring the other (the IE fallback)
  • IE will silently ignore all SVG markup (including the conditional directive), and go straight to the fallback written in HTML.

In the example, below, SVG-aware browsers will display a red circle, and IE a simple text.

<svg xmlns="http://www.w3.org/2000/svg"
        version="1.1" wiewBox="0 0 200 100">
  <switch>
     <circle cx="150px" cy="100px" r="50px"
               fill="#ff0000" stroke="#000000"
               stroke-width="5px"/>
     <foreignObject width="200" height="100">
        <span style="color:red;">
          This is not a red circle
        </span>
     </foreignObject>
   </switch>
</svg>

See the actual rendering below:

This is not a red circle

SVG inline in XHTML

Sunday, February 1st, 2009

SVG is now supported by all web browsers except IE. Opera has the best SVG support, but Safari and Firefox are not far behind. I think it is now time to take advantage of the powerful SVG features.

What does SVG add to the web developer toolkit ?

Well, as its names indicates, SVG first provides a simple syntax to draw shapes that scale: it is thus particularly handy to draw screen layouts and buttons. I see some people starting to get interested …

But wait: SVG also includes a wide range of gradient, opacity and geometric transformations that can be applied not only to native SVG shapes, but also to images. This give you access to many fancy effects very hard to achieve with HTML alone, like for instance image reflection or fading.

Want more ? Check this out: SVG markup can be modified dynamically using javascript, allowing you to actually animate it. Who said cover-flow at the back of the class ?

Animation through javascript sucks on low-end devices ? Well, SVG also includes native animation capabilities (unfortunately only supported today by Opera).

Fair enough, but the whole web is built on top of HTML, and switching to full SVG is definitely an option.

Well, the cherry on the cake is that you don’t have to: SVG can be inserted inline into XHTML. How powerful that is ?

You will find detailed information on why and how it works on the SVG wiki. A method to have inline SVG supported by IE using the Adobe SVG viewer is also provided.

Here is an overview of what you need to do.

  • First, you need to use XHTML, because plain old HTML doesn’t support being extended by another markup language, and your SVG markup will be ignored by the browsers.
  • Second, you need to make sure that the SVG-aware browsers know this is XHTML, by delivering your content to them as application/xhtml+xml. You probably nevertheless still want to deliver your content to IE as text/html, so what you need is conditional mime-types based on the Accept header of the user agent.
    • it can be done at the HTTP server level. See example below for apache (example taken from the SVG wiki):
      AddType text/html .xhtml
      RewriteEngine on
      RewriteBase /
      RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
      RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
      RewriteCond %{REQUEST_URI} \.xhtml$
      RewriteCond %{THE_REQUEST} HTTP/1\.1
      RewriteRule .* - [T=application/xhtml+xml]
      
    • If you don’t have access to your web server configuration, you can still:
      • modify headers on the fly. See an example for PHP below:
        if(preg_match("|application/xhtml\+xml|i",$_SERVER["HTTP_ACCEPT"]){
          header("Content-Type: application/xhtml+xml");
        }
        
      • or insert meta tags in the HTML head to specify the proper mime-type. See another example for PHP:
        if(preg_match("|application/xhtml\+xml|i",$_SERVER["HTTP_ACCEPT"]){
          echo "<meta http-equiv='Content-Type' content='application/xhtml+xml' />" ;
        }
        
      • If you use WordPress, the simpliest way is to use the Content-Negotiation plugin
  • Third, put your SVG under an SVG tag
  • <div id="myDIV">
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
    	 xmlns:xlink="http://www.w3.org/1999/xlink"
    	 viewBox="0 0 400 50"
    	 id="myDIVBG">
    	    <defs>
    	      <linearGradient id="myGrad">
    		<stop style="stop-color:#ff5b06;"
                            offset="0" />
    		<stop style="stop-color:#ffb11a;"
                            offset="1" />
    	      </linearGradient>
    	    </defs>
    	    <rect style="fill:url(#myGrad);"
    	       width="400" height="50"
                   x="0" y="0" rx="10" ry="10" />
        </svg>
        HTML Text with SVG background
    </div>
    
  • Fourth, style your SVG with CSS
  • <style type="text/css">
    #myDIV {
        position:relative;
        width:400px;
        height:50px;
    }
    #myDIV svg {
        position:absolute;
    }
    #myDIV svg:hover rect {
        fill: red !important;
    }
    #myDIV span {
        position:absolute;
        top:15px;
        left:40px;
        font-size: 16px;
        font-weight: bold;
    }
    </style>
    

The result should look like this:

HTML Text with SVG background

Please note in particular how the CSS styling allows the SVG to be perfectly integrated in the page:

  • the absolute positioning of the svg root element allows the next-coming HTML span to appear on top of the SVG background
  • the :hover behaviour allows a dynamic update of the SVG rect element fill attribute when the element is moused over

You will find more examples below:


download free edition adobe photoshopfree download hustler magazine in adobe format Kopen oem software download adobe after effectsadobe svg veiwer downloaddownload adobe acrobat for freeppd download adobe Windows 7 adobe flash player active x downloadadobe reader kostenloser download download Adobe software