Lesson 1 · Unit 02
Fewer bytes
Compress text, right-size images, skip what is off screen
What you practise
- Compression, minifying, image formats and dimensions, responsive images with
srcset, lazy loading
Try a question from this lesson
Pick one
A photo is 2000 × 1500 pixels. The page shows it in a box 400 × 300 CSS pixels on an ordinary screen. How much of the download is never seen?
Hint
Compare pixel counts, not widths: both sides are too long.
Correct answer
About 96%: it has 25 times the pixels needed
Why
2000 × 1500 is 3,000,000 pixels; the box needs 120,000. Five times too wide and five times too tall is 25 times the pixels, so 24 of every 25 are downloaded and then scaled away. The browser cannot ask a server for part of an image.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Three ways to send less
Minify: remove spaces, comments and long local names from code. It runs the same and is smaller.
Compress: the server packs text responses with gzip or Brotli and the browser unpacks them. The browser says which methods it knows, the server says which one it used.
Right-size images: no more pixels than the screen will show, in a format made for the content.
function area(width, height) {
// rectangle only
return width * height;
}
// the same function after minifying:
function areaMin(w,h){return w*h}Concept
One image, several widths
srcset lists the same picture in several files and gives each one's width in pixels (800w). sizes says how wide the image will be drawn, here the full viewport width.
The browser multiplies the drawn width by the screen's pixel density and picks the smallest file that covers it. A small phone gets a small file, a sharp wide screen a large one, from the same HTML.
<img src="lamp-800.jpg"
srcset="lamp-400.jpg 400w,
lamp-800.jpg 800w,
lamp-1600.jpg 1600w"
sizes="100vw" alt="Lamp">10 more exercises in the app
- 3× multiple choice
- 2× fill the blanks
- 2× type the answer
- 2× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.