---
title: Building Ralph with SVG
teaser:
tags: design,svg
author: Steven Harley
published_on: 2014-08-18
---

SVG (Scalable Vector Graphic) is a graphic format, similar to PNG or JPEG,
that's great for icons, charts and supporting high-DPI displays. They're
vector-based, so you won't lose visual fidelity or see any pixelation, no matter
what size your graphic is. Because it's written in XML, you can mark it up just
like <abbr title="HyperText Markup Language">HTML</abbr> and modify it with
other languages like Javascript or CSS.

## Let's build a robot

SVGs are typically made up of objects, like shapes and paths. Let's start by
building something simple like Ralph's head. Here's our full logo in SVG for
reference:

<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><g fill="none"><path d="M49.922 0C22.364 0 0 22.364 0 49.922c0 27.56 22.364 49.923 49.922 49.923 27.56 0 49.923-22.364 49.923-49.923C99.845 22.364 77.48 0 49.922 0z" id="Shape" fill="#B32317" /><path d="M36.938 52.442h-2.016v14.186c.66.814 1.086 1.822 1.086 2.946 0 1.395-.62 2.635-1.628 3.488l-1.24-1.24c.697-.504 1.124-1.318 1.124-2.248 0-1.55-1.28-2.83-2.83-2.83s-2.83 1.28-2.83 2.83c0 .93.427 1.744 1.125 2.248l-1.242 1.24c-.97-.853-1.628-2.093-1.628-3.488 0-1.124.427-2.17 1.086-2.946V44.535c.038-.853.736-1.55 1.59-1.55h40.503c.85 0 1.55.697 1.588 1.55v22.093c.698.814 1.085 1.822 1.085 2.946 0 1.395-.62 2.635-1.628 3.488l-1.24-1.24c.66-.504 1.124-1.318 1.124-2.248 0-1.55-1.28-2.83-2.83-2.83s-2.83 1.28-2.83 2.83c0 .93.426 1.744 1.124 2.248l-1.24 1.24c-1.008-.853-1.628-2.093-1.628-3.488 0-1.124.426-2.17 1.085-2.946V52.442h-2.014v29.922l1.356.776v2.44H50.426v-2.44l1.357-.776v-9.108h-4.03v9.07l1.356.775v2.443l-13.53.038v-2.44l1.32-.776.038-29.922zM49.806 20c2.83 0 5.35 1.008 6.938 2.52l1.24-1.784c-1.976-1.744-4.883-2.868-8.178-2.868-3.294 0-6.24 1.124-8.178 2.868l1.24 1.783c1.59-1.512 4.148-2.52 6.938-2.52zm.155-4.496c3.063 0 5.93.89 8.063 2.52.427.31.814.658 1.163 1.046l1.24-1.783c-2.403-2.365-6.2-3.915-10.465-3.915-4.38 0-8.255 1.59-10.658 4.07l1.24 1.783c.388-.427.815-.853 1.318-1.24 2.17-1.59 5.04-2.48 8.1-2.48zm-.154-4.34c4.884 0 9.186 1.782 11.744 4.495l1.24-1.745C59.807 10.97 55.08 9.07 49.807 9.07c-5.31 0-10 1.9-12.984 4.883l1.24 1.745c2.558-2.752 6.86-4.535 11.744-4.535zM45.698 32.054c0 .737-.582 1.357-1.357 1.357-.735 0-1.356-.62-1.356-1.356 0-.736.62-1.356 1.357-1.356.776 0 1.358.58 1.358 1.356zm14.186-7.248H39.302c-.852 0-1.55.698-1.55 1.55v13.41c0 .854.698 1.552 1.55 1.552h20.582c.852 0 1.55-.698 1.55-1.55v-13.41c0-.854-.698-1.552-1.55-1.552zM44.34 34.65c-1.433 0-2.635-1.162-2.635-2.634 0-1.435 1.163-2.636 2.636-2.636 1.435 0 2.637 1.163 2.637 2.636 0 1.472-1.163 2.635-2.636 2.635zm10.544 0c-1.434 0-2.636-1.162-2.636-2.634 0-1.435 1.163-2.636 2.636-2.636 1.434 0 2.635 1.163 2.635 2.636 0 1.472-1.202 2.635-2.636 2.635zm1.356-2.596c0 .737-.62 1.357-1.356 1.357-.737 0-1.357-.62-1.357-1.356 0-.736.582-1.356 1.357-1.356.736 0 1.356.58 1.356 1.356z" fill="#fff" /></g></svg>

Open your favorite text editor and create a new file. Start with an empty
`<svg>` element and define its <abbr title="Extensible Markup
Language">XML</abbr> namespace:

    <svg xmlns="http://www.w3.org/2000/svg">
    </svg>

To start creating a graphic, we can add a simple rectangle by specifying a
height, width and fill color.

    <svg xmlns="http://www.w3.org/2000/svg">
      <rect height="60" width="100" fill="#B32317" />
    </svg>

Save that file, open it in a web browser, and you should see something like
this:

<p style="height: 60px">
  <svg xmlns="http://www.w3.org/2000/svg">
    <rect height="60" width="100" fill="#B32317" />
  </svg>
</p>

We can round the corners of the rectangle with `rx` and `ry` attributes.
These attribute should always be the same if you want a circular corner.

    ...
    <rect height="60" width="100" rx="10" ry="10" fill="#B32317" />
    ...

<p style="height: 60px">
  <svg xmlns="http://www.w3.org/2000/svg">
    <rect height="60" width="100" rx="10" ry="10" fill="#B32317" />
  </svg>
</p>

Just like the `<rect>` element, SVG has a `<circle>` element. We'll use that
for the eyes.

    ...
    <circle cx="26" cy="26" r="10" fill="#000000"></circle>
    <circle cx="74" cy="26" r="10" fill="#000000"></circle>
    ...

<p style="height: 60px">
  <svg xmlns="http://www.w3.org/2000/svg">
    <circle cx="26" cy="26" r="10" fill="#000000"></circle>
    <circle cx="74" cy="26" r="10" fill="#000000"></circle>
  </svg>
</p>

As you can see, circles and rectangles have different attributes. The `cx` and
`cy` attributes are the x and y coordinates of the center of the circle,
respectively. The coordinate system has the origin at the top/left with the
x-axis pointing to the right and the y-axis pointing down. The `r` attribute is
the radius of the circle.

To keep your code more organized, SVG has a `<g>` element, similar to HTML's
`<div>`, that you can use for grouping. These elements are a great place to add
classes for styling or manipulating later.

While we're at it, let's change the `fill` attribute on the circles to `#FFFFFF`
so they will show up on the red rectangle better.

    <svg xmlns="http://www.w3.org/2000/svg">
      <g class="head">
        <rect height="60" width="100" rx="10" ry="10" fill="#B32317" />
        <g class="eyes">
          <circle cx="26" cy="26" r="10" fill="#FFFFFF"></circle>
          <circle cx="74" cy="26" r="10" fill="#FFFFFF"></circle>
        </g>
      </g>
    </svg>

Starting to look more familiar&hellip;

<p style="height: 60px">
  <svg xmlns="http://www.w3.org/2000/svg">
    <g class="head">
      <rect height="60" width="100" rx="10" ry="10" fill="#B32317" />
      <g class="eyes">
        <circle cx="26" cy="26" r="10" fill="#FFFFFF"></circle>
        <circle cx="74" cy="26" r="10" fill="#FFFFFF"></circle>
      </g>
    </g>
  </svg>
</p>

Add a couple more circles, and we've got ourselves a robot head:

    ...
    <circle cx="26" cy="26" r="7" fill="#B32317"></circle>
    <circle cx="74" cy="26" r="7" fill="#B32317"></circle>
    ...

<p style="height: 60px">
  <svg xmlns="http://www.w3.org/2000/svg">
    <g class="head">
      <rect height="60" width="100" rx="10" ry="10" fill="#B32317" />
      <g class="eyes">
        <circle cx="26" cy="26" r="10" fill="#FFFFFF"></circle>
        <circle cx="74" cy="26" r="10" fill="#FFFFFF"></circle>
        <circle cx="26" cy="26" r="6" fill="#B32317"></circle>
        <circle cx="74" cy="26" r="6" fill="#B32317"></circle>
      </g>
    </g>
  </svg>
</p>

## Working with SVG in the real world

Hand writing SVGs like this rarely makes sense. Exporting an SVG you've already
made in a vector graphics application is much quicker. An example workflow using
Sketch, an OS X-only vector graphics app:

- Select the shape or group you want to export as SVG.

![''](https://images.thoughtbot.com/building-ralph-in-svg/select.png)

- Click "Make Exportable" in the lower right corner.

![''](https://images.thoughtbot.com/building-ralph-in-svg/make-exportable.png)

- Find "SVG" in the dropdown and click "Export".

![''](https://images.thoughtbot.com/building-ralph-in-svg/dropdown.png)
![''](https://images.thoughtbot.com/building-ralph-in-svg/export.png)

Although Sketch doesn't give you insight into the code it produces, it exports
relatively clean SVG files. Next, you take the outputted code and paste it into
the page you're working on. Here is an example:

<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><g fill="none"><path d="M49.922 0C22.364 0 0 22.364 0 49.922c0 27.56 22.364 49.923 49.922 49.923 27.56 0 49.923-22.364 49.923-49.923C99.845 22.364 77.48 0 49.922 0z" id="Shape" fill="#B32317" /><path d="M36.938 52.442h-2.016v14.186c.66.814 1.086 1.822 1.086 2.946 0 1.395-.62 2.635-1.628 3.488l-1.24-1.24c.697-.504 1.124-1.318 1.124-2.248 0-1.55-1.28-2.83-2.83-2.83s-2.83 1.28-2.83 2.83c0 .93.427 1.744 1.125 2.248l-1.242 1.24c-.97-.853-1.628-2.093-1.628-3.488 0-1.124.427-2.17 1.086-2.946V44.535c.038-.853.736-1.55 1.59-1.55h40.503c.85 0 1.55.697 1.588 1.55v22.093c.698.814 1.085 1.822 1.085 2.946 0 1.395-.62 2.635-1.628 3.488l-1.24-1.24c.66-.504 1.124-1.318 1.124-2.248 0-1.55-1.28-2.83-2.83-2.83s-2.83 1.28-2.83 2.83c0 .93.426 1.744 1.124 2.248l-1.24 1.24c-1.008-.853-1.628-2.093-1.628-3.488 0-1.124.426-2.17 1.085-2.946V52.442h-2.014v29.922l1.356.776v2.44H50.426v-2.44l1.357-.776v-9.108h-4.03v9.07l1.356.775v2.443l-13.53.038v-2.44l1.32-.776.038-29.922zM49.806 20c2.83 0 5.35 1.008 6.938 2.52l1.24-1.784c-1.976-1.744-4.883-2.868-8.178-2.868-3.294 0-6.24 1.124-8.178 2.868l1.24 1.783c1.59-1.512 4.148-2.52 6.938-2.52zm.155-4.496c3.063 0 5.93.89 8.063 2.52.427.31.814.658 1.163 1.046l1.24-1.783c-2.403-2.365-6.2-3.915-10.465-3.915-4.38 0-8.255 1.59-10.658 4.07l1.24 1.783c.388-.427.815-.853 1.318-1.24 2.17-1.59 5.04-2.48 8.1-2.48zm-.154-4.34c4.884 0 9.186 1.782 11.744 4.495l1.24-1.745C59.807 10.97 55.08 9.07 49.807 9.07c-5.31 0-10 1.9-12.984 4.883l1.24 1.745c2.558-2.752 6.86-4.535 11.744-4.535zM45.698 32.054c0 .737-.582 1.357-1.357 1.357-.735 0-1.356-.62-1.356-1.356 0-.736.62-1.356 1.357-1.356.776 0 1.358.58 1.358 1.356zm14.186-7.248H39.302c-.852 0-1.55.698-1.55 1.55v13.41c0 .854.698 1.552 1.55 1.552h20.582c.852 0 1.55-.698 1.55-1.55v-13.41c0-.854-.698-1.552-1.55-1.552zM44.34 34.65c-1.433 0-2.635-1.162-2.635-2.634 0-1.435 1.163-2.636 2.636-2.636 1.435 0 2.637 1.163 2.637 2.636 0 1.472-1.163 2.635-2.636 2.635zm10.544 0c-1.434 0-2.636-1.162-2.636-2.634 0-1.435 1.163-2.636 2.636-2.636 1.434 0 2.635 1.163 2.635 2.636 0 1.472-1.202 2.635-2.636 2.635zm1.356-2.596c0 .737-.62 1.357-1.356 1.357-.737 0-1.357-.62-1.357-1.356 0-.736.582-1.356 1.357-1.356.736 0 1.356.58 1.356 1.356z" fill="#fff" /></g></svg>

[Here's a gist](https://gist.github.com/smharley/b518c91fcd85e8212fca) of that
code, so you can see what it will look like when you've exported a more complex
shape. Now that you can export SVGs from an application and have a basic
understanding of how the attributes work, you can start to modify them with
Javascript or CSS.
