What are HTML Images?
HTML images allow you to display pictures, graphics, icons, and illustrations on web pages using the <img> tag. Images make websites more engaging, help explain concepts visually, and improve user experience.
Basic Syntax
Here's the simplest way to add an image to your webpage:
The browser reads this tag, fetches the image from the specified location, and displays it in your page.
Essential Image Attributes
| Attribute | Purpose | Example | Required? |
|---|---|---|---|
| `src` | Image source path (URL or file path) | `src="images/photo.jpg"` | ✅ Yes |
| `alt` | Alternative text for accessibility & broken images | `alt="Sunset over mountains"` | ✅ Yes (recommended) |
| `width` | Image width in pixels or percentage | `width="300"` | ❌ No |
| `height` | Image height in pixels | `height="200"` | ❌ No |
| `title` | Tooltip text shown on hover | `title="Click to enlarge"` | ❌ No |
| `loading` | Lazy loading strategy | `loading="lazy"` | ❌ No |
Complete Example
Here's a well-structured image tag with multiple attributes:
Understanding Image Paths
The src attribute accepts two types of paths:
| Path Type | Example | When to Use |
|---|---|---|
| **Absolute Path** (External URL) | `https://example.com/images/cat.jpg` | Images hosted on another website or CDN |
| **Relative Path** (Local file) | `./images/cat.jpg` or `../assets/photo.png` | Images stored in your own project folder |
Supported Image Formats
| Format | Best For | Pros | Cons |
|---|---|---|---|
| **JPEG/JPG** | Photographs, complex images | Small file size, millions of colors | No transparency, loses quality when compressed |
| **PNG** | Logos, icons, screenshots | Lossless, supports transparency | Larger file size |
| **WebP** | Modern websites (all types) | Smaller than JPEG/PNG, supports transparency | Older browser support (but 96%+ covered) |
| **SVG** | Logos, icons, illustrations | Scalable without quality loss, small size | Not suitable for complex photos |
| **GIF** | Simple animations | Supports animation | Limited colors (256), large file size |
Responsive Images (Mobile-Friendly)
Websites must look good on all devices — from smartphones to 4K monitors. Here's how to make images responsive:
This ensures the image never exceeds its container's width while maintaining aspect ratio.
The browser automatically picks the most appropriate image based on screen size and resolution.
Advanced: The Picture Element
Use <picture> when you need different crops or entirely different images for different screen sizes (e.g., mobile vs desktop layouts).
Making Images Clickable (Image Links)
Wrap an <img> inside an <a> tag to turn it into a clickable link:
Image Optimization Best Practices
- ✅ Always include alt text – Critical for accessibility (screen readers) and SEO. Describe the image's content or purpose.
- ✅ Compress your images – Use tools like TinyPNG, Squoosh, or ImageOptim to reduce file size without visible quality loss.
- ✅ Choose the right format – JPEG for photos, PNG for logos/transparency, WebP for everything modern.
- ✅ Enable lazy loading – Add
loading="lazy"to images below the fold to speed up initial page load. - ✅ Specify dimensions – Always set
widthandheightto prevent layout shifts (Cumulative Layout Shift optimization). - ✅ Use responsive images – Serve smaller images on mobile devices to save bandwidth.
Common Mistakes to Avoid
- ❌ Missing alt attribute – Screen reader users won't know what the image shows. Also hurts SEO.
- ❌ Using huge unoptimized images – A 5MB image will make your page slow and frustrate mobile users.
- ❌ Incorrect file paths – Double-check relative paths;
./images/photo.jpgis different fromimages/photo.jpg. - ❌ Scaling with width/height only – Downloading a 2000px image and displaying it at 200px wastes bandwidth. Use
srcsetinstead. - ❌ Text inside images – Screen readers can't read text inside JPEGs. Use real HTML text whenever possible.
- ❌ Forgetting to set dimensions – Causes layout shifts (CLS) and poor user experience.
Accessibility Tips
Making images accessible ensures everyone can understand your content, including people using screen readers:
- Descriptive alt text –
alt="Golden retriever playing in snow"notalt="dog". - Empty alt for decorative images – Use
alt=""for purely visual images (backgrounds, spacers). - Longdesc for complex images – For charts or diagrams, provide a longer description nearby.
- Good contrast – Ensure text overlaying images has sufficient contrast.
Real-World Example
Here's a complete, production-ready image implementation:
This example includes modern formats (WebP with fallbacks), responsive sizing, accessibility, and performance optimizations.
Conclusion
Images bring your websites to life, but they must be implemented thoughtfully. By following these best practices — proper alt text, responsive techniques, format selection, and optimization — you'll create fast, accessible, and visually appealing web pages that work beautifully on any device.
Remember: A well-optimized image loads instantly and looks great. A poorly implemented image frustrates users and hurts your SEO. Choose wisely!