DEV Community

Cover image for Adding Images in HTML: Sources, Alt Text, and Title Attributes
Sharique Siddiqui
Sharique Siddiqui

Posted on

Adding Images in HTML: Sources, Alt Text, and Title Attributes

Images make web pages dynamic, engaging, and visually appealing. They help illustrate ideas, break up text, and enhance user understanding. But there’s more to adding an image in HTML than just displaying a picture — proper use of sources, alt text, and title attributes ensures accessibility, good SEO, and a better overall experience.

The <img> Tag: The Basics

The HTML element for images is <img>. It’s an empty (self-closing) tag, and at minimum, you need the src (source) and alt (alternative text) attributes.

xml
<img src="path/to/image.jpg" alt="Description of the image">
Enter fullscreen mode Exit fullscreen mode
  • src (source): The URL or path to the image file.
  • alt: Text description of the image for screen readers, search engines, and when the image fails to load.
Setting the Image Source (src Attribute)

You can use different paths depending on where your image is located:

  • Relative path (for images in your project folder):
xml
<img src="images/logo.png" alt="Website Logo">
Enter fullscreen mode Exit fullscreen mode
  • Absolute path (external images):
xml
<img src="https://example.com/banner.jpg" alt="Promotional Banner">
Enter fullscreen mode Exit fullscreen mode

If the image doesn't load (wrong path, broken link), browsers will display the alt text.

Alt Text (alt Attribute): Why It Matters

The alt attribute is critical for:

  • Accessibility: Screen readers describe images using the alt text, making your site usable by visually impaired users.
  • SEO: Search engines "read" alt text to understand what’s in an image, which can impact your search rankings.
  • Fallback: If the image can't load, browsers display the alt text instead.
How to Write Good Alt Text:
  • Be specific and concise (describe only what the image shows).
  • If the image is decorative (adds no content value), use alt="" to tell screen readers to skip it.

Examples:

xml
<img src="team-photo.jpg" alt="Our sales team at the 2025 company retreat">
<img src="divider.png" alt="">
Enter fullscreen mode Exit fullscreen mode

Title Attribute (title): Extra Info on Hover

The title attribute provides additional information, often seen as a tooltip when users hover over the image.

xml
<img src="award.png" alt="First place trophy" title="Winner of the 2025 Web Design Award">
Enter fullscreen mode Exit fullscreen mode

Use it for brief, supplementary info (not as a replacement for alt text).

Note: Not all users will see tooltips (not accessible to screen readers or on mobile).

Complete Example
xml
<img 
  src="artwork.jpg" 
  alt="Colorful abstract painting by Jane Doe" 
  title="Spring Harmony, 2025 - Jane Doe">
Enter fullscreen mode Exit fullscreen mode
  • Screen readers will read “Colorful abstract painting by Jane Doe.”
  • SEO benefits from the descriptive alt text.
  • When hovered (on desktop), a tooltip says “Spring Harmony, 2025 - Jane Doe.”

Best Practices and Tips

  • Always include alt text for every image, unless decorative.
  • Use descriptive filenames for your images (e.g., paris-eiffel-tower.jpg vs. IMG1234.jpg).
  • Compress images to ensure fast loading times.
  • Avoid using images for text content (except for logos). If you must, include the text in the alt attribute.
  • Test with screen readers to ensure accessibility.

Final Thoughts

Adding images in HTML is about more than visuals — it's about usability, accessibility, and providing context.

  • Use the src attribute to point to your image file.
  • Provide clear alt text for accessibility and SEO.
  • Optionally, use the title attribute for hover tooltips.

These small steps help your website reach everyone, look professional, and load smoothly!

Check out the YouTube Playlist for great HTML content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud

Top comments (0)