• HTML5 and SVG

    diades.net - SVG in HTML5

    Prev — || — Next

    Displaying an SVG image within an HTML5 document

    Scalable Vector Graphics, generally referred to as SVG images, are typically produced in drawing programs such as Inkscape. But because they're defined in XML files, and so can also be created and edited in a text editor, they have a number of advantages over other image formats: they can be searched, indexed, and scripted, and yet, like other formats, can be compressed.

    SVG images can be displayed via the <img>, <embed>, and <object> elements. The <embed> tag, which allows for scripting, is deprecated in HTML4 and XHTML but allowed in HTML5, while <object> does not allow for scripting and is supported in all versions of HTML.

    Shown below is an image created as a file (my_image.svg) and displayed via the <img> element. This approach greatly limits the degree of interactivity a viewer can have with it.

    An svg file displayed via the img element:

    You can avoid this confusion about language version support and these limitations on interactivity by writing images directly to the page using SVG markup placed within the document's body. (Internet Explorer 8 does not support SVG code and requires a plug-in for viewing.) Any text it contains can be searched and indexed, and all or any part of it can be manipulated via script.

    Shown below is the same image written directly to the page. Try selecting the "SVG" text in both — you'll find that only the second one allows it.

    The same svg directly written to the page:

    SVG

    Notice that the image has been reduced in size without the jagged edges or blurriness often associated with resizing images created in other pixel-based formats. SVG images have this scalability because they're vector-based, defined mathematically by relationships between points and the paths connecting them. For this reason, the SVG format is generally not suitable for photographs that typically do not contain well-defined shapes and curves.

    Scalability is particularly important for users accessing pages on devices with small or large screens.

    <!DOCTYPE html> <html lang="en"> <head> <title>SVG in HTML5</title> <meta http-equiv="X-UA-Compatible" content="IE=9" /> <meta<link rel='text/css' href="./kp/art.css"> </head> <body id="main-content"> <section id=''> <article> <h3>An svg file shown via the img element</h1> <p>This svg img is written as a file (svgimg.svg) and the file placed into the page via an <img> element.</p> <pre>&lt;img src="./kp/svgimg.svg" class="svgimg" title="An svg image" /&gt;</pre> <p class='callout'>This means that, whilst it still retains the quality of the svg image, it acts as an image and the user can have no interactivity with it unless it is in the same manner as any other image.</p> <img src="./kp/svgimg.svg" class="svgimg" title="An svg image" /> </article> <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" version="1.1" width="469.69101" height="410.1405" id="svg2"> <defs id="defs4"> <linearGradient id="linearGradient3590"> <stop id="stop3592" style="stop-color:#f91818;stop-opacity:1" offset="0" /> <stop id="stop3594" style="stop-color:#f91818;stop-opacity:0" offset="1" /> </linearGradient> </defs> <g transform="translate(145.80641,174.82868)" id="layer1"> <g id="g5811"> <rect width="127.52179" height="129.34354" x="13.66305" y="14.881308" id="rect2816" style="opacity:0.5;fill:url(#linearGradient5818);fill-opacity:1;stroke:none" /> <path d="m 246.84576,148.77919 a 82.889168,84.710907 0 1 1 -165.778339,0 82.889168,84.710907 0 1 1 165.778339,0 z" transform="translate(-138.45223,1.8217403)" id="path3610" style="opacity:0.59734509; fill:url(#oldpaint_bitmap);fill-opacity:1;stroke:none" /> <path d="m 10.019569,14.881308 c 3.196874,5.02365 -5.1945555,6.787813 -8.349637,5.313413 C -6.8801438,16.19919 -5.7659725, 4.357921 -0.60725799,-1.8179671 8.620472,-12.865182 25.705262,-10.602981 35.068482, -1.0589335 48.809383,12.94734 45.277226,35.883933 31.273224,48.279857 12.608092, 64.801674 -16.35652,59.946206 -31.728617,41.448376 -51.072833,18.170745 -44.870515, -16.895589 -21.860913,-35.216517 6.0067989,-57.405614 47.212755,-49.844431 68.467031, -22.312588 93.514459,10.132736 84.587548,57.500446 52.526879,81.678407 15.511568, 109.59282 -38.031973,99.295918 -65.127167,62.702031 -95.914449,21.121731 -84.244741, -38.607164 -43.114568,-68.615066 3.0272713,-102.2794 68.948271,-89.234928 101.86558, -43.566243 138.41003,7.1346538 123.98939,79.252719 73.780534,115.07696" transform="matrix(1.6368518,0,0,1.8056782,-3.1865971,-9.2745571)" id="path5151" style="fill:none;stroke:#1247bc;stroke-width:8.72501945;stroke-linecap:round;stroke-linejoin:miter; stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;marker-end:none" /> <text x="190.37181" y="54.048714" id="text5803" xml:space="preserve" style="font-size:144px;font-style:normal;font-weight:normal;text-align:center;text-anchor:middle;opacity:0.62000008; fill:#000000;fill-opacity:1;stroke:#1247bc;stroke-width:2.79999995;stroke-miterlimit:4;stroke-opacity:1; stroke-dasharray:none;font-family:BahamasHeavy;-inkscape-font-specification:BahamasHeavy"><tspan x="190.37181" y="54.048714" id="tspan5805" style="fill:#00ff00;stroke:#1247bc;stroke-width:2.79999995;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none">svg</tspan></text> </g> </g> </svg> </article> <article> </article> <article> </article> </section> <section id="footer"> <p id="copyright"> Copyright © 2012 diades - <a href="/" rel="" title="DiaDes Dot Net">DiaDes Dot Net</a></p> </section> </body> </html> Prev — || — Next
  • Recent Articles