모바일 퍼스트 이미지
Coddy HTML 여정의 실전 프론트엔드 섹션에 포함된 레슨. 35개 중 9번째.
모바일 우선 이미지란 먼저 작은 화면에서 이미지가 잘 표시되도록 한 다음, 더 큰 디스플레이에 맞게 개선하는 것을 의미합니다. 모바일에서는 용량이 큰 이미지가 로딩을 느리게 할 수 있으므로, 더 작고 최적화된 버전으로 시작합니다. 화면이 커지면 더 높은 품질의 이미지로 바꿀 수 있습니다
화면 너비에 따라 다양한 이미지 크기를 제공하기 위해 HTML picture 요소를 사용해 봅시다.
<picture>→ 다양한 화면에 맞는 여러 이미지 옵션을 담는 래퍼입니다.
<source>→ 어떤 이미지를 불러올지(srcset), 그리고 어떤 조건에서 불러올지(media)를 정의합니다. 예: 휴대폰(≤600px) → 작은 이미지, 태블릿(≤1024px) → 중간 크기 이미지.
<img>→ 일치하는<source>가 없을 때의 대체 항목으로, 보통 데스크톱용 대형 이미지입니다. 또한alt텍스트를 포함합니다.
<picture>
<source srcset="small-image.jpg" media="(max-width: 600px)">
<source srcset="medium-image.jpg" media="(max-width: 1024px)">
<img src="large-image.jpg" alt="Description of image">
</picture>챌린지
쉬움모바일과 데스크톱 화면 모두에서 보기 좋은 달 이미지를 표시하는 것이 목표입니다.
할 일:
- 반응형 이미지를 설정하려면
<picture>요소를 사용하세요. - 768px 이상인 화면에는 더 큰 달 이미지를 불러오세요:
https://upload.wikimedia.org/wikipedia/commons/e/e1/FullMoon2010.jpg 더 작은 화면(기본 모바일)에는 더 작은 달 이미지를 불러오세요:
https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/330px-FullMoon2010.jpg- 접근성을 위해
alt="Moon in the night sky"를 포함하세요.
직접 해보기
<!DOCTYPE html>
<html>
<head>
<title>Mobile-First Images</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 1rem;
line-height: 1.6;
background: #0a0a23;
color: #f5f5f5;
}
header {
text-align: center;
margin-bottom: 1.5rem;
}
h1 {
font-size: 1.5rem;
color: #ffd369;
}
.content {
max-width: 800px;
margin: 0 auto;
}
img {
max-width: 100%;
height: auto;
border-radius: 8px;
margin: 1rem 0;
}
p {
font-size: 1rem;
}
@media (min-width: 768px) {
h1 {
font-size: 2.25rem;
}
p {
font-size: 1.125rem;
}
}
</style>
</head>
<body>
<header>
<h1>The Moon</h1>
</header>
<main class="content">
<p>
The Moon is Earth's only natural satellite and has fascinated humans for thousands of years.
It influences tides, inspires myths, and continues to be a target for exploration.
</p>
<p>
Mobile-first images ensure the page loads fast on smaller devices while still looking
beautiful on bigger screens.
</p>
</main>
</body>
</html>이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
실전 프론트엔드의 모든 레슨
직접 연습해 보세요: Web 플레이그라운드