This document discusses Scalable Vector Graphics (SVG), including basics, gotchas, and optimization. It covers when to use SVG versus other formats like PNG and JPG, how to include SVGs on a website using various methods, and tips for optimizing SVG file size such as using the viewBox attribute and inheriting values. The summary emphasizes that SVG is for vector graphics, HTML and SVG can coexist but don't mix, and to consider file size, complexity, and different embedding options when working with SVGs.
15. ✅ Wins
• Easy to read/write
• Live in HTML document
• CSS can change
presentation
⚠ Warnings
• HTML inside SVG breaks
• SVG doesn’t inherit size
from children
• SVG needs viewBox to
scale
SVG + HTML
16. ✅ Wins
• fill can inherit
• currentColor pulls value
from color
⚠ Warnings
• Inline attributes override
inherited styles
• Direct selectors override
inline attributes
• Selector > Inline > Inherit
SVG + CSS
17. SVG Sizing
✅ ❌
viewBox, width, height 300 x 150
viewBox width, height 100%
width, height viewBox Crop to fit
viewBox, width, height Scale to fit
18. SVG needs viewBox to scale
SVG != HTML
External images require
xmlns="http://www.w3.org/2000/svg"
tl;dr📚
19. •When is SVG better than PNG?
•How do I export SVGs from Sketch?
•How do I include SVGs on my website?
But what about…🤔
62. 1. If you have a vector asset, try SVG first
2. If SVG file size is too large, use PNG*
3. If transparency doesn’t matter, use JPG*
Rules of Thumb👍
*Use WebP too!
64. Inline <svg>
✅ Pros
• Loads immediately
• Can manipulate
• Accessible*
❌ Cons
• Bloat
• Duplication†
†Unless you manage your markup with
reusable components.
*Assuming you include a <title> / <desc>,
or at least some <text>.
66. Sprite Map <use>
✅ Pros
• Reusable
• External SVGs are cached*
• Works inline & external
• Can manipulate†
❌ Cons
• Can’t animate†
• Can’t use cross-domain,
CORS
*Requires XML namespace attribute:
xmlns="http://www.w3.org/2000/svg"
†Can’t animate, but can still adjust colors.
Great opportunity for CSS Variables.
67. CSS background-image
✅ Pros
• Reusable
• Clean markup
• External CSS is cached*†
❌ Cons
• Bloat†
• Can’t manipulate
*Requires XML namespace attribute:
xmlns="http://www.w3.org/2000/svg"
†If you encode your image as a data URI,
make sure you encode special characters
68. 🔥 Summary 🔥
• SVG = Scalable Vector Graphics
• Bitmaps store pixels, Vectors store paths
• HTML and SVG are similar but different (they can coexist but don’t mix)
• viewBox defines an SVG’s artboard and allows it to scale
• Take advantage of inherited values (e.g., fill: currentColor)
• Watch out for base64 bitmaps, shape complexity, and text layers
• Use SVGs for vector, PNG when they’re too big, and JPG for the rest
• Use WebP too!
• Include SVGs with <svg>, <img>, <use>, and background-image