반응형 이미지 및 지연 로딩
`srcset`과 `sizes`를 사용한 반응형 이미지 기법을 구현하고 화면 밖 미디어에 지연 로딩을 적용합니다.
반응형 이미지 및 지연 로딩은(는) CoddyKit의 무료 Web Performance Optimization & Lighthouse 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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 resolution2x,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(withxfor resolution,wfor viewport width) andsizesto 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Web Performance Optimization & Lighthouse 강의 전체를 잠금 해제할 수 있습니다. Web Performance Optimization & Lighthouse 강의에는 총 4개의 강의가 포함되어 있습니다.
“반응형 이미지 및 지연 로딩”에서 뭘 배우나요?
`srcset`과 `sizes`를 사용한 반응형 이미지 기법을 구현하고 화면 밖 미디어에 지연 로딩을 적용합니다. 브라우저에서 직접 실행하는 실습 코드로 Web Performance Optimization & Lighthouse을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Web Performance Optimization & Lighthouse을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Web Performance Optimization & Lighthouse은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“반응형 이미지 및 지연 로딩” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Web Performance Optimization & Lighthouse 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Web Performance Optimization & Lighthouse 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 효율적인 이미지 형식
- 반응형 이미지 및 지연 로딩
- 동영상 및 애니메이션 성능
- 이미지 CDN과 전송 최적화