Skip to content

Instantly share code, notes, and snippets.

@ashx3s
Created October 4, 2021 04:52
Show Gist options
  • Save ashx3s/fff897237bcd0427183c76eee9483404 to your computer and use it in GitHub Desktop.
Save ashx3s/fff897237bcd0427183c76eee9483404 to your computer and use it in GitHub Desktop.
How to Use Srcset and Sizes

Srcset, Sizes, and Image Optimization

  • srcset is an html attribute that identifies which image to render from a set depending on specified circumstances.
  • use srcset and sizes together to serve web content that renders best for the user's device.

Srcset

'Here are my images, pick whichever one you think is best'

General Theory

  • Pixels render differently depending on the screen size
  • This syntax makes use of saving multiple sizes of the same image in full, lg, md, sm directories
  • Use a logical naming system that includes same title and a distinction between them
    • you can use lg, md, sm (But if you have lots of image sizes this could become difficult later)
  • When using chrome, the browser will cache the high res version of the image once it serves that and the other ones wont be needed anymore.
  • Important This is particularly important for the initial load on different sized screens.
  • Note that this example follows a mobile first approach

Methods

  • To create a srcset, define which version to use per display size
<img src="path-to-img/img-small.jpg"
  srcset="path-to-first-img/img-small.jpg 320w, path-to-second-img/img-medium.jpg 800w, path-to-third-img/img-large.jpg 1200w" 
  alt="describe image">
  • Check your image sizes and be ready to play with your compression a bit as finding a good balance between quality and weight is important.
  • in your html, use the 200w syntax for widths
    • this helps improve speed because when the browser loads the image, these numbers + w will tell it how much space to save while the browser displays all the content

A note on 1x, 2x, 3x syntax

  • you can replace overt widths as shown above with 1x, 2x, 3x
    • this will calculate where

Sizes

'At this viewport size I want an image slot this wide`

General Theory

  • This is used to specify the size range of image versions
  • With your sizes, you essentially add media query breakpoints into your markup
  • Use this alongside srcset
  • sizes specifies width for image layout, this is not the same as srcset - srcset is the real img width. Sizes is for creating a placeholder for the image to be loaded into
  • You cannot use percentages
  • Don't use sizes to serve specific images. That is handled by srcset
  • Important The browser will usually pick srcset's smallest image that is wider than the sizes slot.

Method

  • add sizes attribute after your srcset
  • set your breakpoints and widths
<img src="path/img-sm.webp"
  srcset="path/img-sm.webp 320w,
    path/img-md.webp 800w,
    path/img-lg.webp 1200w"
  sizes="(min-width: 960px) 80vw,
         (min-width: 640px) 90vw,
         100vw"
  alt="img description>

Syntax

  • sizes="(breakpoint: value) value"
  • you can set multiple breakpoints

Logic Notes

  • In this example the sizes declaration translate to:
    • If the viewport is at least 960 pixels wide, then make the slot for the image 80vw
    • Else If the viewport is at least 640 pixels wide, then make the slot for the image 90vw
    • Else make the slow for the image 100vw
  • This will effect the img selection
    • As the slot is 80vw at 960px, the actual px size of the slot will be closer to 860px
    • And the breakpoint at 640px slot will be closer to 570px
  • This means that
    • if the slot is at 810px, img-lg will be used
    • if the slot is 780px, img-md will be used
    • as long as the slot is img-sm will be used

Attributions

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Raster Image Optimization</title>
<!-- Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Oswald:wght@500;600;700&family=Source+Sans+Pro:ital,wght@0,300;0,400;0,600;1,400&display=swap"
rel="stylesheet">
<!-- Stylesheets -->
<link rel="stylesheet" href="assets/css/reset.css">
<link rel="stylesheet" href="assets/css/fonts.css">
<link rel="stylesheet" href="assets/css/main.css">
</head>
<body>
<main>
<header>
<h1>Image Optimization and Display Examination</h1>
<p>Fork this repo and start hacking</p>
<hr>
</header>
<section>
<header>
<h2>Using just srcset</h2>
</header>
<img src="assets/images/sm/penguin-family-500.jpg" alt="A baby and adult penguin among other penguins" srcset="assets/images/sm/penguin-family-500.jpg 500w,
assets/images/md/penguin-family-800.jpg 800w,
assets/images/lg/penguin-family-1800.jpg 1500w" loading="lazy">
</section>
<section>
<header>
<h2>Using sizes and srcset together</h2>
</header>
<img src="assets/images/sm/penguin-family-500.webp" alt="family of penguins" sizes="(min-width: 960px) 80vw,
(min-width: 640px) 90vw,
100vw" srcset="assets/images/sm/penguin-family-500.webp 500w,
assets/images/md/penguin-family-800.webp 800w,
assets/images/lg/penguin-family-1800.webp 1200w">
</section>
<section>
<header>
<h2>Things to test with these images</h2>
</header>
<ul>
<li>Visually test how the images are loaded by changing the browser width</li>
<li>Change the widths in srcset to see how things render at different sizes</li>
<li>Change the sizes breakpoints</li>
<li>
<h3>Modify the images</h3>
<ul>
<li>resize the images</li>
<li>change the quality</li>
<li>Keep them under 200kb</li>
<li>Add a visual marker on the different versions of webp images</li>
</ul>
</li>
</ul>
</section>
</main>
<footer>
<p>&copy; 2021 Ashlyn Knox | MIT License</p>
</footer>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment