SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
HTML
1. HTML Introduction: What is HTML? Learn with Examples 2. Best Free HTML Editors: Notepad to VS Code for Beginners 3. HTML Basic Structure: Doctype, Head, Body Explained 4. HTML Elements Explained: Tags, Content & Empty Elements 5. HTML Attributes List: href, src, alt, width, title & More 6. HTML Headings h1 to h6: Which Heading Tag to Use When 7. HTML Paragraphs: p Tag, br Line Breaks & pre Text 8. HTML Style Attribute: Colour, Font Size & Alignment 9. HTML Text Formatting Tags: bold, italic, mark & More 10. HTML Comments: How to Add Notes & Hide Code in HTML 11. HTML Colors: Names, HEX, RGB & HSL Colour Codes 12. HTML CSS: Inline, Internal & External Stylesheets 13. HTML Links: a Tag, href, New Tab & Email Links 14. HTML Images: img Tag, src, alt, Width & Formats 15. HTML Favicon: Create & Add a Browser Tab Icon 16. HTML Title Tag: How to Write a Good Page Title 17. HTML Tables: table, tr, th, td, colspan & rowspan 18. HTML Lists: ul, ol, dl & Nested Lists Explained 19. HTML Block vs Inline Elements: div, p, span Explained 20. HTML Div Tag: Group Content & Build Page Layouts 21. HTML Class Attribute: Style Many Elements at Once 22. HTML id Attribute: Unique IDs, Links, CSS & JavaScript 23. HTML Iframes: Embed YouTube, Maps & Pages in HTML 24. HTML JavaScript: script Tag, Clicks & First Example 25. HTML File Paths: Relative vs Absolute Paths Explained 26. HTML Head Element: title, meta, charset & viewport 27. HTML Layout: header, nav, main, footer Page Structure 28. HTML Responsive Design: Viewport, Images & Media Queries 29. Semantic HTML Tags: header, nav, main, article & footer 30. HTML Forms: input, label, select, checkbox & Submit 31. HTML Entities: & < > © with Examples 32. HTML Audio & Video Tags: controls, source & Fallback 33. HTML Accessibility: alt Text, Labels, Landmarks & ARIA 34. HTML Meta Tags & SEO: charset, viewport, OG Tags 35. HTML Quotations: blockquote, q, cite, abbr & address
Final Test — Certificate ₹10
Try this code live
Lesson 14 of 35

HTML Images: img Tag, src, alt, Width & Formats

4 min read · Free · No login needed

Images make a page feel alive. The <img> tag adds an image to a page. It is an empty element — no closing tag — and it needs two attributes to work well: src (the image file) and alt (a text description).

Diagram — see how it fits together
Infographic: parts of an HTML image tag

src and alt

src is the path to the image file: <img src="photo.jpg" alt="Students in a class">. The path can be relative (images/photo.jpg) or a full URL. alt is shown if the image fails to load, and it is read aloud by screen readers and read by Google. Never leave alt empty on a meaningful image — write a short, honest description of what the picture shows.

Width and height

Always set width and height. They reserve the space before the image loads, so the page does not jump around while loading. You can also set the size in CSS — that is the modern way — but the attributes still help the browser plan the layout.

Which image format?

  • JPG — photos. Small file size, good quality.
  • PNG — graphics, logos, and anything needing transparency.
  • WebP — the modern choice: smaller than JPG and PNG at the same quality.
  • GIF — simple animations only.
  • SVG — icons and logos that must stay sharp at any size.

Keep images as small as you can — resize them to the size they will actually appear, and compress them. A 5 MB photo slows the whole page down, and visitors on mobile data will leave before it loads.

Tip: Add loading="lazy" to images below the fold. The browser then loads them only when the visitor scrolls near them, so the page opens faster.

What the img tag is

The <img> tag places a picture on the page. It is an empty element — it has no end tag and no content. Everything it needs comes from its attributes: src says which image file to load, and alt says what the image shows. If either is missing, the image is broken or invisible to many users.

Image attributes

AttributeValueDescription
srca file pathWhere the image file is. Required.
alttextDescribes the image. Shown if it fails to load; read by screen readers. Required.
widthpixelsThe width of the image on the page.
heightpixelsThe height of the image on the page.
titletextA tooltip shown on hover.
loadinglazyLoads the image only when the user scrolls near it — faster pages.

Always set width and height. Without them, the browser does not know how much space to leave, and the page jumps around while images load:

<img src="lotus.jpg" alt="Orange lotus logo" width="300" height="200">
<img src="lotus.jpg" alt="Orange lotus logo" loading="lazy">
Note: The alt text should describe the image, not say "image of". Write alt="Students in a computer lab", not alt="image of students in a computer lab". If an image is purely decorative, use an empty alt="" so screen readers skip it.
Tip: Compress your images before uploading. A photo straight from a phone can be 4 MB; the same photo at web size is often under 200 KB. Smaller images mean a faster page and happier visitors.

Common mistakes

  • Leaving out alt — blind users and search engines then have no idea what the image is.
  • Setting only width in HTML and stretching the image out of shape — set both, or let CSS handle the size.
  • Using a huge image and shrinking it with width and height — the browser still downloads the huge file.
  • Writing the file name with the wrong capital letters — Photo.jpg and photo.jpg are different files on a server.

Example explained

  • <img src="classroom.jpg" alt="Students learning in a KAMALDNP classroom" width="600" height="400"> loads the photo file classroom.jpg, describes it with alt text, and reserves a 600 by 400 pixel space so the page does not jump while loading.
  • <img> has no closing tag — it is an empty element, so everything it needs comes from its attributes.
  • The second image sits inside a <p> tag, which simply puts the small logo on its own line as a block of content.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Images</title>
</head>
<body>

<img src="classroom.jpg" alt="Students learning in a KAMALDNP classroom" width="600" height="400">
<p><img src="logo.png" alt="KAMALDNP logo" width="120" height="120"></p>

</body>
</html>

Output

Output — how it looks in the browser
Output of HTML Images: img Tag, src, alt, Width & Formats

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. Which attributes does an img tag need?

src for the image file and alt for a text description. Never leave alt empty on a meaningful image.

2. Why should you set width and height on images?

They reserve space before the image loads, so the page does not jump around while loading.

3. Which image format should you choose?

JPG for photos, PNG for graphics and transparency, WebP as the modern smaller choice, and SVG for icons that must stay sharp.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Master Python + AI Skills in 30 Days
₹499
View
AI Prompt Engineering Webinar 2026
View
Ethical Hacking & Cyber Security Workshop
View
Browse all courses
HTML — Lessons
1. HTML Introduction: What is HTML? Learn with Examples 2. Best Free HTML Editors: Notepad to VS Code for Beginners 3. HTML Basic Structure: Doctype, Head, Body Explained 4. HTML Elements Explained: Tags, Content & Empty Elements 5. HTML Attributes List: href, src, alt, width, title & More 6. HTML Headings h1 to h6: Which Heading Tag to Use When 7. HTML Paragraphs: p Tag, br Line Breaks & pre Text 8. HTML Style Attribute: Colour, Font Size & Alignment 9. HTML Text Formatting Tags: bold, italic, mark & More 10. HTML Comments: How to Add Notes & Hide Code in HTML 11. HTML Colors: Names, HEX, RGB & HSL Colour Codes 12. HTML CSS: Inline, Internal & External Stylesheets 13. HTML Links: a Tag, href, New Tab & Email Links 14. HTML Images: img Tag, src, alt, Width & Formats 15. HTML Favicon: Create & Add a Browser Tab Icon 16. HTML Title Tag: How to Write a Good Page Title 17. HTML Tables: table, tr, th, td, colspan & rowspan 18. HTML Lists: ul, ol, dl & Nested Lists Explained 19. HTML Block vs Inline Elements: div, p, span Explained 20. HTML Div Tag: Group Content & Build Page Layouts 21. HTML Class Attribute: Style Many Elements at Once 22. HTML id Attribute: Unique IDs, Links, CSS & JavaScript 23. HTML Iframes: Embed YouTube, Maps & Pages in HTML 24. HTML JavaScript: script Tag, Clicks & First Example 25. HTML File Paths: Relative vs Absolute Paths Explained 26. HTML Head Element: title, meta, charset & viewport 27. HTML Layout: header, nav, main, footer Page Structure 28. HTML Responsive Design: Viewport, Images & Media Queries 29. Semantic HTML Tags: header, nav, main, article & footer 30. HTML Forms: input, label, select, checkbox & Submit 31. HTML Entities: & < > © with Examples 32. HTML Audio & Video Tags: controls, source & Fallback 33. HTML Accessibility: alt Text, Labels, Landmarks & ARIA 34. HTML Meta Tags & SEO: charset, viewport, OG Tags 35. HTML Quotations: blockquote, q, cite, abbr & address
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →