Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Web Design
  2. SVG
Webdesign

How to Create Responsive SVG Images

by
Difficulty:BeginnerLength:ShortLanguages:

In this tutorial I’ll explain how to create responsive SVG images; more specifically, I’ll explain how to create SVG logos, icons, and images, which change depending on the screen they’re displayed with.

Responsive SVG Images

Let’s begin with a demo. Here’s what we’re working towards (check out the full screen version for maximum effect):

If you resize the container (hat tip to Rick Strahl for his jQuery Resizable plugin) or the browser window, you’ll see the SVG respond.

What We’re Doing

Follow the video to learn how to: 

  • Create this logo in Adobe XD
  • Export the SVG assets
  • Clean up and optimize the SVG using SVGOMG.

Once you have your SVG (download the optimized assets here if you like) the following steps describe how you make a responsive logo.

1. Hand Code the SVG Element

We’re going to hand code the beginnings of this SVG, pasting in pieces of our logo assets wherever needed. Begin by opening an SVG element.

Note: we need to include these peculiar namespace strings so that we can use href attributes etc. further down the line.

2. Define Some Symbols

Nested inside our svg we need to define some symbols; each of our components will have its own symbol. Here’s what a symbol looks like, including, in this case, a viewBox of 60x60 (matching the dimensions of our icon).

Now we paste the contents of our SVG icon within the symbol element, but only the path elements for the flag and the castle. We end up with:

Repeat this process of creating a symbol for the “type” SVG too.

3. Define Symbols For the Logo Versions

We now need to define more symbols; one for each version of the logo we designed previously (the landscape, and the full version).

logo versions in Adobe XD

These symbols are created just like before, making sure that the viewBox is the correct size in each case. For example:

The main difference comes now: instead of pasting in the svg path contents directly, we’re going to place use elements referencing our previous symbol parts by their id, like so:

We can get the x, y, width, and height values from Adobe XD (or wherever you designed the icons). Here you can see the position and dimension values for the text icon:

position and dimensions

You should end up with something like this–the two versions as symbols:

4. Reveal the Symbols in Your Browser

We have a whole bunch of code now, but if you open the SVG in your browser you won’t see anything. We’ve defined our symbols, but we still need to actually use them. Let’s directly place three use elements on our canvas; one for each version:

If you now open your SVG in a browser you’ll see all three logos positioned in the same place, stacked on top of one another. 

5. Display Icons Selectively Using Media Queries

We’ll use media queries to display each of our icons individually, depending on the screen size, and we can do that directly within our SVG file.

After the opening svg tag add some style tags:

Within them, add some media queries targeting the icons via the class names we just assigned them.

  • Here we begin by hiding the landscape and full versions by default. 
  • Then at screens of at least 500px we hide the square and reveal the landscape
  • Then at screens of at least 1200px we keep the square hidden, hide the landscape, and reveal the full version.

Conclusion

And we’re done! Save the SVG file and you’ll be able to use it just like a normal image (with an img element). Don’t forget you can check out the full screen version to get a full impression of the responsive effect we’ve created.

Learn More

Related Links

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.