CodeBlanks Logo Icon CodeBlanks

Summary - Responsive hero

HTML course · Advanced · Lesson 12/56

Write a hero image that changes per screen with <picture>.

What we learned in this group

In this group you learned responsive image techniques:

  • <picture> and <source> for image selection
  • media for per-screen art direction
  • type for progressive AVIF/WebP serving
  • width and height to reserve space and reduce CLS
  • empty alt="" for decorative images

Combine these five and you get an image that is fast, accessible and shows the right crop per screen. Now we will merge them in a responsive hero image.

Exercise Write a responsive hero image

Add a <picture> to body. Put two <source> tags inside: the first with media set to (min-width: 800px), srcset set to hero-wide.avif and type set to image/avif; the second with media set to (max-width: 799px), srcset set to hero-narrow.webp and type set to image/webp. Finish with an <img> whose src is hero-narrow.png, alt is Hero image, width is 800 and height is 450.

Show example solution
<body>
  <picture>
    <source media="(min-width: 800px)" srcset="hero-wide.avif" type="image/avif">
    <source media="(max-width: 799px)" srcset="hero-narrow.webp" type="image/webp">
    <img src="hero-narrow.png" alt="Hero image" width="800" height="450">
  </picture>
</body>

Test yourself

  1. Match each concept to its role

    <picture>
    Image selection wrapper
    media
    Screen condition
    type
    Format (MIME)
    width/height
    Space reservation
    alt=""
    Decorative image
  2. Open the responsive hero with picture

    <____>
      <source media="(min-width: 800px)" srcset="hero-wide.avif" type="image/avif">
      <img src="hero-narrow.png" alt="Hero image" width="800" height="450">
    </picture>
    Show answer
    <picture>
      <source media="(min-width: 800px)" srcset="hero-wide.avif" type="image/avif">
      <img src="hero-narrow.png" alt="Hero image" width="800" height="450">
    </picture>
  3. Which is the best structure for a responsive hero image?

    • A picture with media-based sources and a final img fallback
    • Only an img and a CSS background-image
    • A picture with only one source and no img
    • Several img tags side by side
    Show answer

    A picture with media-based sources and a final img fallback — Correct. media sources with an img fallback is ideal for a responsive hero.

All lessons in Advanced