Перейти к основному содержимому
← 📄 HTML & the platform·Модуль A4 · Урок 5
ЗаданиеAdd an image with src='/hero.jpg', alt='Three developers at a whiteboard', width='1200', height='630', loading='lazy'.

<img>: alt, ширина/высота, отложенная загрузка

100 XP8 min
Теория

The four attributes that matter

<img src="/hero.jpg"
     alt="Aerial view of San Francisco at sunset"
     width="1200" height="630"
     loading="lazy"
     decoding="async" />
  • `alt` — what the image SHOWS. For decoration only, use alt="" (empty alt — screen readers skip it). NEVER omit the attribute entirely.
  • `width` + `height` — intrinsic pixel dimensions. The browser reserves the space BEFORE downloading, preventing CLS (Cumulative Layout Shift). Lighthouse penalizes images without dimensions hard.
  • `loading="lazy"` — defer download until the image is near the viewport. Big win for long pages. Set on every image below the fold.
  • `decoding="async"` — decode off the main thread.

alt-text mini-lesson

ImageGood altBad alt
Hero photo"Three developers at a whiteboard""image" or "hero"
Logo"CodeMentor AI""Logo"
Decorative patternalt="""decorative pattern"
Chart"Revenue grew 40% in Q3""Bar chart"
Icon next to text "Settings"alt="" (text already says it)"Settings icon"

Width/height vs aspect-ratio CSS

Specify width AND height in the attributes. Modern browsers compute the aspect ratio from them and reserve correct space even when CSS makes the image fluid (max-width: 100%; height: auto). The "always set width and height" rule applies.

🔒

Зарегистрируйся чтобы кодить

Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.

Регистрация — бесплатный trial →

Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.

← НазадСледующий урок →

Получай один совет по Python или веб-разработке — каждый день в почте

Коротко, без воды, без спама. Отписаться — в один клик.