The img tag

The <img> tag is how you embed images in HTML. Unlike most tags, <img> is self-closing โ€” it doesn't need a closing tag. Every image needs two critical attributes: src (the image file path) and alt (alternative text for accessibility).

images.html Basic img tag with all attributes

Key attributes:

  • src โ€” The image file path (required). Can be a local file like "images/photo.jpg" or a full URL.
  • alt โ€” Alternative text describing the image (required for accessibility). Screen readers use this to describe images to visually impaired users. If the image doesn't load, this text displays instead.
  • width and height โ€” Dimensions in pixels. Modern best practice: always set these to prevent layout shift when the image loads.
  • loading="lazy" โ€” Tells the browser to delay loading the image until it's near the viewport. Improves page speed on image-heavy sites.
Accessibility requirement:
Always write meaningful alt text. Instead of "image" or "photo", describe what the image shows: "A red bicycle leaning against a brick wall" helps everyone understand your content.

Image formats: When to use each

Different image formats excel at different jobs. Choosing the right format improves page speed and visual quality:

PNG (Portable Network Graphics)

  • Best for: Images with transparency, logos, icons, screenshots, illustrations with flat colors.
  • Lossless: No quality loss when compressed. File size can be large.
  • Transparency: Supports an alpha channel for transparent backgrounds.

JPG/JPEG (Joint Photographic Experts Group)

  • Best for: Photographs, complex images with many colors.
  • Lossy: Compresses by discarding some color data. Much smaller than PNG.
  • No transparency: Always has a background color.

SVG (Scalable Vector Graphics)

  • Best for: Logos, icons, diagrams, illustrations. Can be edited with code.
  • Vector-based: Scales to any size without quality loss.
  • Small file size: Excellent for responsive design.

WebP

  • Best for: Modern web. Smaller file sizes than JPG with better quality.
  • Newer format: Not supported in older browsers (IE, old Safari).
  • Best practice: Use with the <picture> element as a fallback.

GIF

  • Best for: Animations, short looping videos.
  • Legacy format: Use HTML5 <video> for better quality and smaller file sizes.
Pro tip:
Use free tools like TinyPNG or Squoosh to compress images before uploading. A 2MB image is too large โ€” aim for under 200KB per image.

Semantic markup: figure and figcaption

The <figure> element wraps self-contained content like images, charts, or code snippets. The <figcaption> element provides a caption or description. This semantic markup helps search engines and assistive devices understand your content.

semantic.html Figure with figcaption

The <figure>/<figcaption> combo tells screen readers and search engines: "This image is important and belongs with this caption." Use <img> alone for decorative images; use <figure> for images essential to understanding your content.

โœ๏ธ Try It Yourself โ€” Image Gallery
Preview

Responsive images with picture and srcset

Modern websites adapt to different screen sizes. The <picture> element lets you serve different image formats or sizes based on the viewer's device, screen size, or browser capabilities.

responsive.html Picture element with srcset

How it works:

  • <picture> is a wrapper element.
  • <source> elements define conditions (media queries, file types).
  • The browser picks the first <source> that matches.
  • The <img> tag is a fallback for browsers that don't support <picture>.
  • srcset uses width descriptors (500w, 800w) to let the browser choose based on viewport width.
  • sizes tells the browser how wide the image will display at different breakpoints.
Why responsive images matter:
A 1500px image shouldn't load on a 320px phone. Responsive images save bandwidth and improve load times for mobile users โ€” crucial for SEO and user experience.

Embedding video and audio

HTML5 made it easy to embed video and audio without plugins. The <video> and <audio> tags work natively in all modern browsers.

Video element:

  • controls โ€” Shows play/pause buttons and a progress bar.
  • autoplay โ€” Starts playing automatically. Most browsers require muted to be set too.
  • loop โ€” Replays the video when it ends.
  • muted โ€” Starts with sound off. Often used with autoplay.
  • poster โ€” An image shown before the video plays.
  • <source> โ€” Different video formats for browser compatibility.

Audio element:

  • controls โ€” Shows a play button and volume slider.
  • autoplay โ€” Starts playing automatically (muted unless allowed).
  • loop โ€” Repeats the audio.
  • <source> โ€” Different audio formats (MP3, OGG, WAV).
โœ๏ธ Try It Yourself โ€” Video & Audio
Preview
Note:
The video and audio won't actually play in this editor, but the elements will render with controls. In production, you'd link to real media files on a server or CDN.

Best practices for images and media

  • Always use alt text. It's mandatory for accessibility. Describe what the image shows, not "image" or "photo".
  • Optimize image sizes. Use free tools like TinyPNG or Squoosh. Aim for images under 200KB.
  • Choose the right format. PNG for graphics, JPG for photos, SVG for icons and logos.
  • Use lazy loading. Add loading="lazy" to images below the fold to speed up initial page load.
  • Set width and height. This prevents layout shift when images load, improving your page's Core Web Vitals score.
  • Use responsive images. Serve different image sizes for mobile, tablet, and desktop using <picture> or srcset.
  • Serve modern formats with fallbacks. Use WebP with JPG/PNG fallbacks via <picture>.
  • Add captions to important images. Use <figure> and <figcaption> for images that are central to your content.
  • Use video instead of GIF. A 3-second MP4 video is often smaller than an animated GIF and loads faster.
Build an Image Gallery Page

Task: Create an HTML page with:

  • An <h1> title: "My Photo Gallery"
  • Three <figure> elements, each with:
    • An <img> using placeholder images from https://picsum.photos/300/200?random=1 (etc.)
    • A meaningful alt attribute
    • A <figcaption> describing the image
  • CSS to display the figures side by side using display: flex
  • One image using the <picture> element (just for practice)
  • A <video> element at the bottom with controls and a placeholder poster image

Test your alt text by temporarily disabling images in your browser to see the fallback text.

Hint: Start with a basic HTML skeleton. Add three figures in a container div. Use inline style="display: flex; gap: 20px;" on the container. For the picture element, wrap an img in a picture tag with one source tag inside. The video won't play, but the controls will show.

Key takeaways

  • The <img> tag is self-closing and requires src and alt attributes.
  • Choose image formats wisely: PNG for graphics, JPG for photos, SVG for scalable icons.
  • Use <figure> and <figcaption> for semantically important images with captions.
  • Make images responsive with the <picture> element or srcset attribute.
  • Embed video and audio natively with <video> and <audio> tags.
  • Always optimize: Compress images, lazy load below the fold, set width/height to prevent layout shift.
  • Accessibility first: Write meaningful alt text that describes the image's content and purpose.

Test your knowledge

Question 1 of 5
Which attribute provides alternative text for images?
Question 2 of 5
What element pairs an image with its caption?
Question 3 of 5
Which image format is best for photographs?
Question 4 of 5
What does loading="lazy" do?
Question 5 of 5
Which element provides multiple image sources for different screens?
0
/5
Great work! You understand images and media.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just learned to embed images, video, and audio.
Ready for more?

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’