HTML Images & Media
Images are a vital part of the web experience. The <img> tag allows you to embed graphical content, but it requires careful attention to attributes to ensure performance and accessibility.
The Source Attribute
The src attribute is mandatory. It tells the browser where the image file is located. This can be a relative path (e.g., /images/logo.png) or an absolute URL (e.g., https://example.com/logo.png).
Alternative Text
The alt attribute provides a text description of the image. This is displayed if the image fails to load and is read aloud by screen readers for visually impaired users. Good alt text describes the content and function of the image.
Width and Height
Always define width and height attributes. This allows the browser to reserve space for the image before it downloads, preventing Cumulative Layout Shift (CLS), which can negatively impact user experience and SEO.
View Full Transcript+
This section contains detailed information about image formats (JPEG, PNG, WebP), responsive images using the picture element, and advanced accessibility techniques. Remember that decorative images should have an empty alt attribute (alt="").
