SVG Fundamentals
Homework
- Review
- Scalable Vector Graphics
- Watch: A beginners guide to SVG: Part 1 by Kevin Powell. You can skip Part 2 but watch Part 3 if you’re interested.
- Optimizing SVG
- Tool: SVG OMG
- Watch: How to Optimize SVGs from your editor by Kevin Powell (about 19 minutes into a longer video)
- Advanced: Understanding and Manually Improving SVG Optimization
- SVG in HTML Documents
- Read: Using SVG on CSS-Tricks
- Read: How to Scale SVG
- How to Work with SVGs in Figma, HTML, and CSS by Build UX
- Optional (but fun): SVG Can Do That?! by Sarah Drasner
Morning reflection
Housekeeping
- Javascript homework
- Show-and-Tell
- Joel
- Vitaly
1. Inserting SVGs
Learning Objectives
- Explore 3 ways to insert an SVG into an HTML page.
- Discuss the advantages and disadvantages of each method.
- Style an inline
svg
with CSS usingfill
andstroke
. - Apply a CSS
filter
to an inlinesvg
.
Materials
- Gist: 3 ways to insert an SVG
Key Takeaways
- Inserting an
svg
as either an HTML (using animg
element) or CSS image (usingbackground-image
) treats it like any other image. -
Inserting an inline SVG allows you to style it with CSS.
Note: SVGs are defined by a different specification than HTML and CSS. Background colours and borders are controlled with the
fill
andstroke
properties, respectively. See MDN:Fills and Strokes - When inserting as an image, all styling must be included with the SVG itself. When using an inline
svg
element, these styles can be removed (assuming you’re styling with CSS). - Unlike images, the browser will not break the aspect ratio of an inline
svg
and will crop the graphic instead. SetpreserveAspectRatio
tonone
if you want to override this (this is rarely preferred).
2. viewBox
and SVG anatomy
Learning Objectives
- Discuss the role of the
width
andheight
attributes of ansvg
element and it’s relationship to responsive design. - Discuss the nature of the
viewBox
and its relationship to the rest of an SVG. - Explore different methods of exporting an SVG from Figma (for example) to control the
viewBox
. - Optimize an
svg
with SVGOMG.
Materials
- Gist: The problem with
viewBox
Key Takeaways
-
Remove
width
andheight
attributes from andsvg
element to make it responsive.Warning: There are many references to the SVG “viewport” in online resources, relating to the
width
andheight
attributes. This is not the same as the browser viewport. Save yourself the hassle and just delete thewidth
andheight
attributes and pretend SVG viewports don’t exist (for now). - SVG styles and effects cannot leave the
viewBox
. Make yourviewBox
a little larger than the graphic if styles or filter effects are getting clipped. It’s easiest to do this in a vector editor like Figma or Illustrator. - The
xmlns
attribute is needed if you want to support non-HTML5 browsers (we probably do) or XML parsers (we probably don’t). - SVGOMG:
- Most of the defaults are fine.
- Prettify the code, if desired. It won’t add a lot to file size.
width
andheight
attributes will be removed by default (this is good).- A lower precision will result in a smaller file size but very low values might affect the detail of your designs.
Open lab-time
Activities:
- Gist: SVG Anatomy
- Gist: Inserting an SVG
- Gist: Add an SVG colour scheme
Tony’s goals for Lab-Time
- Menu cleanup
- Add JS MDN exercises to primary nav
Dailies
- Submit today’s Codepen/repo/gist to the Dailies section (in Assessments) in Brightspace.