## SVGs ### Scalable Vector Graphics --- ### Key Takeaways - Vector logos look sharp at all zoom levels. - SVG files are generally smaller in file size compared to raster images. - Inline SVGs are part of the DOM, which can be styled and animated using CSS and Javascript. - Complex SVGs may impact performance due to processor load. --- ## 3 Ways to add SVGs to HTML --- ### 1. `
` Element HTML: ```html
``` CSS: ```css img { display: block; width: 50vmin; } ``` --- ### 2. `background-image` Property HTML: ```html
``` CSS: ```css .image { width: 50vmin; height: 50vmin; background-image: url(no-bugs.svg); background-size: cover; } ``` --- ## SVGs as Images ### Advantages - No need to worry about SVG code; the browser will treat it like a raster image. - Image will be sharp at any size. - Aspect ratio will always be preserved (raster images can be distorted). ### Disadvantages - Internal SVG elements cannot be styled or animated. --- ### 3. Inline SVG ```html
``` Copy/paste this code into a `` element. #### Source: [ban](https://fontawesome.com/icons/ban?s=solid), [bacterium](https://fontawesome.com/icons/bacterium?s=solid) --- ## Inline SVGs ### Advantages - Can be styled/and animated with CSS. - Can be controlled with JS using the DOM. - No extra server calls needed: the SVG since it arrived with the HTML file. ### Disadvantages - Large inline SVGs can clutter your HTML. --- ## Tips and Tricks --- ### `width` and `height` Attributes
width="496" height="496"
- When in doubt, remove the `width` and `height` attributes to make your SVG responsive. - Without these attributes, the SVG will be 100% the width of its container. - Either way, the actual width and height of an SVG can be overridden with CSS. --- ### `xmlns` Attribute
xmlns="http://www.w3.org/2000/svg"
- `xmlns` is not needed for inline SVGs but is required for SVGs referenced as images (`
` or `background-image`). - When in doubt, include this attribute in case you want to use the SVG as an image. --- ### `version` Attribute
version="1.1"
- The `version="1.1"` attribute should always be removed. --- ### `viewBox` Attribute
viewBox="0 0 496 496"
- Always include the `viewBox` attribute. Without it, the SVG cannot be made responsive. - CSS effects _cannot_ leave the SVG viewport, which is defined by the `viewBox`. - See [this Codepen](https://codepen.io/acidtone/pen/eYBzxPq) for an example. - If this happens, it's usually easier to expand your viewBox in your vector editor. --- ### Exporting SVGs - Every vector application is different. - For inline SVGs, you generally want to flatten _Pathfinder effects_ before export. - If you want to ensure certain paths are separated, put them on different layers before export. - Always optimize your SVGs for the web with a tool like [SVG OMG](https://jakearchibald.github.io/svgomg/). --- ### Optimizing with [SVG OMG](https://jakearchibald.github.io/svgomg/) - Most of the defaults are fine. Prettify the code, if desired. It won’t add a lot to file size. - `width` and `height` 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. - See [Understanding and Manually Improving SVG Optimization](https://css-tricks.com/understanding-and-manually-improving-svg-optimization/) for more information than you probably need. --- ## Styling SVGs - Instead of `border-color` use `stroke`. - `border-width` -> `stroke-width` - `border-style` -> `stroke-dasharray` - Instead of `background-color` use `fill`. - Most other styles will work on inline SVGs but some don't. You mileage may vary. - Other SVG-specific styles exist that you can use in CSS such as `stroke-linecap` and `stroke-miterlimit`. --- ### Extras - [SVG's can do that?](https://www.youtube.com/watch?v=4laPOtTRteI) by Sarah Drasner - SVG animation libraries - [Greensock](https://greensock.com/) - [Animejs](https://animejs.com/) - Activities - [Add a colour scheme to a pinball diagram](https://gist.github.com/acidtone/118f11cd417a7b20fb4f6976f36767a1) ([Codepen](https://codepen.io/acidtone/pen/QWGERKm))