- [Instructor] The last piece of our responsive images puzzle is the sizes attribute. If you're not familiar with the sizes attribute already, here's a two minute primer. The sizes attribute allows us to tell the browser how wide an image is depending on the width of the full viewport. So for example, this top image, the showcase image, will always be the full width of the viewport, so 100 vw. The images down here that I've labeled the reason images on the other hand, right now they're about 372 pixels wide and they'll stay that width for all wider screens.
But if I reduce the width of the screen down to below 800 pixels, all of the sudden they become full-width. So, using the sizes attribute, you can tell the browser 'Hey, if the viewport is narrower than 800 pixels, then this image will be full width. If it's any wider than 800 pixels, the image will never be wider than 372 pixels.' Then we can do the same for the feature images, whose width will be max 558 pixels, and the story images at the very bottom whose width will be a max of 670 pixels.
If you want to know more about responsive images, sizes, sources, and everything else, go check out my course responsive images. To generate the sizes attribute for each of our images, we first need to know what type of image we are displaying. That's why I've added this data type attribute. Here I've marked all my showcase images with showcase, all my reason images with reason, feature images with feature, and story images with story. That makes it easy for me to apply the correct sizes attribute to the correct image.
We've already captured this information in the let called type, this captures the data type for each of the images. At the very top of my document I'm now going to create an object and have each of the properties of that object map to these different sizes. Showcase, reason, feature, and story, and then the value of each of the properties will be the actual size's attribute. This will make it really easy to apply it later on. So I'll set up a constant, call it sizes, and inside sizes I'll set up the properties.
So first showcase, and for showcase the sizes attribute will just be 100 vw. For reason, the sizes attribute will have a media query max width 799 pixels, that means as long as the width is narrower than 800 pixels then the width of the image will be 100 vw, full width, otherwise it'll be 372 pixels.
Then we have feature. And feature starts exactly the same, so I'll just copy this out. And then change the last value, which is 558, and finally we have story. And here we end up at 670. Now we just have to grab our type datatype and use it to pull out the correct property from our sizes object.
Now remember previously in the course where we talked about dot versus bracket notation? This is an example of where that kicks in. So let's say I set up a lets named sizes and I want to assign to it the sizes and then the property that matches the attribute that we have here. The problem is I captured that attribute in the type lets and if I say dot type, then we're looking for a property inside sizes named type.
That's not what we're looking for. So instead, I'll wrap type in square brackets. This will allow type to be filled in with the actual data type from the image, and in return we get the actual sizes attribute. Now we can test that by console logging out sizes, save script.js, go back in the browser, and here you see now each of my images has both the source set attribute and my sizes attribute, and if I scrolled up you'll see the sizes attributes are different depending on what type of image it is.
That means all my pieces are in place and the only thing left to do is stitch everything together. That's next.
Author
Updated
4/1/2019Released
5/17/2017Through practical examples and mini-projects, this course helps you build your understanding of JavaScript piece by piece, from core principles like variables, data types, conditionals, and functions through advanced topics including loops, closures, and DOM scripting. Along the way, you will also be introduced to some ES6 and the basics of JavaScript libraries.
- What is JavaScript?
- Working with data
- Using functions and objects
- Working with JavaScript and the DOM
- Changing DOM elements
- Handling events
- Working with loops
- Making images responsive using markup
- Troubleshooting code
- Validating functionality
- Minifying JavaScript
Skill Level Beginner
Duration
Views
Related Courses
-
Introduction
-
Welcome1m 7s
-
-
1. JavaScript: An Introduction
-
What is JavaScript?2m 38s
-
-
2. The Basics
-
Introducing the browser console10m 31s
-
3. Working with data
-
Data types in JavaScript4m 2s
-
Arrays2m 20s
-
4. Functions and Objects
-
Functions in JavaScript3m 28s
-
Build a basic function3m 29s
-
Anonymous functions5m 11s
-
Variable scope3m 17s
-
ES2015: let and const6m 12s
-
Make sense of objects3m 19s
-
Object constructors6m 16s
-
Closures8m 11s
-
-
5. JavaScript and the DOM, Part 1: Changing DOM Elements
-
Access and change elements4m 33s
-
Access and change classes3m 45s
-
Access and change attributes4m 53s
-
Add DOM elements6m 56s
-
6. Project: Create an Analog Clock
-
Use CSS to move clock hands3m 49s
-
7. JavaScript and the DOM, Part 2: Events
-
What are DOM events?1m 31s
-
Some typical DOM events1m 59s
-
Add and use event listeners6m 51s
-
-
8. Project: Typing Speed Tester
-
Rundown of HTML markup2m 58s
-
Build a count-up timer5m 56s
-
Add a reset button5m 3s
-
-
9. Loops
-
Loops3m 37s
-
Looping through arrays4m 7s
-
Break and continue loops7m 9s
-
-
10. Project: Automated Responsive Images Markup
-
Project breakdown1m 55s
-
Rundown of project setup3m 26s
-
-
11. Troubleshooting, Validating, and Minifying JavaScript
-
Troubleshooting JavaScript7m 20s
-
Online script linting5m 57s
-
Automate script linting8m 24s
-
Online script minification2m 50s
-
Automate script minification2m 24s
-
12. Bonus Chapter: Ask the Instructor
-
What are arrow functions?3m 11s
-
What does the % symbol do?3m 47s
-
-
Conclusion
-
Next Steps1m 55s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Create object containing different sizes values