The <img> tag is used to display images on a webpage.
Important thing to remember:
<img>is a void element- It does not have a closing tag
<img>The image itself is not inside the tag — it is referenced using attributes.
<img src="images/logo.png" alt="Website logo">Breakdown:
<img>→ image elementsrc→ path to the image filealt→ alternative text
Both src and alt are extremely important.
src tells the browser where the image file is located.
<img src="images/profile.jpg" alt="My profile photo">Folder structure:
project/
├── index.html
└── images/
└── profile.jpg
Browser logic:
Start at index.html → go into images → load profile.jpg
<img src="https://example.com/images/logo.png" alt="Company logo">Use absolute URLs when:
- Loading images from a CDN
- Linking external images
alt = alternative text
It is used when:
- Image fails to load
- Screen readers read the page
- Search engines index content
<img src="cat.jpg" alt="Orange cat sitting on a sofa">- Accessibility for visually impaired users
- Improves SEO
- Required for valid, good HTML
❌ Bad alt:
<img src="cat.jpg" alt="">✅ Good alt:
<img src="cat.jpg" alt="Orange cat sitting on a sofa">💡 Think of alt as:
“How would I describe this image to someone over the phone?”
You can control image size using attributes:
<img src="photo.jpg" alt="Beach view" width="300" height="200">- Values are in pixels
- Helps browser reserve space before image loads
- Use CSS for styling
- Use HTML width/height to avoid layout shift
<img src="images/logo.png" alt="Logo">- Portable
- Works locally
- Recommended for most projects
<img src="https://cdn.example.com/logo.png" alt="Logo">- Works everywhere
- Depends on external source
❌ Forgetting the alt attribute
❌ Wrong file path (Images vs images)
❌ Using spaces in file names (my photo.jpg)
❌ Relying only on HTML for styling images
Correct naming:
profile-photo.jpg ✅
My Photo.jpg ❌
- Always include
alt - Keep image file sizes optimized
- Don’t put important text inside images
- Use descriptive file names
Good example:
<img src="team-member-john.jpg" alt="John smiling, frontend developer">