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
svgwith CSS usingfillandstroke. - Apply a CSS
filterto an inlinesvg.
Materials
- Gist: 3 ways to insert an SVG
Key Takeaways
- Inserting an
svgas either an HTML (using animgelement) 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
fillandstrokeproperties, respectively. See MDN:Fills and Strokes - When inserting as an image, all styling must be included with the SVG itself. When using an inline
svgelement, these styles can be removed (assuming you’re styling with CSS). - Unlike images, the browser will not break the aspect ratio of an inline
svgand will crop the graphic instead. SetpreserveAspectRatiotononeif you want to override this (this is rarely preferred).
2. viewBox and SVG anatomy
Learning Objectives
- Discuss the role of the
widthandheightattributes of ansvgelement and it’s relationship to responsive design. - Discuss the nature of the
viewBoxand 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
svgwith SVGOMG.
Materials
- Gist: The problem with
viewBox
Key Takeaways
-
Remove
widthandheightattributes from andsvgelement to make it responsive.Warning: There are many references to the SVG “viewport” in online resources, relating to the
widthandheightattributes. This is not the same as the browser viewport. Save yourself the hassle and just delete thewidthandheightattributes and pretend SVG viewports don’t exist (for now). - SVG styles and effects cannot leave the
viewBox. Make yourviewBoxa 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
xmlnsattribute 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.
widthandheightattributes 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.
