Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SVG is for Everybody

Chris Coyier
November 17, 2014

SVG is for Everybody

Slides from the last time I did this talk live, at An Event Apart San Francisco '14.

Chris Coyier

November 17, 2014
Tweet

More Decks by Chris Coyier

Other Decks in Design

Transcript

  1. View Slide

  2. CHRIS COYIER

    View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. mycockpit.club

    View Slide

  7. 2014

    View Slide

  8. View Slide



  9. View Slide

  10. View Slide

  11. View Slide

  12. SLIDE TO SEEM TIMELY AND RELEVANT

    View Slide

  13. 13.9K

    View Slide

  14. 13.9K
    13.9K
    13.9K
    13.9K

    View Slide

  15. Sharp at any size or resolution
    Small in file size
    SVG is

    View Slide

  16. Styleable & Responsive - you can apply CSS
    Scriptable - shapes can be in the DOM
    Systematic - easy to build an icon system
    Animateable - in many comfortable ways
    Interactive - links! hovers! games!
    Accessible - in all the right ways
    SVG is

    View Slide

  17. Big Promises / Can’t Use

    View Slide

  18. Nope. You can use SVG now
    and it’s pretty dang easy.

    View Slide

  19. Well-supported

    View Slide

  20. Even if you need to go deeper, it’s not
    that bad. We’ll get into that in a bit.

    View Slide

  21. HEY
    that looks like
    HTML
    (It’s XML)

    View Slide

  22. View Slide

  23. SVG syntax can be learned!
    Unlike any other image format.
    (You don’t “learn” JPG, unless you’re like Rain Man.)

    View Slide

  24. SVG can be made from
    essentially anything “vector”.
    .eps / .pdf / .ai / .whatever
    open in Illustrator, Save As .svg

    View Slide

  25. “Scaleable Vector Graphics”
    SVG can be made from
    essentially anything “vector”.

    View Slide

  26. SVG will never replace
    raster graphics.

    View Slide

  27. Rasterfarian GET IT?!
    (This image would be very bad at being vector.)

    View Slide

  28. wpapcommunity.com/site/index.php/gallery/musisi-jazz/bob-marley-rastaman-2048
    (This image would be very good at being vector.)

    View Slide

  29. Todd Parker’s Chart
    https://docs.google.com/presentation/d/1CNQLbqC0krocy_fZrM5fZ-YmQ2JgEADRh3qR6RbOOGk/edit#slide=id.p

    View Slide

  30. 94K

    View Slide

  31. 1.3MB
    129K

    View Slide

  32. How do you get
    SVG to use?

    View Slide

  33. 1
    Learn the SVG syntax and
    hand-craft them.

    View Slide








  34. View Slide

  35. Basic Shapes




    View Slide

  36. 2
    Make your own images in a
    vector editing program

    View Slide

  37. View Slide

  38. (p.s. you can copy and paste SVG out of Illustrator CC)

    View Slide

  39. Illustrator
    $19.99 / month
    Extremely robust
    Lots of training/tutorials available

    View Slide

  40. Sketch
    $99
    Modern take on design software
    Great for web
    Great exporting

    View Slide

  41. View Slide

  42. WebCode
    $39.99

    View Slide

  43. iDraw
    $24.99

    View Slide

  44. Inkscape
    Free

    View Slide

  45. View Slide

  46. 3
    Go snag some vector stuff
    off the internet.

    View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. • Adjust colors
    • Change artboard size
    • Remove cruft
    • Save it

    View Slide

  51. View Slide

  52. http://creativedroplets.com/export-svg-for-the-web-with-illustrator-cc/

    View Slide

  53. View Slide

  54. How to Use SVG
    LIKE, ON WEBPAGES YA TEAMSTERS

    View Slide


  55. SVG-as-img
    HTML

    View Slide

  56. SVG-as-img
    HTML

    View Slide

  57. View Slide

  58. .clover {
    background: url(clover.svg);
    }
    Great way to support gradients in IE 9.
    Compass can automatically do that for you.
    www.sitepoint.com/getting-started-svg-gradients/
    SVG as background-image
    CSS

    View Slide

  59. html5hub.com/#i.5cx78814yheb6u

    View Slide

  60. View Slide

  61. View Slide

  62. AddType image/svg+xml .svg .svgz
    .htaccess
    css-tricks.com/snippets/htaccess/serve-svg-correct-content-type/
    content-type fix

    View Slide

  63. Because SVG can be resized worry-free, you
    probably will resize them most of the time.
    http://blog.froont.com/9-basic-principles-of-responsive-web-design/

    View Slide

  64. img {
    max-width: 100%;
    height: auto;
    }
    CSS
    img[src$=".svg"] {
    max-width: auto;
    width: 100%;
    height: auto;
    }
    Don’t get bigger than the
    container element.
    Also, don’t scale up.
    Don’t get bigger than the
    container element.
    Scale up if you need to.

    View Slide

  65. .horse {
    background: url(horse.svg);
    background-size: cover;
    }
    CSS
    Firefox used to have an issue with blurring SVG when resizing, but not anymore since v24.
    Fill the entire background of
    this element no matter what.
    Scale up if you have to.

    View Slide

  66. You can put right up in
    your , homegirl.
    Inline SVG

    View Slide

  67. View Slide

  68. Three Big Advantages to
    Using Inline SVG:
    1) Shapes are in the DOM: CSS Styling! JavaScript stuff!
    2) No additional HTTP Request (it’s like a data URI, but better)
    3) You can use define once and use in multiple places

    View Slide

  69. SVG Templating
    Using inline as an icon system

    View Slide

  70. Sites need a system for icons.

    View Slide



  71. View Slide


  72. View Slide






  73. View Slide








  74. View Slide









  75. View Slide









  76. View Slide



  77. View Slide




  78. View Slide






  79. Blog





    Videos

    View Slide

  80. View Slide

  81. View Slide

  82. 2014.cssconf.com
    codepen.io/elrumordelaluz/pen/dhwLi

    View Slide

  83. Even better, let’s get that block of SVG
    out of the HTML entirely.

    View Slide

  84. defs.svg







    View Slide








  85. View Slide




  86. browser caching!

    View Slide

  87. IE (any version) won’t allow with an external source.
    Jonathan Neal has a fixer-upper script.
    (XHRs in the guts so it’s like straight up inline SVG.)

    View Slide

  88. Using instead of
    is even better!

    View Slide








  89. Icon of a clamdigger.


    View Slide




  90. Icon of a clamdigger.






    Or, do add viewBox if
    you want/need
    width: auto;
    behavior.

    View Slide

  91. .specific-icon {
    background: url('svg.svg#svgView(viewBox(0,0,48,41))');
    }




    Fragment Identifiers

    View Slide

  92. This is the idea of
    SVG Sprites

    View Slide

  93. View Slide

  94. 1) Vector (awesome)
    2) No background-position craziness
    3) More CSS control (i.e. SVG-specific, @media, etc)
    4) All the other advantages of inline SVG (accessible, multiple links, scriptable)
    5) Fallbacks are possible
    It’s the same idea,
    only way better because:

    View Slide

  95. View Slide

  96. codepen.io/chriscoyier/pen/mhaFy

    View Slide

  97. View Slide

  98. View Slide

  99. View Slide

  100. View Slide

  101. View Slide

  102. View Slide

  103. View Slide

  104. Media Queries in CSS + SVG

    View Slide

  105. All this stuff is possible on inline SVG.
    You get some advantages of SVG with or
    background-image, but not as many.

    View Slide

  106. SVG vs. Icon Fonts
    Cagematch!

    View Slide

  107. THE ANCIENT PAST

    View Slide

  108. Icon Font SVG
    Vector yup (but anti-aliasing…) yup
    CSS Control some (single color) better
    Positioning die XXX murder good
    Weird Failures* yup less
    Semantics none / ::before better
    Accessibility be very careful better,
    Ease of use meh better
    Browser Support IE 6+ IE 9+
    Weird Failures: CORS/Fx + more?, CDN/network fail, Chrome non-active tab, no @font-face support (Opera
    Mini, Nokia XPress, Blackberry 4&5, Android 2.1, Windows Phone 7, 7.8, 8?, more?)

    View Slide

  109. Guess which one I like?
    Tip: it’s the one that isn’t icon fonts.

    View Slide

  110. If you’re going to use
    icon fonts, read this.
    It goes into how it can
    be done perfectly* in
    regards to accessibility,
    browser support, and
    failures.
    *95%

    View Slide

  111. View Slide

  112. Build Tools
    Don’t freak out.

    View Slide

  113. View Slide

  114. View Slide

  115. View Slide

  116. View Slide

  117. Optimizing SVG
    1. Removing meta junk
    2. Simplifying shapes
    3. Reducing crazy precision
    4. Removing hidden elements
    5. Removing whitespace
    youtube.com/watch?v=iVzW3XuOm7E&feature=youtu.be
    youtube.com/watch?v=1AdX8odLC8M&feature=youtu.be
    Kyle Foster has great videos
    detailing SVG optimization

    View Slide

  118. View Slide

  119. Raster images
    are a grid of
    pixels so it’s
    easy to
    understand
    they have an
    intrinsic width
    and height.

    View Slide

  120. View Slide

  121. View Slide

  122. SVG has a
    Viewport and viewBox
    Viewport is the size of the element. Think how the browser
    window is the viewport for the whole site but an iframe has it’s
    own smaller viewport. Likewise, SVG has it’s own viewport.
    viewBox is an attribute on . It is the “canvas” on which
    SVG instructions are carried out. It is essentially a nexus with
    an aspect ratio (don’t think of them as pixels).

    View Slide

  123. View Slide


  124. .buddy {
    width: 400px;
    height: 200px;
    }

    View Slide


  125. .buddy {
    width: 200px;
    height: 200px;
    }

    View Slide


  126. .buddy {
    width: 200px;
    height: 200px;
    }

    View Slide

  127. Dealing with
    Fallbacks
    e.g. dealing with IE 8 and Android 2.3

    View Slide

  128. Remember, one kind of fallback
    is nothing.
    Like if you’re using an icon to enhance a word.

    View Slide

  129. View Slide



  130. <br/>svgeezy.init(false, "png");<br/>

    View Slide

  131. View Slide



  132. <br/>svgeezy.init(false, 'png');<br/>

    View Slide

  133. codepen.io/chriscoyier/pen/jnsIr

    View Slide

  134. codepen.io/chriscoyier/pen/jnsIr

    View Slide

  135. It works, but it requests two images in
    non-supporting browsers.

    View Slide





  136. … but, SVG is more well supported than the responsive images stuff is.

    View Slide

  137. View Slide





  138. View Slide

  139. .my-element {
    background-image: url(fallback.png);
    background-image:
    linear-gradient(transparent, transparent),
    url(image.svg);
    }
    Fallback for CSS background-image
    This works because it combines multiple backgrounds and old-syntax gradients,
    thus it works for Android 2.x where multiple backgrounds alone did not.
    You could use Modernizr too, but if you just need it for this, it would be overkill.
    pauginer.tumblr.com/post/36614680636/invisible-gradient-technique

    View Slide

  140. Also, background is generally
    decorative and non-vital to content, so no
    fallback is often acceptable.

    View Slide

  141. Supporting (most) browsers
    Just leaves alone
    IE 9-11
    Ajax’s in
    IE 6-8

    View Slide

  142. Animating SVG
    Let us count the ways.

    View Slide

  143. 1. SVG Syntax (“SMIL”)
    2. With CSS @keyframes / transitions
    3. With JavaScript

    View Slide

  144. View Slide



  145. attributeName="points" dur="500ms" to="110,58.2 147.3,0 192.1,29 …”/>


    Save

    View Slide

  146. View Slide

  147. View Slide

  148. View Slide

  149. View Slide

  150. View Slide

  151. SVG Filters
    & Blend Modes
    The OG of cool web effects.

    View Slide







  152. .an-event-apart {
    -webkit-filter: url(#blur);
    filter: url(#blur);
    }

    View Slide

  153. View Slide

  154. SVG Filters
    You can use CSS filters on SVG, but SVG has its own special filters too.

    View Slide

  155. Accessible SVG
    Thankfully it ain’t that hard.

    View Slide

  156. View Slide

  157. 1. Use SVG
    2. Use Inline SVG
    3. Provide a and aria-labeledby
    4. Provide a and aria-labeledby
    5. Use
    6. Make links focusable (if parts)
    7. Use ARIA roles
    8. Create alternatives

    View Slide

  158. aria-labelledby="title desc">
    Green rectangle

    A light green rectangle with
    rounded corners and a dark green border.


    fill="#90ee90" stroke="#228b22" stroke-fill="1" />
    fill=“#000000">
    Website



    View Slide

  159. http://css-tricks.com/svg-text-typographic-designs/

    View Slide

  160. View Slide

  161. @chriscoyier

    View Slide