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).
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.
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.
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.
loading="lazy" to images below the fold. The browser then loads them only when the visitor scrolls near them, so the page opens faster.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.
| Attribute | Value | Description |
|---|---|---|
src | a file path | Where the image file is. Required. |
alt | text | Describes the image. Shown if it fails to load; read by screen readers. Required. |
width | pixels | The width of the image on the page. |
height | pixels | The height of the image on the page. |
title | text | A tooltip shown on hover. |
loading | lazy | Loads 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">
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.alt — blind users and search engines then have no idea what the image is.width in HTML and stretching the image out of shape — set both, or let CSS handle the size.Photo.jpg and photo.jpg are different files on a server.<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.<p> tag, which simply puts the small logo on its own line as a block of content.<!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>
Change the code below and press Run to see the result instantly. No file to save, no login needed.
The questions learners ask most about this lesson — read the question, then open it to see the answer.
src for the image file and alt for a text description. Never leave alt empty on a meaningful image.
They reserve space before the image loads, so the page does not jump around while loading.
JPG for photos, PNG for graphics and transparency, WebP as the modern smaller choice, and SVG for icons that must stay sharp.
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.
Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.
Take the testPrefer watching? Video courses teach the same topics step by step.
Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.
Browse all coursesAsk about courses, website & more
Request human callback:
Please log in to download. You will stay on this page after logging in!
New here? Register now →