The Great SVG RetCon
A presentation by
Amelia Bellamy-Royds
(@AmeliasBrain)
at
Exchange.js
(Edmonton, 6 October 2016)
These slides: ameliabr.github.io/great-svg-retcon

Retroactive: Because sometimes you need to fix the past.



RetCon-ing the Web


What you say to old browsers:
<picture>
<source media="(min-width: 650px)" srcset="images/kitten-large.png" />
<source media="(min-width: 465px)" srcset="images/kitten-medium.png" />
<img src="images/kitten-small.png" alt="a cute kitten" />
</picture>
What they hear:
<blah>
<blah-blah />
<blah-blah />
<img src="images/kitten-small.png" alt="a cute kitten" />
</blah>
Markup
Stand-alone SVG files are XML!

Use your browser's parser to clean up sloppy HTML markup

(Or just use MS Edge's Save Picture As… option)
Don't use XML prefixes for SVG or HTML elements in HTML markup.
For namespaced attributes, only use:
xlink:href
xml:lang
Clean up extra XML bloat before pasting into HTML!
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 15.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_extend "http://ns.adobe.com/Extensibility/1.0/">
<!ENTITY ns_ai "http://ns.adobe.com/AdobeIllustrator/10.0/">
<!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/">
<!ENTITY ns_vars "http://ns.adobe.com/Variables/1.0/">
<!ENTITY ns_imrep "http://ns.adobe.com/ImageReplacement/1.0/">
<!ENTITY ns_sfw "http://ns.adobe.com/SaveForWeb/1.0/">
<!ENTITY ns_custom "http://ns.adobe.com/GenericCustomNamespace/1.0/">
<!ENTITY ns_adobe_xpath "http://ns.adobe.com/XPath/1.0/">
]>
<svg version="1.1" id="Ebene_1" xmlns:x="&ns_extend;" xmlns:i="&ns_ai;" xmlns:graph="&ns_graphs;"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="566.93px"
height="283.46px" viewBox="0 0 566.93 283.46" enable-background="new 0 0 566.93 283.46" xml:space="preserve">
<switch>
<foreignObject requiredExtensions="&ns_ai;" x="0" y="0" width="1" height="1">
<i:pgfRef xlink:href="#adobe_illustrator_pgf">
</i:pgfRef>
</foreignObject>
<g i:extraneous="self">
<!-- actual SVG stuff used by browser is here -->
</g>
</switch>
<!-- more Adobe proprietary blobs were here -->
</svg>
Namespaces always matter when scripting!
(xmlns
attribute doesn't, though)
But, innerHTML
triggers the parser.
var ns = {svg: "http://www.w3.org/2000/svg",
xlink: "http://www.w3.org/1999/xlink"};
var u = createElementNS(ns.svg, "use");
u.setAttributeNS(ns.xlink, "href", "#icon");
var wrapper = document.createElement("div");
wrapper.innerHTML = "<svg><use xlink:href='#icon'/></svg>"
var u = wrapper.querySelector("use");
Links
href
attribute to create simple links
href
attribute to create Yes: MS Edge & Internet Explorer (all versions!), Chrome 50+, Firefox 51+ (Nightly / Dev Editions)
No: Safari, Firefox ≤ 50

SVG 2 also makes xml
prefix optional for lang
attribute.
Supported: Chrome, Firefox, & Safari (most versions in common use)
No: MS Edge & Internet Explorer

Styles
Any supported CSS syntax should be supported in SVG.
.icon {
fill: currentColor;
fill: var(--icon-color, currentColor);
}
svg *:link {
text-decoration: underline overline;
}
@supports (text-decoration-style: wavy) {
svg *:link {
text-decoration: wavy underline;
}
}
Geometry
Changing geometry changes the drawing.
Changing styles just adds a little flair…
But… attributes ≠ styles
Same attribute names, on different elements, with different effects:
Same attribute names, on different elements, with different defaults:
SVG geometry properties (that can be set with CSS)
Property | Elements with presentation attribute | Elements with ordinary attribute only |
---|---|---|
width | rect, image, foreignObject, svg, symbol, use | pattern, mask, filter, and filter effect (fe*) elements |
height | ||
x | All above, plus text, tspan, hatch, meshgradient, cursor | |
y | ||
cx | circle, ellipse | radialGradient |
cy | ||
r | circle | |
rx | ellipse, rect | (none) |
ry | ||
d | path | hatchpath |
Transforms
transform
attribute
transform
and transform-origin
transform
presentation attribute
SVG layout, & SVG transforms, are based on a coordinate system.
CSS doesn't always have a coordinate system.
For an off-center shape in a centered SVG coordinate system…
<svg viewBox="-35 -35 70 70">
<polygon points="-10,-10 0,-15 5,-30 10,-15,
20,-10 10,-5 5,10, 0,-5" />
</svg>
You get different transform-box sizes and different box origins:
transform
, transform-origin
and transform-box
transform
presentation attribute
Examples of equivalent transforms, old versus new syntax
SVG 1.1 | CSS Transforms |
---|---|
translate( 16, 0 ) | translate( 16px, 0 ) |
translate( 12pt, 0 ) | |
translateX( 16px ) | |
rotate( 90 ) | rotate( 90deg ) |
rotate( 0.25turn ) | |
rotateZ( 90deg ) | |
scale( 2 ) | scale( 2 ) |
scale( 1, -1 ) | scaleY( -1 ) |
3D Transforms are fun! (but very buggy)
Fallback layout uses the transform
attribute:

There's much more…
paint-order
(good support) andz-index
(no support)- CSS-crossover graphical effects (filters, masking, clip-paths)
- Blend modes instead of filters
- Wrapping text in a shape!
- SVG-in-OpenType fonts (old SVG Fonts are dead)
- Proper keyboard support
- Better accessibility guidance
- Unified approach to Web Animations, CSS or SMIL-like elements (not completely dead)
- Overhauled SVG DOM. Many features removed. Replacements pending…
- New paint servers: mesh gradients and hatches
- Context paint, to coordinate line markers and multi-shape icons
- Coming soon: SVG paint servers for CSS elements, filled & stroked CSS text, CSS images and multiple layers for SVG fill & stroke