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
Srcset, sizes, and picture should work with any image format the browser supports. You could also, for example, deliver WebP to browsers that support it and deliver a fallback for browsers that don't. However, in these lessons, we're going to use JPEGs, since they represent the most common use case and they'll give us the best filesize to image compression ratio.
-
0:00
There are many image formats on the web, so we should take a moment to review our
-
0:04
options before diving into responsive images in HTML.
-
0:08
You might have already learned about scalable vectors graphics otherwise
-
0:12
known as SVGs, but this is [UNKNOWN] is one of the best options for
-
0:17
delivering images that work at all pixel densities resolutions and most sizes.
-
0:23
In fact, because SVGs are just mark-up,
-
0:26
we can modify them on the fly using media queries.
-
0:29
SVGs work best for images that have large, flat areas of color.
-
0:34
For any image that has lots of rich detail,
-
0:37
such as a photograph, we will need to pick another image format.
-
0:41
For more details on SVGs, see the notes associated with this video.
-
0:47
For any image that isn't a photograph, you have a few choices.
-
0:51
There's Ping or png.
-
0:53
There's the graphics interchange format or gif.
-
0:57
And there's more common formats like jpeg.
-
0:59
And then finally, there's newer emerging formats like webp.
-
1:04
In general jpegs tends to work best for photographs.
-
1:07
In the rare case that we need an image to be animated,
-
1:11
we can use an animated gif and additionally,
-
1:14
if we need full alpha transparency on an image, we can use a 24-bit png.
-
1:19
However, just know that gifs and pngs have a tendency to
-
1:22
increase load times due to their typically higher file sizes.
-
1:27
JPEG is designed for digital photography,
-
1:30
which is why it compresses photographs and detailed artwork so well.
-
1:36
Source set sizes and
-
1:38
picture should work with any image format the browser supports.
-
1:42
We could also, for example, deliver WebP to browsers that support it and
-
1:47
deliver a fall-back for browsers that don't.
-
1:51
However, in these lessons, we're going to use .jpgs since and
-
1:54
they represent the most common use case, and
-
1:58
they'll give us the best file size to image compression ratio.
-
2:02
Now, when working with .jpgs, there is a trick we can use to get
-
2:06
a small file size on images that are at 2X pixel density.
-
2:12
We'll talk about how to properly deliver these images next.
-
2:15
But, first let's take a look at these in an image editing tool, like Photoshop.
-
2:22
So, here I have an image open in In Photoshop, and
-
2:26
you'll be able to find this image in the workspaces associated with this video.
-
2:30
So we'll take a look at that soon.
-
2:32
But first I just wanted to show you this little compression trick that you can use
-
2:36
for 2X images.
-
2:38
So this is a fairly high resolution image.
-
2:41
I'll zoom in here.
-
2:42
So this is at 100%, and you can see that there's a lot of find detail here and
-
2:49
it would be nice to have this at 2x resolution and
-
2:53
still get a good file size to compression ratio and
-
2:58
the thing about 2x images is that you can't really see
-
3:02
jpeg artifacts as much as you can in 1x images, so
-
3:07
I'm going to go up here to file, and I'm going to go to save for web.
-
3:13
And this will bring up the save for web dialogue box.
-
3:19
And you can scroll around and pan the image here just to see what it looks like.
-
3:24
And over here, I have the jpeg preset.
-
3:28
And I'm going to set the quality as low as possible here.
-
3:34
And now you might not be able to see this perfectly, but basically there's a lot
-
3:40
of JPEG compression artifacts going on in this particular image.
-
3:45
So I'm going to bump this up a little bit to about 20% here.
-
3:50
And, actually, maybe even go a little higher, maybe like 30 or 40.
-
3:54
Let's try 40.
-
3:56
Here we go.
-
3:57
Now, there are still some JPEG compression artifacts here,
-
4:02
but the file size is pretty good.
-
4:05
This is about 300K for an image that is 2048
-
4:10
pixels across by 1366 pixels in height.
-
4:16
And that's a pretty good file size to compression ratio,
-
4:22
but like I said, there's a lot of JPEG compression artifacts.
-
4:25
However, when this is actually viewed at 2x resolution on a webpage, and
-
4:30
it's shrunk down, so maybe I can zoom out here to show you what that looks like.
-
4:35
You'll see something that looks more like this.
-
4:38
So, you'll see all of the crisp detail on a high DPI display, but
-
4:43
because of the scale of the JPEG artifacts you won't really see those.
-
4:49
So, basically, you can get away with much higher compression on 2 x images.
You need to sign up for Treehouse in order to download course files.
Sign up