On Github cloudfour / slides-svg-101
A Gentle Introduction
Since 2001 (Seriously!)
331 bytes (optimized)
103 bytes (optimized)
<img width="240" height="240" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAABEklEQVR42r1WSw6CMBDlJP4uY1rQrZ8buDKENnAh5QpyEg+gJ7AIavzNM8TGhQ0SxsVLoZ15r0M7M3jBIvyKIg46uZKJ0TIzSu5oPNG7ybXYGiU2WCujUd/F4SAWa6PFNdfy4QIJ3WALn1oCR+2PyeEA51+AyCjCmVOAdrzEjuDQBCRwJ4SfApZ8AgPr0FwEXFbAHqaBQRsAFzjfAjS5wkK7EOlLoIhkt/l3d98ucNPu/RgTHAC3h4ThEgA3bs+eSwDcHtKfMYIzu8BfPlHGecj817SMhz2uRAN3VSpEylEq/lfsABP507bKNbgYG46I+FqmEvPaTR+HVCca2MDW0fTr/bYgK2m8ANVzhrUy8Qcujid2yGIBaj5olQAAAABJRU5ErkJggg=="> <svg width="240" height="240" viewBox="0 0 24 24"> <circle fill="#F26941" cx="12" cy="12" r="12"/> </svg>
Made up of scalable vector shapes and paths (not pixels).
Written as XML and optionally CSS.
Accessible to screen readers and assistive devices.
Embeddable as images, objects or directly into HTML.
Able to be manipulated by CSS and/or JavaScript.
Supported earlier than IE9 and Android 3.
100% consistent in modern browsers.
A replacement for canvas.
Particularly intuitive to learn.*
* There are three ways of doing everything. 😅
(Even if you exported from one of the aforementioned apps!)
The viewBox attribute defines the coordinate system.
<svg viewBox="0 0 100 100"></svg>
Values are minimum x and y coordinates, then width and height.
<!-- Normal: --> <svg viewBox="0 0 100 100"></svg> <!-- Larger: --> <svg viewBox="0 0 200 200"></svg> <!-- Smaller: --> <svg viewBox="0 0 50 50"></svg> <!-- Negative: --> <svg viewBox="-50 -50 100 100"></svg> <!-- Mixed: --> <svg viewBox="-25 50 75 50"></svg>
Further reading: Understanding SVG Coordinate Systems →
By default, SVGs will attempt to fill their container regardless of viewBox. You can specify dimensions to make the asset behave more like an img would.
<svg width="100" height="100" viewBox="0 0 100 100"></svg>
Tells the browser how to handle dimensions that contradict viewBox.
<svg></svg> <svg preserveAspectRatio="none"></svg> <svg preserveAspectRatio="xMidYMin"></svg> <svg preserveAspectRatio="xMidYMax"></svg> <svg preserveAspectRatio="xMidYMid slice"></svg>
More examples: preserveAspectRatio on MDN →
<svg viewBox="0 0 320 100"> <rect fill="#5A7BDD" x="0" y="0" width="100" height="100" rx="12" ry="12"/> <circle fill="#F26941" cx="160" cy="50" r="50"/> <line x1="230" y1="90" x2="310" y2="10" stroke="#0F1C3F" stroke-width="10" stroke-linecap="round"/> </svg>
<svg viewBox="0 0 225 100"> <polygon fill="#F26941" points="50,0 95,25 95,75 50,100 5,75 5,25"/> <path fill="none" d="M 120 90 c 0 -100 100 -100 100 0" stroke="#5A7BDD" stroke-width="10" stroke-linecap="round"/> </svg>
<svg viewBox="0 0 210 100"> <style type="text/css"> .demo-shape { fill: #5A7BDD; } .demo-shape:hover { fill: #F26941; } </style> <rect class="demo-shape" x="0" y="0" width="100" height="100" rx="12" ry="12"/> <circle class="demo-shape" cx="160" cy="50" r="50"/> </svg>
If embedded in-page, you can style SVGs from that page’s stylesheet. SVGs can also link to external stylesheets just like webpages.
<svg viewBox="0 0 24 24"> <style type="text/css"> .logo { fill: #5A7BDD; } </style> <g class="logo"> <!-- multiple paths --> </g> </svg>
<svg viewBox="0 0 340 100"> <circle id="orange-circle" fill="#F26941" cx="50" cy="50" r="50"/> <use xlink:href="#orange-circle" x="120"/> <use xlink:href="#orange-circle" x="240"/> </svg>
<svg viewBox="0 0 340 100"> <defs> <circle id="circle" cx="50" cy="50" r="50"/> </defs> <use xlink:href="#circle" fill="#0F1C3F" x="0" /> <use xlink:href="#circle" fill="#5A7BDD" x="120"/> <use xlink:href="#circle" fill="#F26941" x="240"/> </svg>
Further reading: Structuring, Grouping, and Referencing in SVG →
Re-usable groups with their own viewBox and preserveAspectRatio attributes.
Welcome to Cloud Four
<svg> <symbol id="cloud-four" viewBox="0 0 24 24"> <!-- paths and stuff --> </symbol> </svg> <!-- Later on in the same document... --> <h1> <svg class="icon"> <!-- Look ma, no viewbox! --> <use xlink:href="#cloud-four"/> </svg> Welcome to Cloud Four </h1>
Further reading: SVG symbol a Good Choice for Icons →
<svg viewBox="0 0 100 100"> <title>Orange circle</title> <desc>A delightfully balanced apricot ellipse.</desc> <circle fill="#F26941" cx="50" cy="50" r="50"></circle> </svg>
Further reading: Tips for Creating Accessible SVG →
<svg viewbox="0 0 480 240"> <style type="text/css"> /* text styles */ </style> <a xlink:href="http://cuteoverload.com" target="_blank"> <image xlink:href="http://placeimg.com/480/240/animals" x="0" y="0" height="240" width="480"/> <text x="240" y="120" text-anchor="middle" dominant-baseline="central" transform="rotate(-22 240 120)">Animals!</text> </a> </svg>
Elements can be re-used, transformed, filtered, masked and animated as browser support allows.
Animals!Performant, familiar
See the Pen <a href='http://codepen.io/tylersticka/pen/850480f3e2c4bb63f719dbc3c491554c/'>850480f3e2c4bb63f719dbc3c491554c</a> by Tyler Sticka (<a href='http://codepen.io/tylersticka'>@tylersticka</a>) on <a href='http://codepen.io'>CodePen</a>.Powerful, compatible, concise
See the Pen <a href='http://codepen.io/tylersticka/pen/1157b5e469f9c3275ad4db766e6934b1/'>1157b5e469f9c3275ad4db766e6934b1</a> by Tyler Sticka (<a href='http://codepen.io/tylersticka'>@tylersticka</a>) on <a href='http://codepen.io'>CodePen</a>.GSAP is by far the most powerful, and usually the most performant. It can handle tweening, sequencing, easing and a bunch more.
Snap.svg is the most common choice for animating path data, but this feature is coming to GSAP soon.
Other alternatives include Velocity.js and Animate Plus.
Further reading: The State of SVG Animation →
Icons inherit parent color except when color is explicitly defined.
See the Pen <a href='http://codepen.io/tylersticka/pen/137a12b3c16376c3eed6c97d7a97a31f/'>137a12b3c16376c3eed6c97d7a97a31f</a> by Tyler Sticka (<a href='http://codepen.io/tylersticka'>@tylersticka</a>) on <a href='http://codepen.io'>CodePen</a>.Animates with CSS and GSAP.
See the Pen <a href='http://codepen.io/tylersticka/pen/681ff18f6b10a73cc30b2a310b0dbada/'>Button Morph (GSAP)</a> by Tyler Sticka (<a href='http://codepen.io/tylersticka'>@tylersticka</a>) on <a href='http://codepen.io'>CodePen</a>.Uses masks and text paths, animates on click with GSAP.
See the Pen <a href='http://codepen.io/tylersticka/pen/e85cda8fa38844644763b9ebb6c6365f/'>Bathroom Lock</a> by Tyler Sticka (<a href='http://codepen.io/tylersticka'>@tylersticka</a>) on <a href='http://codepen.io'>CodePen</a>.<svg> <!-- Older browsers render as img: --> <image src="fallback.png" xlink:href="" width="100%"> </svg> <object type="image/svg+xml" data="example.svg"> <!-- Fallback content --> </object> <picture> <source type="image/svg+xml" srcset="example.svg"> <img src="fallback.png"> </picture>
.example { background-image: url("fallback.png"); } .svg .example { /* Modernizr */ background-image: url("example.svg"); }
Firefox handles proportional transform-origin differently than other browsers, though a fix is coming.
IE does not support external content without a polyfill.
Prior to Android 4.4, the default Android browser does not support masking.
Opera Mini and Opera Mobile versions earlier than 12.1 pixelate SVG graphics that are scaled up.