HTML Images

Master the art of adding visual content to your web pages with HTML images!

šŸ–¼ļø Visual Content
♿ Accessibility
⚔ Performance

What are HTML Images?

The <img> tag is used to embed images in web pages. It's a self-closing tag that pulls images from external sources.

šŸŽÆ Why Images Matter:

  • šŸ‘ļøVisual Appeal - Make content engaging and memorable
  • šŸ“–Content Enhancement - Show what words can't describe
  • ♿Accessibility - alt text helps screen readers
  • šŸ”SEO Benefits - Images can appear in search results
šŸžļø

Image Formats

JPEGPhotos
PNGGraphics, Transparency
GIFAnimations
SVGIcons, Logos
WebPModern Web

šŸ“ Basic Image Syntax

The <img> Tag

<img src="image.jpg" alt="Description">
šŸ’”

Self-Closing Tag

The <img> tag doesn't need a closing tag - it's self-contained!

šŸ“

External Reference

Images are loaded from external files, not embedded in HTML

Live Example

Beautiful landscape with mountains and lake
<img src="landscape.jpg" alt="Beautiful landscape with mountains and lake">

šŸ”§ Image Attributes Explained

šŸŽÆ Required Attributes

src (Source)

Specifies the path to the image file

src="images/photo.jpg"
Paths can be: Relative, Absolute, or Full URL

alt (Alternative Text)

Provides text description for accessibility

alt="A red apple on a wooden table"
Used by: Screen readers, when images fail to load, SEO

⚔ Optional Attributes

width & height

Specifies image dimensions in pixels

width="400" height="300"
Benefit: Prevents layout shifts during loading

loading

Controls when the image loads

loading="lazy"
Values: eager (default), lazy (load when visible)

šŸ‘€ Visual Attribute Guide

Attribute Explorer

Live Demonstration

šŸ“ → šŸ–¼ļø

src Attribute

<img src="images/photo.jpg" ...>
What it does:

The image file path or URL

Example:

src="images/photo.jpg"

Importance:

Required

🌐 How Browsers Process Images

Browser Processing Journey

Follow how the browser processes an image from HTML to screen

1

HTML Parsing

Browser reads the HTML and encounters the <img> tag

šŸ“„ → šŸ”
2

Source Resolution

Browser determines the image file location

šŸ” → šŸ“
3

HTTP Request

Browser sends request to fetch the image file

🌐 → šŸ“”
4

Download & Decode

Image data is downloaded and processed

šŸ“„ → šŸ–¼ļø
5

Rendering

Image is displayed on the page

šŸŽØ → šŸ‘ļø
6

Accessibility

Alt text is available for screen readers

♿ → šŸ“–

Step 1: HTML Parsing

šŸ“„ → šŸ”

HTML parser identifies the image element and its attributes

<img src="photo.jpg" alt="Description">
What happens:

Browser reads the HTML and encounters the <img> tag

Browser Action:

Creates DOM element and collects attributes

Complete Processing Flow

šŸ“„
Step 1
→
šŸ”
Step 2
→
🌐
Step 3
→
šŸ“„
Step 4
→
šŸŽØ
Step 5
→
♿
Step 6

šŸ’” Real-World Examples

šŸžļø

Basic Image

<img src="landscape.jpg" alt="Mountain landscape at sunset">

Simple image with descriptive alt text

Use Case: General purpose images
šŸ“

With Dimensions

<img src="photo.jpg" alt="Portrait" width="400" height="600">

Image with specified width and height

Use Case: Preventing layout shifts
šŸš€

Lazy Loading

<img src="banner.jpg" alt="Promotional banner" loading="lazy">

Image loads when it enters viewport

Use Case: Below-fold content
šŸ”—

External URL

<img src="https://example.com/image.jpg" alt="External image">

Image loaded from external source

Use Case: CDN or external hosting
šŸ’«

With CSS Class

<img src="avatar.jpg" alt="User avatar" class="rounded-full">

Image with CSS styling class

Use Case: Styled images
♿

Decorative Image

<img src="divider.png" alt="">

Empty alt for decorative images

Use Case: Visual elements without meaning

♿ Accessibility & Best Practices

šŸŽÆ Writing Good Alt Text

āœ… Do's

  • āœ“Be descriptive but concise
  • āœ“Include text that's in the image
  • āœ“Describe the purpose, not just content
  • āœ“Use empty alt for decorative images

āŒ Don'ts

  • āœ—Start with "image of" or "picture of"
  • āœ—Use filenames as alt text
  • āœ—Write overly long descriptions
  • āœ—Skip alt attribute entirely

šŸ“Š Alt Text Examples

āœ…

Descriptive & Concise

alt="Golden retriever playing fetch in the park"

āŒ

Too Vague

alt="Dog" (What kind? What is it doing?)

āœ…

Functional Context

alt="Download PDF file" (for download button image)

āŒ

Filename as Alt

alt="IMG_0234.jpg" (Not helpful for users)

āœ…

Empty for Decorative

alt="" (For purely decorative images)

āŒ

Missing Alt

No alt attribute (Poor accessibility)

šŸŽ® Image Playground

Configure Your Image

šŸ’” Try These URLs:

Live Preview & Code

Snowy mountain landscape under blue sky

Generated HTML Code:

<img 
  src="https://images.unsplash.com/photo-1506905925346-21bda4d32df4"
  alt="Snowy mountain landscape under blue sky"
  width="400"
  height="300"
  loading="eager"
>
Current Settings:

Size: 400Ɨ300px

Loading: eager

Accessibility:

āœ… Alt text provided

⚔ Performance & Optimization

šŸ“

Right Size & Format

Use appropriate dimensions and modern formats like WebP for better performance.

šŸš€

Lazy Loading

Use loading="lazy" for images below the fold to improve initial page load.

šŸŽÆ

Dimensions

Always specify width and height to prevent layout shifts and improve CLS.

šŸš€ Performance Best Practices:

  • • Compress images without losing quality
  • • Use responsive images with srcset
  • • Implement lazy loading for off-screen images
  • • Consider using CDN for image delivery
  • • Specify dimensions to prevent layout shifts

Ready to Learn More?

Ā© 2024 WebDevLearn. Making web development accessible for everyone.

VIDO - Learn Web Development