Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
When working with embedded SVGs and styling them with CSS, it's possible to add media queries and create responsive images. This is helpful for SVGs that contain lots of fine detail that might not look good at smaller sizes. In a mobile-first approach, the detail is first removed, and then added back in along the spectrum of larger screen sizes.
Resources
Browser Compatibility
Browser support for SVGs is generally good. However, there are a few issues to be aware of in Internet Explorer, particularly that SVGs are only supported in IE 9+. Read more about SVG browser support at caniuse.com.
SVG and Illustrator Files
Here are the SVG and Adobe Illustrator files used in this course. If you're using Workspaces with this course, there is no need to download these files; they are just listed here in case you prefer to use another text editor.
Free Vector Drawing Tools
If you're looking for an alternative to Adobe Illustrator, here are some free vector drawing tools that work in a web browser. All of them should be able to export to the SVG format.
Related Courses
-
0:00
We've already seen how an SVG can play a role in responsive design.
-
0:06
They always look sharp regardless of screen size or resolution.
-
0:10
However, if we embed an SVG into our HTML,
-
0:14
it's possible to apply some additional styling at different resolutions.
-
0:19
When an SVG is at a small size, we may want to increase the width of strokes and
-
0:25
show less detail so it looks more like an icon.
-
0:29
When an SVG is shown on a larger screen,
-
0:32
we should show more detail and allow the stroke widths to relax a little bit.
-
0:38
So, let's try it out.
-
0:41
Here I have my workspace open, and there's a path here that is
-
0:46
actually going to be the star itself in our graphic.
-
0:50
So, I'm going to change this class from star to graphic.
-
0:59
And we already have our outer-circle selected.
-
1:02
Now, this next circle is actually the inner-circle.
-
1:12
So, if we switch back to our SVG, we have our outer circle here and
-
1:17
that has a stroke on it.
-
1:19
But then we also have this inner circle that sort of surrounds this star, and
-
1:24
that has a stroke on it as well.
-
1:26
So, next we have a path and then we have another path.
-
1:31
Now, this second path is our star, so I'm going to say class star.
-
1:39
But what is this first path?
-
1:41
Well, if we switch over to our graphic, we have this star and that's one path.
-
1:48
But we also have these two little lines on each side,
-
1:52
and that's actually the first path in our SVG.
-
1:57
So, let's style that.
-
2:00
By saying class outer-circle, so
-
2:04
we'll use the same class that we used previously, and even though it's not quite
-
2:10
the same thing, we'll still be using it in a similar fashion in our style sheet.
-
2:16
Now we have a bunch of circles here, and we can sort of
-
2:21
figure out which part is what in our SVG by counting them.
-
2:26
So, there's one, two, three, four, five, six, seven, eight,
-
2:31
nine, ten circles here, all in succession.
-
2:37
And if we look at our star here, there's ten circles there as well.
-
2:43
So that gives a clue that oh, those circles must be the ones here, or
-
2:48
at least it's very likely.
-
2:50
And that's sort of one way that you can figure out which parts of
-
2:53
your SVG correspond to what part of the image.
-
2:57
So, let's go back here, and
-
2:59
we're just gonna style all of these circles with the class star-point.
-
3:05
[BLANK_AUDIO]
-
3:07
So, let me copy that to my clipboard with a space, and
-
3:11
I'm just going to paste it into each one of these circles here.
-
3:16
So let's just go down the list.
-
3:18
[BLANK_AUDIO]
-
3:22
And paste those in.
-
3:24
And now we're ready to jump over to our CSS.
-
3:28
I'm just gonna make some room to work here, so let me scroll down.
-
3:34
There we go.
-
3:36
And we've created that first class, graphic, and
-
3:39
that's selecting our SVG element.
-
3:43
And I'm going to give that very similar styles to what we had previously,
-
3:48
margin 1em on the top and bottom, and auto on the left and right sides.
-
3:54
I'm actually gonna give this a width of 30%.
-
3:58
And we need to set this to display block because, by default,
-
4:02
it should be an inline element and we want it to be a block-level element.
-
4:08
So let's save that out, and switch over to our workspace and refresh.
-
4:12
And as you could see, it's now taking up 30% of the width of its parent element.
-
4:19
So now, we need to style some of those individual components.
-
4:25
We're going to use a mobile first approach.
-
4:28
And at the smallest size, I want these star points and
-
4:34
the outer circle to just disappear entirely, so we'll say opacity 0.
-
4:42
I also want the inner circle to
-
4:47
have a stroke-opacity of 0.
-
4:53
[BLANK_AUDIO]
-
4:56
And then i want the star to
-
5:01
have a stroke-width of 8.
-
5:08
And you'll see here that I'm using some properties that even Workspaces doesn't
-
5:14
recognize, but these are CSS properties that will be applied to our SVG.
-
5:21
And you can learn more about these by looking at some of
-
5:24
the attributes in the SVG specification.
-
5:29
So, let's save that out, and we'll switch over to our preview, and
-
5:34
let's see what this looks like now.
-
5:37
So you'll notice that our graphic is much more like an icon now.
-
5:41
A lot of the detail has sort of gone away.
-
5:45
And if I come out of full screen here and resize the browser, you'll see
-
5:51
that at a small size, this actually looks pretty good, and that's what we want.
-
5:57
However, when we get back up to these larger sizes, we want more detail, so
-
6:02
let's try to add some of that detail back in.
-
6:06
So to do that, I'm going to scroll down here and I'm going to write a media query.
-
6:13
I'll say at media only screen and
-
6:18
I'll set the minimum width to 30 ems.
-
6:23
And in general, that will target large phones and small tablets.
-
6:31
And we'll say inner-circle.
-
6:33
[BLANK_AUDIO]
-
6:36
And remember we set that to an opacity of 0, so we're gonna bring that back in.
-
6:40
We'll say stroke-opacity 1.
-
6:44
Then I'm going to create another media query here.
-
6:48
And I'm just going to copy and
-
6:49
paste the one that I had previously, and just change the size.
-
6:54
So we'll say maybe 40 ems here, and that will be roughly tablet-sized.
-
7:00
And we'll say outer-circle.
-
7:01
[BLANK_AUDIO]
-
7:04
And on that outer-circle, we'll set the opacity to 1,
-
7:12
and we'll give the star its original stroke width of 4.
-
7:19
And then finally, we will add one more media query here, and
-
7:25
this would be for larger screens like, say, laptops or
-
7:29
desktops, and we will put that at about 64 ems.
-
7:34
And then we will bring back in the star points at this larger size,
-
7:42
and for that, we'll say opacity 1.
-
7:47
So, if we save all of that out, and
-
7:50
I'll start out with the browser at a small size here, we can refresh that page.
-
7:55
And at first, we won't see anything happen, but when we resize the browser,
-
8:01
we'll hit that first media query and start bringing back in some of that detail.
-
8:07
And then we'll hit the next one and bring back in the star points.
-
8:12
So you can see how this sort of evolves at different sizes.
-
8:20
Making an SVG responsive is a fairly advanced technique, and
-
8:24
in most cases, it's not necessary.
-
8:27
However, it's a great idea to use it on a website logo or
-
8:31
other very important images.
-
8:33
As with anything technical, there's always more to learn, so
-
8:37
be sure to read the documentation and experiment.
You need to sign up for Treehouse in order to download course files.
Sign up