SVG – less pixel, more fun!
Sven Wolfermann | maddesigns
Sven Wolfermann | maddesigns
<svg viewBox="0 0 300 200" width="300" height="200">
<rect width="300" height="200" fill="red" />
<circle cx="150" cy="100" r="80" fill="green" />
</svg>
As Image
<img src="mySVG.svg" alt="image alt text" />
Object Tag
<object type="image/svg+xml" data="mySVG.svg"><!--fallback--></object>
iFrame
<iframe src="mySVG.svg"><!--fallback--></iframe>
CSS
.element {background: url(mySVG.svg);}
Inline SVG (HTML5)
<svg></svg>
<!--[if (gt IE 8)]><!--><svg></svg><!--<![endif]-->
logo.svg
<div class="logo">
<img src="logo.svg">
</div>
with Modernizr fallback
.no-svg .logo {
background: url("logo.png");
}
.no-svg .logo img {
visibility: hidden;
}
Font Awesome is a full suite of 479 pictographic icons
If you only use a bunch of icons, strip down the files with icomoon.io
pick your icons
save as font (or SVG)
save Kb!
<a class="st-icon st-multi-color st-icon-github st-shape-icon" href="">
:before
<span>
:before
:after
</span>
Github
:after
</a>
kinda works, but NO!
Ten reasons we switched from an icon font to SVG
Chris Coyier – SVG Is For Everybody – beyond tellerrad Düsseldorf 2014
internal SVG element reference
<defs>
<symbol id="menu">
<path d="…" />
</symbol>
</defs>
<use xlink:href="#menu" />
external SVG element reference (cachable!)
<svg xmlns:xlink="…">
<use xlink:href="img/all-icons.svg#menu" />
</svg>
SVG Store – Grunt task
svgstore: {
options: {},
dist: {
files: {
'dist/img/all.svg': ['source/img/icons/*.svg']
}
}
}
integration “icon home” in HTML:
<svg class="icon-home" role="img" title="Home Icon"
viewbox="0 0 75 75" width="25" height="25">
<use xlink:href="img/all.svg#home"></use>
</svg>
integration “icon menu” in HTML:
<svg class="icon-menu" role="img" title="Menu Icon"
viewbox="0 0 75 75" width="25" height="25">
<use xlink:href="img/all.svg#menu"></use>
</svg>
// inherit the fill color from parent
svg {
polygon, path, rect, circle {
fill: inherit;
}
}
fill all icons with red
// fill all elements contains the class „icon” with ’red’
[class*="icon"] {
fill: red;
}
[class*="icon"] {
fill: red;
}
.icon-menu {
fill: green;
}
.original-svg-pin {
fill: blue;
}
(works in Firefox, but spec said Chrome/WebKit is right)
<path fill="#FF931E" stroke="#ED1C24" stroke-width="5px" />
<svg>
<style type="text/css">
path {
fill: #FF931E;
stroke: #ED1C24;
stroke-width: 5px;
}
</style>
</svg>
Example: Sass Logo
<svg style="display:none;">
<symbol id="sass" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="50" />
<path d="M40.06 66.65c0.73 …"
fill="currentColor"/>
</symbol>
</svg>
<svg class="sass"><use xlink:href="#sass" /></svg>
<svg class="sass sass--color"><use xlink:href="#sass" /></svg>
<svg class="sass sass--invert"><use xlink:href="#sass" /></svg>
Example: Sass Logo
.sass {
color: white;
}
.sass--color{
fill: red;
color: green;
}
.sass--invert{
fill: white;
color: black;
}
- sadly IE9-11 don't support <use> references
- Chrome (WebKit) is buggy
https://github.com/jonathantneal/svg4everybody
Use a part of a SVG sprite (not stylable)
<img src="all-bunnys.svg#svgView(viewBox(0,0,100,100))"
width="100" height="100" alt="bunny" />
<img src="all-bunnys.svg#happy-bunny"
width="100" height="100" alt="happy bunny" />
<img src="all-bunnys.svg#bunny-view"
width="100" height="100" alt="the bunny" />
my blog post (german)
<svg role="img" aria-labelledby="title desc">
<title id="title">Chicken</title>
<desc id="desc">A chicken icon</desc>
<use xlink:href="animals.svg#chicken" />
</svg>
Use WAI-ARIA attributes and roles
aria-labelledby="title" aria-describedby="desc"
aria-labelledby="title desc"
role="img"
It's fine if your SVG content is purely graphical, but if it contains interactive content, a different approach may be needed.
Animating SVG with CSS and SMIL
<svg viewBox="0 0 48 48" width="240px" height="240px">
<circle id="Circle" fill="#00B000" r="20" cx="24" cy="24" />
<path id="Arrow" fill="#FFFFFF" transform="rotate(5, 24 24)"
d="M14.2,25.9l8.9-7.2l-2.3-2.8l12.7,0l-2.4,12.4l-2.5-3l-9,7.1L14.2,25.9z">
<animateTransform
attributeName="transform"
attributeType="XML"
type="rotate"
dur="4s"
repeatCount="indefinite"
values="5 24 24;25 24 24;5 24 24"/>
</path>
</svg>
<svg id="clock" viewBox="0 0 100 100" width="600" height="600">
<defs>
<style type="text/css">
<![CDATA[
#face { stroke-width: 2px; stroke: #fff; }
#hour, #min, #sec { stroke-width: 1px; fill: #333; stroke: #555; }
#sec { stroke: #f55; }
]]>
</style>
<script type="text/javascript">
<![CDATA[
setInterval(function() {
function r(el, deg) {
el.setAttribute('transform', 'rotate('+ deg +' 50 50)')
}
var d = new Date()
r(sec, 6*d.getSeconds())
r(min, 6*d.getMinutes())
r(hour, 30*(d.getHours()%12) + d.getMinutes()/2)
}, 1000)
]]>
</script>
</defs>
var svg, circ;
svg = Snap('#paper');
circ = svg
.circle(10,10,10)
.attr({fill:'#c09'})
.pattern(0,0,25,25)
.attr({patternTransform: 'rotate(45)'});
svg.rect(0,0,'100%','100%')
.attr({fill: circ});
Sven Wolfermann | @maddesigns | maddesigns.de