Notes

Drawing SVGs

Edit on GitHub


Web Development
3 minutes

Notes on keeping SVGs crisp

  • Keep stroke widths in even numbers (0, 2, 4, 6) to keep em sharp. Browsers draw lines in between pixels
  • SVG coordinates are mapped to the left of a pixel line and not in the middle. A single pixel line at (2, 1), your line will be drawn on half pixels resulting in blurred or anti aliased line. But if we offset it by 0.5 then it will be drawn in the middle, will fill the entire pixel and look sharp. Draw at (2.5, 1) instead.
  • Draw on pixel boundries top keep it sharp, i.e. snap to pixel.
  • You can use shape-rendering=crispEdges to disable browser anti-aliasing.

viewBox, height and width

Here’s the simplest SVG, a box

1<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
2  <rect width="100" height="100" fill="lightseagreen"/>
3</svg>
  • Default height and width values are auto which is treated as 100%. If you don’t specifically give height and width values to the SVG, it’ll assume 100%. This gives you a responsive SVG which will take up entire available space.
  • If there is no containing element for the SVG, it will take up the entire screen, even if the value is of the viewBox is 0 0 32 32. If it’s in a 64px container element, the SVG will be 64px.
  • If height and width have fixed values, say 32px, the SVG will remain that size instead of taking up entire space, i.e. a non-responsive SVG
  • viewBox value does not have to match the height/width value. they don’t have to be the same SVG
viewBox = <min-x> <min-y> <width> <height>
1<!-- RESPONSIVE SVG -->
2<svg viewBox="0 0 460 100" xmlns="http://www.w3.org/2000/svg">
3  <rect width="100" height="100" fill="lightseagreen"/>
4  <rect x="120" width="100" height="100" rx="15" fill="salmon"/>
5  <rect x="240" width="100" height="100" rx="15" fill="plum"/>
6  <rect x="360" width="100" height="100" rx="15" fill="palevioletred"/>
7</svg>
1<!-- FIXED SIZE SVG -->
2<svg viewBox="0 0 460 100" width="460" height="100" xmlns="http://www.w3.org/2000/svg">
3  <rect width="100" height="100" fill="lightseagreen"/>
4  <rect x="120" width="100" height="100" rx="15" fill="salmon"/>
5  <rect x="240" width="100" height="100" rx="15" fill="plum"/>
6  <rect x="360" width="100" height="100" rx="15" fill="palevioletred"/>
7</svg>
  • A responsive SVG will try to fit in the container’s size, but if the container is too small SVG will overflow
  • The containing element will determine the size of the SVG
 1<!-- CONATINED SVG -->
 2<div style="width: 64px; height: 64px">
 3  <svg viewBox="0 0 460 100" xmlns="http://www.w3.org/2000/svg">
 4    <!-- Simple rectangle -->
 5    <rect width="100" height="100" fill="lightseagreen"/>
 6
 7    <!-- Rounded corner rectangle -->
 8    <rect x="120" width="100" height="100" rx="15" fill="salmon"/>
 9
10    <!-- Rounded corner rectangle -->
11    <rect x="240" width="100" height="100" rx="15" fill="plum"/>
12
13    <!-- Rounded corner rectangle -->
14    <rect x="360" width="100" height="100" rx="15" fill="palevioletred"/>
15  </svg>
16</div>