02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Optimizing based on device resolution

When it comes to providing the best experience to users and cutting down on costs of bandwidth on your server nothing comes close in importance to the topic we will cover in this lecture. Instead of reusing assets on all screens a better strategy would be to provide content based on resolution. In this lecture we will cover this topic and more.

Keynotes:

  • Why resolution matters.
  • How to work with the resolution media queries.
  • Planning a resolution strategy that is effective.

Properties:

adoptive design, resolution

Video Transcript

00:00Alright, let's start our lecture and in this lecture we're really talking about resolution.
00:06And image resolution is really, so far we ignored images and assumed that there are
00:10no images in your project, but really in a real world application you have to configure
00:15and figure things out for your images.
00:18So in this lecture we're going to kind of talk about why you need to think about images
00:22and how do you configure them.
00:24Best practices currently today, we're also going to talk about best practices towards
00:29the future and we're going to be talking about far reaching future that isn't
00:33available yet, just you are kind of familiar with.
00:37So we're going to work with the resolution and with media course to
00:40configure how do we set a background image and that's really going to be our
00:44gateway to any image in general.
00:46And we're going to plan a strategy and we're going to talk about the different
00:49things that you could do.
00:51So how about we get started.
00:53I'm going to go ahead and write into my source files and follow along with me
00:57because you're going to find in the source files inside of the bg folder,
01:00inside of the image folder, three images and different resolutions of a width of
01:05400, 800 or 1200 pixels.
01:08And I'm going to go ahead and I'm not going to put a background into my body.
01:11And the reason is very simple.
01:13I do not want to load any image whatsoever until I configure some sort of rule.
01:18And the logic here is beautiful because if your browser is an older browser,
01:22then we're just not going to give them the image at all.
01:24Let's not put too much weight onto a browser that just doesn't support media
01:28queries.
01:29If the browser supports media queries, let's go into the media and I'm going to
01:34go ahead and just copy here the body tag, the body information that I'd want to
01:38put inside here, which is just a traditional image configuration.
01:42So literally all I'm doing here is I'm setting a background image and I'm
01:45setting its width and height to be a hundred percent.
01:48Now I'm using the large image.
01:49So if we're more than 900 pixels, I'm using the large image and I'm scaling.
01:53Notice how in this case, I'm not creating also an image for ultra high.
01:59You know, if you have one of those retina displays or hyper HD or whatever
02:05or not, it's not going to be really great.
02:07So you might want to configure images in different formats and
02:10configure it based on different resolutions.
02:13But if I go ahead and I click on refresh, we'll see that automatically we're
02:16getting that image and notice that red squiggly line there.
02:20I put that deliberately there.
02:21So it'll be easy for us to recognize that that is the image that we're using.
02:25But I'm going to go ahead and continue.
02:27And what I want to do is if my screen is lower than 900 pixels, I'm going to
02:31go ahead and just copy here a new body tag, but this time around in a case
02:36where in case we're a 900 or below, I'm going to serve our 800 pixel image.
02:42In a case where our image is above underneath 400, I'll just go ahead here
02:47and just let me just copy this and I'm going to serve up the 400 pixel item.
02:53So let me just set that to be 400.
02:55And that's really all I had to do to create that specificality.
02:58But there's a lot of copy pasting involved.
03:01So when I'm doing these these variations and I go ahead and I resize,
03:06let me just make sure I saved it and I click on refresh and I resize my image.
03:09You'll see that at some point the image is replaced with the 800 pixel image.
03:14And at some point the image is replaced with the 400 pixel image.
03:19And we could see that the image will automatically be replaced, but
03:21really it's only going to load whatever it needs.
03:25So if our browser was only and it was from the beginning was only in this size,
03:28then we're never going to load another image.
03:31And that is why we don't want to put anything inside of our media queries
03:34that no background if it's not inside of a media query.
03:37It also solves the problem of well, if it's old browser,
03:39let's not push on it too much information.
03:42Now before I move on into the next topic that relates to images,
03:47I want to talk about a future resolution that will come at some point.
03:50We just don't know yet where and go into the source files again into the CSS
03:55measurement types, scroll all the way to the bottom and
03:57just find the optimizing images based on resolution with CSS image sets.
04:02If you click into it, you're going to get into the W3C specification.
04:09And in the specification you see this is a very early draft
04:12that it's talking about a possibility.
04:14And I'm just going to show here just the sample itself of adding a background image
04:18with image sets.
04:19And really, I'm not going to get into the technicalities of it because it's not
04:22supported really.
04:23And even if it is supported by anyone,
04:25it is going to be probably going to change a lot because it's not really
04:28configured. It's a very early draft really as we speak.
04:31So I would definitely not implement it at this stage, but
04:34this is a glimpse into how it's going to look like in the future.
04:38We'll just list out our different images.
04:40So I do encourage you, if you're watching this video six months in or
04:43you're watching this video at any other point, go ahead into that link,
04:46check to see what is the current version.
04:49Is there a specification that has been updated?
04:51And obviously, go and check Oto on the site.
04:56Can I use and see what is the support level?
04:58We're not going to get into that at this stage because it's just not
05:01supported enough for it to be worth diving deep into it.
05:04But I wanted you to be familiar with it.
05:06So with that said, I want to go back into showing one more thing that is
05:09still an early draft, but a little bit more supported and saying that
05:13we could start using right away.
05:15Now, I'm going to go into my HTML and what I want to do is I want to add
05:18an image right above our references in line 32.
05:22And I'm going to go ahead and just copy here a code that I already
05:25pre-created and I'll just walk through it and explain it.
05:28So basically, I created here an image.
05:30I set an initial source that of our 400 pixel width image.
05:34And I just resize it so we could see everything on our screen.
05:37But I also set here a new property which is called source set that
05:41enables me to specify different images with different resolutions.
05:45So in this case, notice that we have here another image that's 800 pixels
05:48and another image that is 1200 pixels.
05:51And I could go ahead here and list out all the images.
05:53So I could go ahead here and also put in also that 400 pixel one.
05:57If I wanted to, I could have put here just a gray item or
06:00saying that really wouldn't take almost anything like a one pixel and
06:03stretch it in and wait for the right image to load giving the browser.
06:07And that's really what we're doing.
06:08We're letting the browser decide what image is most appropriate,
06:12literally without telling him much more than that.
06:14The idea behind it is not only based on what is specifically best
06:18optimized right now, but also if you're running on a mobile device,
06:21maybe it's best to serve the lowest quality image if you're running on your
06:25mobile because you want to shorten the amount of gigabytes used on your plan.
06:30And if you're running on Wi-Fi then how about we just go and
06:32configure it or maybe the user in the future will build it,
06:35configure to on high res images, low res images.
06:38So this type of feature, even if it's not widely supported right now,
06:42is really, I really encourage you to use it because any browser that does support
06:47it will get it and it's going to be a much better experience for those users.
06:51It's going to, if you could load up a site much faster, better.
06:54Now obviously at this stage you're not going to put here an image that is
06:56irrelevant because there isn't enough browser support.
07:00So you could put there the lowest level quality image and
07:03then upscale based on the support that is available.
07:07So if I go ahead here and I just click on save and I click on refresh,
07:10then you'll see that now we're going to have your O2N image that is optimized for
07:131200 pixels.
07:15And if I go ahead and resize my screen and this time around I need to click on
07:18refresh, we'll see that now it's going to configure it to different size,
07:21a different size based on the specifically that the browser thinks is right.
07:26Now again, it is not 100% bug free this feature yet and it's still in growth.
07:30But yet if you're going to give something that is a little bit better to the user,
07:34that's always better and it's not going to break the experience.
07:38So that is a huge plus so you could start using it right away,
07:41even if it's not a final part of any specification yet and it's an earlier
07:45draft.
07:46So that's it about images and optimization and optimizing your images really
07:50for devices.
07:52And in the next lecture,
07:53what we're going to do is we're done with this topic obviously,
07:56and we're going to get close to basically wrapping our topics,
07:59moving to two last topics where in the next lecture,
08:02which won't be too long,
08:04we're going to talk about designing by aspect ratio,
08:06which is going to take us back into media queries and we're going to see where is
08:10that appropriate.
08:11So we'll see in the next lecture when we talk about aspect ratios.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!