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).
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.widthandheightโ 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.
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.
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.
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.
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.
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>. srcsetuses width descriptors (500w, 800w) to let the browser choose based on viewport width.sizestells the browser how wide the image will display at different breakpoints.
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 requiremutedto be set too.loopโ Replays the video when it ends.mutedโ Starts with sound off. Often used withautoplay.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).
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>orsrcset. - 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.
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
altattribute - A
<figcaption>describing the image
- An
- 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 withcontrolsand a placeholderposterimage
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 requiressrcandaltattributes. - 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 orsrcsetattribute. - 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
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 โ