0Pricing
Web Performance Optimization & Lighthouse · レッスン

レスポンシブ画像と遅延読み込み

`srcset`と`sizes`を使ったレスポンシブ画像の技法を実装し、画面外のメディアには遅延読み込みを活用します。

「レスポンシブ画像と遅延読み込み」はCoddyKit上の無料Web Performance Optimization & Lighthouseレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWeb Performance Optimization & Lighthouse学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Web Performance Optimization & Lighthouseコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Responsive Images?

Imagine browsing a website on a tiny phone screen, but the site loads an image meant for a huge desktop monitor. That's a waste of data and makes the page slow!

Responsive images ensure users get images optimized for their specific device and screen size, improving performance and user experience.

`srcset` for Resolution

The srcset attribute allows you to provide a list of different image files for the browser to choose from. One way to use it is for resolution switching.

This means serving higher-resolution images to devices with high-DPI (Retina) screens and standard resolution images to others.

  • 1x: Standard resolution
  • 2x, 3x: Higher resolutions
<img src="cat-1x.jpg" srcset="cat-1x.jpg 1x, cat-2x.jpg 2x" alt="A cute cat" />

`srcset` for Viewport Width

Beyond resolution, srcset can also help with viewport switching. This lets you offer different image files based on the browser's viewport width.

The w descriptor indicates the intrinsic width of the image file. The browser then picks the most appropriate source.

<img srcset="small.jpg 400w, medium.jpg 800w, large.jpg 1200w" src="medium.jpg" alt="A landscape" />

The `sizes` Attribute

When using srcset with w descriptors, the browser needs to know how wide the image will actually be rendered on the page at different viewport sizes. That's where the sizes attribute comes in.

sizes takes a list of media conditions and image slot widths. It tells the browser, "At this screen size, the image will take up X% of the viewport."

Working with `sizes`

The browser uses sizes to pick the best image from srcset. It doesn't resize the image; it just helps select the right source file.

  • (max-width: 600px) 100vw: If viewport is 600px or less, image is 100% of viewport width.
  • (max-width: 1200px) 50vw: If viewport is 1200px or less, image is 50% of viewport width.
  • 800px: Otherwise, image is 800px wide.

Combined `srcset` & `sizes`

Here’s how srcset and sizes work together. Try changing your browser window size to see how it might pick different images.

The browser uses sizes to calculate the ideal image width and then finds the closest match in srcset's provided widths.

<!DOCTYPE html>
<html>
<head>
  <title>Responsive Image</title>
</head>
<body>
  <img
    srcset="
      image-400w.jpg 400w,
      image-800w.jpg 800w,
      image-1200w.jpg 1200w"
    sizes="
      (max-width: 600px) 100vw,
      (max-width: 1200px) 50vw,
      800px"
    src="image-800w.jpg"
    alt="A beautiful landscape"
    style="max-width: 100%; height: auto;"
  >
  <p>Imagine image-400w.jpg, image-800w.jpg, image-1200w.jpg exist.</p>
</body>
</html>

Art Direction with `<picture>`

Sometimes, simply scaling an image isn't enough. You might want to display a completely different image, or a different crop, based on the screen size or device type. This is called art direction.

The <picture> element allows you to provide multiple <source> elements with different media queries and image sources, and a fallback <img>.

<picture>
  <source media="(max-width: 600px)" srcset="narrow-crop.jpg">
  <source media="(min-width: 601px)" srcset="wide-crop.jpg">
  <img src="default-crop.jpg" alt="A scenic view">
</picture>

What is Lazy Loading?

Even with responsive images, if a page has many images, it can still be slow. Lazy loading helps by deferring the loading of non-critical resources (like images or iframes) until they are needed.

Typically, this means loading them only when they are about to enter the user's viewport.

  • Reduces initial page load time.
  • Saves bandwidth for users.
  • Improves Core Web Vitals like LCP.

Native Lazy Loading

Modern browsers offer native lazy loading, meaning you don't need JavaScript libraries for basic lazy loading. You just add the loading="lazy" attribute to your <img> or <iframe> tags.

  • eager: Loads immediately.
  • lazy: Defers loading until near viewport.
  • auto: Browser decides.
<img src="placeholder.jpg" data-src="actual-image.jpg" loading="lazy" alt="Product image" />

<p>Note: <code>data-src</code> is often used with JS-based lazy loading. For native, just <code>src</code> + <code>loading='lazy'</code> is usually enough if the browser supports it.</p>

Quick Check

Which attributes and elements are primarily used to implement responsive images and lazy loading?

Lesson Summary

We've explored powerful techniques to optimize images and media for web performance:

  • Responsive Images: Use srcset (with x for resolution, w for viewport width) and sizes to deliver appropriate image sizes.
  • Art Direction: Employ the <picture> element for more control over image display based on media conditions.
  • Lazy Loading: Implement loading="lazy" on <img> and <iframe> to defer off-screen content, boosting initial page load speed.

By using these methods, you can significantly improve your website's performance and user experience!

よくある質問

「レスポンシブ画像と遅延読み込み」レッスンは無料ですか?

はい。「レスポンシブ画像と遅延読み込み」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Web Performance Optimization & Lighthouseコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Web Performance Optimization & Lighthouseコースには全4レッスンが含まれています。

「レスポンシブ画像と遅延読み込み」で何を学びますか?

`srcset`と`sizes`を使ったレスポンシブ画像の技法を実装し、画面外のメディアには遅延読み込みを活用します。 ブラウザで直接実行するハンズオンコードでWeb Performance Optimization & Lighthouseを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Web Performance Optimization & Lighthouseを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWeb Performance Optimization & Lighthouseは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「レスポンシブ画像と遅延読み込み」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWeb Performance Optimization & Lighthouseレッスンでコードを書いて実行できますか?

はい。すべてのWeb Performance Optimization & Lighthouseレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 効率的な画像フォーマット
  2. レスポンシブ画像と遅延読み込み
  3. 動画とアニメーションのパフォーマンス
  4. 画像CDNと配信の最適化
← Web Performance Optimization & Lighthouseに戻る