Web Image Formats

The Images of the Modern Web

What is in it for you?

Topics and Material

  • Learn about the image types and formats
  • See advantages and disadvantages
  • Learn about the browser support
  • Learn about the web development challenges
  • See compression ratios
  • Learn which format is right for what task

Demo images provided by Pexels under variouos free licenses. More in the footer of each demo page.

Image Types

How to describe image information

Vector Images

I tell you a picture

  • Don't paint a picture, let someone else draw it
  • Contains the instructions how to draw
  • Uses a virtual coordinate system
  • Because the image is not draw yet when data is transferred, the drawer can decide to draw it in different dimensions or colors
  • Fonts and SVGs are example formats

<rect
   width="50"
   height="50"
   rx="8"
   fill="#07a081"
   id="rect1741"
   x="2.0048923"
   y="0" />
<path
   d="m 18.671,33.73 h 7.091 V 26.935 H 27.9 a 5.657,5.657 0 1 0 0,
   -11.314 h -9.229 z m 10.763,3.622 H 15 V 12 h 12.9 a 9.271,
   9.271 0 0 1 1.53,18.435 z"
   fill="#ffffff"
   id="path1743" />

Raster Images

I give you a picture

  • Delivers information using pixels
  • A pixel has no physical dimension
  • A pixel has just color information
  • Color information is often RGB or RGBA
  • Color palettes depend on image format
  • Image pixel will be mapped to canvas pixel
  • Uneven mapping creates artifactes because pixels have to be combined or new information guessed
  • JPG, PNG, BMP are typical example formats

Advantages and Disadvantages

When is what format useful or unfitting

Vector Images

  • Easily scalable
  • Drawing always fits canvas pixels
  • Can be edited without losing information
  • Awesome for clear shapes
  • Painter (renderer) might interpret the instructions differently
  • Painted/drawn on demand
  • Not suitable for photos or very complex graphics
  • The more complex, the slower

Raster Images

  • Quick to draw
  • Perfect for dense information
  • Mainly photo like images
  • Larger in size
  • Not scalable without loss
  • Often not editable without loss

Image Formats

How to transport image information

Why do we need formats?

Just some numbers

  • PNG: 960x 636 File: 67.0 kB Memory: 6.3 MB
  • PNG: 1920x1272 File: 143.8 kB Memory: 25.2 MB*
  • SVG: 1920x1272 File: 20.0 kB Memory: 22.8 MB
  • JPG: 1920x1272 File: 283.8 kB Memory: 22.8 MB
  • PNG: 960x 636 File: 860.6 kB Memory: 5.70 MB
  • JPG: 960x 636 File: 59.1 kB Memory: 5.70 MB
  • PNG: 1920x1272 File: 3,200.0 kB Memory: 22.80 MB
  • JPG: 1920x1272 File: 188.7 kB Memory: 22.80 MB Q80
  • JPG: 1920x1272 File: 1,800.0 kB Memory: 22.80 MB Q100

SVG - Scalable Vector Graphics

Any size matters

  • True vector format
  • Open and well supported
  • Creator and renderer must agree on standard
  • Perfect for icons and logos
  • Perfect for charts and diagrams
  • Unsuitable for photos

SVG, 1.2 MB

SVG - Demo

SVG, 1.5 MB, Inkscape Rocks by m1981, CC-BY-SA 4.0

GIF - Graphics Interchange Format

The oldest kid on the block

  • Raster format
  • 8-bit, 256 colors only
  • Lossless compression
  • Lempel-Ziv-Welch-like compression
  • Can contain animations (film-strip)
  • Supports transparency
  • Was once patented
  • Rarely used except for some simple animations

GIF, 960x636, 241.2 kB

PNG - Portable Network Graphics

The better GIF

  • Created to overcome patents
  • Lossless compression
  • Supports transparency
  • 16.7 million colors, RGB
  • Additional 8-bit alpha-channel
  • Great intermediate format
  • Perfect for artwork
  • Animation support APNG

PNG, 960x636, 67.0 kB

PNG - Demo

PNG, 17.8 kB, 250x171, RGBA

PNG, 17.8 kB, 250x171, RGBA, resized 750x513

PNG, 51.7 kB, RGBA, resized 250x513

PNG, 51.7 kB, RGBA

JPEG

Joint Photographic Experts Group

  • Most known format, used everywhere
  • Patent free, standardized
  • JPEG is more than a format, it is a huge standard
  • There are 41 JPEG formats, but we use only one!
  • ISO/IEC IS 10918-1, only 8 bit, lossy, Huffmann in use
  • Can be progressive/interlaced
  • Not suitable for image editing (lossy)
  • Allows quality settings from 1 to 100
  • 8x8 tile-based compression
  • Compresses by dropping details - eyes are more sensitive to differences in brightness than differences in hue
  • Dropping color information compresses (lossy)
  • Discrete cosine transformation (DCT) to split the image into separate frequencies
  • Dropping detail information compresses (lossy)
  • Applying Huffmann compression at the end (lossless)

JPEG - Demo Quality 1

PNG, Original, 505 kB

JPG, Quality 100, 330 kB

JPG, Quality 70, 61 kB

JPG, Quality 30, 35 kB

JPEG - Demo Quality 2

JPG, Quality 90, 108 kB

JPG, Quality 80, 75 kB

JPG, Quality 70, 61 kB

JPG, Quality 60, 52 kB

JPEG - Demo Quality 3

JPG, Quality 50, 46 kB

JPG, Quality 30, 36 kB

JPG, Quality 10, 18 kB

JPG, Quality 1, 6 kB

JPEG - Commerce

JPG, Quality 100, 180 kB

JPG, Quality 90, 48 kB

JPG, Quality 80, 4:4:4, 32 kB

JPG, Quality 80, 4:2:0, 26 kB

WebP

Video encoding for still images

  • Lossless or lossy
  • Based on V8 video codec
  • Predictive drawing
  • Adaptive block quantization
  • Arithmetic entropy encoding
  • Supports alpha channels
  • Animation possible
  • No progressive mode
  • By Google
  • Slower to encode and decode
  • WebP2 in the making
  • Supported by all major browsers
  • Only 4:2:0 color subsampling
  • 8-bit color depth aka 16.7 million colors

WebP - Demo Quality 1

PNG, Original, 505 kB

WebP, Lossless, 343 kB

WebP, Quality 70, 45 kB

WebP, Quality 30, 32 kB

WebP - Demo Quality 2

WebP, Quality 90, 108 kB

WebP, Quality 80, 75 kB

WebP, Quality 70, 61 kB

WebP, Quality 60, 52 kB

WebP - Demo Quality 3

WebP, Quality 50, 46 kB

WebP, Quality 30, 36 kB

WebP, Quality 10, 18 kB

WebP, Quality 1, 6 kB

WebP - Commerce

WebP, Quality 100, 155 kB

WebP, Quality 90, 83 kB

WebP, Quality 80, 55 kB

WebP, Quality 70, 45 kB

AVIF

Another video codec used for still images

  • Based on AV1
  • Lossless and loosy
  • Up to 12-bit color depth
  • Animation support
  • Better color subsampling (similar to JPEG)
  • Limited browser support, but getting stronger
  • No progressive support
  • Expensive encoding
  • Good for photos

AVIF - Demo Quality 1

PNG, Original, 505 kB

AVIF, Lossless, 343 kB

AVIF, Quality 70, 38 kB

AVIF, Quality 30, 17 kB

WebP - Demo Quality 2

AVIF, Quality 90, 85 kB

AVIF, Quality 80, 50 kB

AVIF, Quality 70, 38 kB

AVIF, Quality 60, 30 kB

WebP - Demo Quality 3

AVIF, Quality 50, 20 kB

AVIF, Quality 30, 17 kB

AVIF, Quality 10, 5 kB

AVIF, Quality 1, 3 kB

AVIF - Commerce

AVIF, Quality 100, 82 kB

AVIF, Quality 90, 30 kB

AVIF, Quality 80, 19 kB

AVIF, Quality 70, 14 kB

JPEG XL

JPEG improved

  • Lossy and lossless
  • Transcoding from JPEG without loss
  • Up to 32 bits per channel
  • Up to 4k channels
  • Animation support
  • Supports alpha transparency
  • Progressive support
  • No browser support!
  • Extremly fast (WebP and AVIF are really slow)
  • Good for photos and synthetic images
  • Support for various color spaces, transfer curves, and high screen brightness
  • Variable block size (JPEG was 8x8)
  • Encoding models and predictions
  • Noise synthesis
  • Extremly nice outcome, examples ahead.

Direct Comparison

Images Formats Compared

JPEG XL, 171 kB

JPG, 169 kB

WebP, 169 kB

AVIF, 170 kB

JPEG XL, Quality 50, 53 kB

JPG, Quality 50, 73 kB

WebP, Quality 50, 59 kB

AVIF, Quality 50, 37 kB

Commerce - 500 width, Quality 90

Intrinsic size displayed

JPEG XL, Quality 90, 24 kB

JPG, Quality 90, 33 kB

WebP, Quality 90, 22 kB

AVIF, Quality 90, 16 kB

Commerce - 1000 width, Quality 90

Scaled to 50% width

JPEG XL, Quality 90, 89 kB

JPG, Quality 90, 129 kB

WebP, Quality 90, 90 kB

AVIF, Quality 90, 71 kB

Responsive Images

How tell the browser more

Basics

How do responsive images work?

  • Control what image is used under which conditions
  • Conditions: Resolution (DPR), size, device, properties
  • Control the image source and control the image display
  • Goal: Use the best image for the job and limit bandwidth use
  • Additional feature: Control loading priorities
  • Two possible ways to do that
  • Recommend - descriptive syntax
  • Force - prescriptive syntax, also called art direction

Recommend - descriptive syntax I

Suggest possible images with srcset

  • Describes images and possible fits
  • Leaves decision making to the browser
  • You can hint but not change.

<!-- Demo Example, More Later -->
<img src="img1.jpg"
     srcset="img1.jpg 100w, img2.jpg 500w, img3.jpg 1000w"
     sizes="(max-width: 900px) 90vw,
            (max-width:1100px) 70vw,
            60vw"
     width="600" height="500">
  • Enhances img
  • srcset declares alternatives and when to use
  • Browser replaces src attribute
  • MUST come with sizes attribute
  • Image path + additional information
  • 1. Image size, e.g. 420w, 900px, 20em
  • 2. Resolution, e.g. 1x, 2x
  • One or the other, not both!

Important: If a larger size has been loaded before, the browser does not load the smaller despite a better fit. Testing might be confusing!

Recommend - descriptive syntax II

Examples

By DPR


<!--
    Usually 1x, 1.5x and 2x make sense, not more,
    no sizes attribute, implies same screen respresentation all the time
-->
<img src="img1.jpg"
     srcset="img1.jpg 1x, img2.jpg 2x, img3.jpg 4x"
     width="600" height="500"
     alt="Blue horses on a yellow field"">

By Size


<!--
    img1 as default for older browsers,
    srcset says what size each image has
    100w means 100 pixel

    sizes defines which size should be used based in the media using media-queries,
    such as min-width, max-width, orientation, color schema, height and more

    (max-width: 900px) 90vw
    If the viewport has a size of 900px or less use an image that fits 90%
    of view port size (vw).
-->
<img src="img1.jpg"
     srcset="img1.jpg 100w, img2.jpg 500w, img3.jpg 1000w"
     sizes="(max-width: 900px) 90vw,
            (max-width:1100px) 70vw,
            60vw"
     width="600" height="500">

 <img src="img1.jpg"
      srcset="img1.jpg 500w, img2.jpg 1000w, img3.jpg 2000w"
      sizes="(min-width: 1900px) 2000px,
             (min-width: 1200px) 1000px,
             (min-width: 780px) 500px,
             200px"
      width="2000" height="800">
  • First rule stops processing

Width, Height, and Alt

Some more intel

  • img attributes width and height are needed
  • Tells the browser the intrinsic size
  • Important for the aspect ratio
  • Helps to reserve space
  • alt is meant to aid screenreaders
  • When the image is decoration, use an EMPTY alt, never drop it

<!-- decoration -->
<img
    src="flourish.png"
    role="presentation"
    alt=""
    width="400" height="50">

<!-- meaning -->
<img
    src="family.jpg"
    alt="Family of four standing in front of an ice cream truck"
    width="400" height="50">

Force - prescriptive syntax I

Enforces image use, also called art direction

  • Enables switching images based on size, resolution, media, and image support
  • Supports all of srcset and more
  • Uses picture and source
  • Can combine many features for the optimal choice
  • Can combine prescriptive and descriptive syntax

Simple Examples


<!--
    Switch the images based on the color scheme preference
-->
<picture>
   <source media="(prefers-color-scheme: dark)" srcset="hero-dark.jpg">
   <img src="hero-light.jpg">
</picture>

<!--
    Switch the images based on image support, first match is used
-->
<picture>
    <source srcset="small.avif" type="image/avif">
    <source srcset="small.webp" type="image/webp">
    <img src="hero.jpg">
</picture>

Force - prescriptive syntax II

Srcset-like Example




    
    
    Two horses walking the walk




    
    
    
    Two horses walking the walk




    
    
    The horses...

Srcset/Size/Resolution Example


<!--
    Offer more alternatives per media query
-->
<picture>
    <source media="(min-width: 1200px)"
            srcset="high-bp-1600.jpg 1600w, high-bp-1000.jpg 1000w">
    <source media="(min-width: 800px)"
            srcset="high-bp-900.jpg 900w, high-bp-500.jpg 500w">
    <source srcset="lower-bp-1200.jpg 1200w, lower-bp-800.jpg 800w">
    <img src="fallback.jpg" alt="…">
</picture>

<!--
    Alternate by resolution
-->
<picture>
    <source media="(min-width: 1200px)"
            srcset="high-bp-1200.jpg 1x, high-bp-2400.jpg 2x">
    <source srcset="lower-bp-600.jpg 1x, lower-bp-800.jpg 2x">
    <img src="fallback.jpg" alt="…">
</picture>

Factors to Consider

Factors to consider when adding images to a site

Reserving the right space for each image. Figuring out how many sizes one needs. Deciding whether the image is content or decorative.

It's worth spending the time to get images right. Poor image strategies are responsible for frustration and annoyance for users. A good image strategy makes your site feel snappy and sharp, regardless of the user's device or network connection.

Loading priorities I

How do change priorities

  • All images are by default eagerly loaded
  • Some images might not be needed at first or never - below the fold content
  • Declare these lazy using loading="lazy"
  • fetchpriority="high"
  • Make an image most important
  • Helps to load it first and not in line with all other important images
  • Apply with caution, you even might push it before a font

<!-- Below the fold -->
<img src="insta.jpg" alt="A description of the image." loading="lazy">

<!-- Hero, loading="eager" is default-->
<img src="hero.jpg" alt="A description of the image." fetchpriority="high">

Loading priorities II

How do change priorities

  • Image via JS or CSS background images might be still delayed
  • Preload these using link rel="preload"
  • It is a forced load
  • Might bypass image selection logic!
  • Repeats code, so only good for some cases

<head>
    ...
    <!-- Simple -->
    <link rel="preload" as="image" href="important.png">

    <!-- Responsive -->
    <link rel="preload" as="image" href="wolf.jpg"
        imagesrcset="wolf_400px.jpg 400w, wolf_800px.jpg 800w"
        imagesizes="50vw">
    ...
</head>

Misc

Some more information

Resources

Resources to learn and play

Squoosh App by Google

Questions And Answers

Your Questions and Feedback