Mozilla: Shaving 2.2 seconds off page load time increased downloads by 15.4%.
Yahoo: 400 ms resulted in a 5 to 9% drop in traffic.
For the typical website, images are probably the asset that takes up the most bandwidth. Therefore, the choices you make with your images could make or break your performance.
Images come in different formats. You've probably already noticed that image files can have different file extensions. Each format has some things they're good at, and some things they're not so good at. It depends what it's an image of, and what you want to use it for!
Everybody loves rich, full-colour photographic images.
This is where the JPEG format shines. It uses "lossy compression" to deliver reasonably lightweight images that look good (even if some amount of detail is lost).
Images in JPEG format are saved with the file extension .jpg.
Alternatively, there is a new images format that does well with photographic detail at an even small size. This image format is called WEBP. The drawbacks of webp are that it isn't supported in older browsers, or by most image-viewing software outside the browser, and converting to webp can be challenging.
Want a lossless image format to preserve detail, like fine text, or a detailed screenshot? (Not that you would put relevant text content in an image! But we'll get to that later.)
You want the Portable Network Graphics format.
Want parts of your image to be partially or entirely see-through?
I love SVG. They are tiny - this elephant image is 1/100th the size of the JPEG example (3kb vs 305kb before compression).
SVGs are great anytime you need simple shapes and colours. They have a ton of cool features - you can create animations with them Opens in a new window and change them programmatically using CSS and/or javascript, they never pixellate so they're great for responsiveness, and they're actually made of markup code, just like HTML!
Obviously, they aren't capable of photo-realism, but they are great at what they do.
GIFs and PNG (when they're in 8-bit format) are also good choices for limited colours - they both come in small sizes when they only use 256 colours.
And now for the old debate - is it pronounced "gif", or "gif"?
I hate to be the one to break it to you, but all your favourite gifs aren't gifs at all. They're videos. Look at the above file sizes (and compare the qualities) to see why.
Twitter, Pinterest, Imgur, even /r/highQualityGifs, they all deliver video in place of gifs.
And how do we put a video on our page?
Image optimization isn't as fancy as it sounds. It just means using images that aren't bigger than the size at which anyone will see them, and using the right amount of compression - not too much, not too little.
To resize images, you'll need image editing software.
You should be able to use the Adobe Suite via Humber's appsteam service Opens in a new window. Otherwise, GIMP Opens in a new window is a free, open-source alternative.
But the functionality we need is pretty simple compared to what those photo editing suites offer. If you don't already have a photo editor you like installed on your computer, Adobe has a free online editor called Photoshop Express Opens in a new window that does everything we need for our purposes, including resizing images and compressing them.
So consider the largest your image will need to be - if you're going to use it to fill the width of your main column, and your main column is 960px wide, your image should be 960px in width!
<picture>
element Opens in a new window. However, it's a deep-dive that we don't have time for today. If you want to know more, I highly recommend Jason Grigsby's Responsive Images 1010 Opens in a new windowBalancing image resolution with file size isn't an exact science. For one thing, it depends on the image!
If it's a well-lit photograph of primary-coloured blocks, that's going to compress really nicely. Gradients, on the other hand, don't tend to fare so well, as they're the first thing you notice once the number of colours starts to drop from many millions to hundreds of thousands to hundreds.
Notice that there is a huge saving on the file size between Level 100 and Level 80, with diminishing returns the further down you go. Judge for yourself when you start caring about the loss in image quality. For me it's > Level 30, but < Level 80.
You can actually get a lot more savings with tools beyond just exporting from your image editor, but most of these tools are command-line based and/or should be built into your development workflow.
That being said, I've struggled to get better results than those offered by a free, drag-and-drop website called TinyPNG Opens in a new window, which, despite the name, works for both PNG and JPEG formats.
It does a really remarkable job, and I use it often for small projects!
Remember how I mentioned earlier that you would never put relevant text content in an image?
There's a good reason for that! Computers suck at reading text embedded in images, and Google doesn't try. If your text content matters, it shouldn't be hidden away in an image, or an alt attribute. That would be very bad for both accessibility and SEO.
If you're going to overlay text on an image that conveys information, you're going to have to use the CSS positioning & layering techniques we talked about in week 5 in order to move your text on top of your image.
However, if your image conveys no information, then we can use the CSS property background-image
. The reason I make this distinction is because background images are not accessible - you can't put an alt attribute on them to supply a text alternative.
You're already familiar with the CSS "background" property, but there are several more properties that affect the background of a block element.
.my-class {
background: blue;
background-image: url('/images/my-image.jpg');
}
The "background-image" property accepts a url pointing to a an image file. Notice that the file path is wrapped in the url()
function - it won't work without that.
Luckily, if you forgot it, or if the link to the image was broken, or if the image was in a format that the browser didn't support, the elements with your class would still get the background color you defined.
One of the very cool things about CSS is that if you have an invalid or unsupported property, it doesn't throw an error - it just ignores it and moves on to the next thing. This means it's really easy to create fallbacks like this!
Go ahead, use that fancy new property that only works in one browser - when it doesn't work, you've got a backup declared.
This is also known as "progressive enhancement" - letting cool new features fail silently in places that they aren't supported, with reliable backups in place.
You can also set the background-size
, background-position
, and background-repeat
properties.
Use these for all your poetry backdrops!
Okay, I know the exercises are ungraded, but I strongly suggest that everyone completes this, because next week's lab will build off of this. In other words, do this now otherwise you'll need to do it all next week.
Your deliverables for this lab: a zipped folder with 1) your copy deck and 2) image assets.