Learn how to add images to a webpage using the <img> tag with attributes like src, alt, width, and height.
To create an HTML webpage that demonstrates how to add images using the <img> tag with attributes like src, alt, width, and height.
The <img> tag is used to embed images in an HTML webpage. It is a self-closing tag (no closing tag needed). The main attributes are:
src — specifies the path to the image file (URL or local path)alt — provides alternative text if the image cannot be displayedwidth — sets the width of the image in pixelsheight — sets the height of the image in pixelsalt attribute for accessibility. It helps screen readers describe the image to visually impaired users and is also displayed if the image fails to load.
src: "image1.jpg"
Width: 200px, Height: 120px
src: "image2.png"
Width: 200px, Height: 120px
src: "image3.gif"
Width: 200px, Height: 120px
src: "image4.webp"
Width: 200px, Height: 120px
<img src="photo.jpg" alt="A beautiful sunset over the mountains">
<img src="photo.jpg"> (missing alt text)
80x80 (square)
150x80 (wide)
80x120 (tall)
src attribute.| Attribute | Purpose | Example |
|---|---|---|
src |
Specifies the image file path or URL | src="image.jpg" |
alt |
Alternative text for accessibility | alt="A beautiful sunset" |
width |
Sets image width in pixels | width="300" |
height |
Sets image height in pixels | height="200" |
title |
Tooltip text on mouse hover | title="Click to enlarge" |
| Source Type | Example | Explanation |
|---|---|---|
| Local File | src="image.jpg" |
Image in same folder as HTML file |
| Subfolder | src="images/photo.jpg" |
Image in a subfolder called "images" |
| Absolute Path | src="C:/Users/Pictures/photo.jpg" |
Full file path (not recommended) |
| Online URL | src="https://example.com/image.jpg" |
Image hosted on the web |
| Mistake | Why It's Wrong | Correct Way |
|---|---|---|
Forgetting alt attribute |
Poor accessibility | Always add alt text |
| Wrong file path | Image won't display | Check file path and extension |
| Not closing the tag | <img> is self-closing |
Use <img ...> not <img>...</img> |
| Distorted image | Width and height mismatch | Use height: auto; or maintain aspect ratio |
| Using spaces in file names | May cause loading issues | Use underscores or hyphens (image_file.jpg) |
<img> tag is self-closing and does not need a closing tag.src attribute is required — without it, the image won't load.alt attribute is important for accessibility and SEO.<a> tag.You have successfully:
<img> tag.src, alt, width, and height attributes.alt attribute for accessibility and SEO. Use meaningful, descriptive text that explains what the image shows. This helps users with visual impairments and search engines understand your content.
ایک HTML ویب صفحہ بنانا جو <img> ٹیگ کو src، alt، width، اور height صفات کے ساتھ استعمال کرتے ہوئے تصاویر شامل کرنے کا طریقہ ظاہر کرتا ہے۔
<img> ٹیگ HTML ویب صفحے میں تصاویر شامل کرنے کے لیے استعمال ہوتا ہے۔ یہ ایک خود بند ہونے والا ٹیگ ہے (اسے بند کرنے والے ٹیگ کی ضرورت نہیں)۔ اس کی اہم صفات یہ ہیں:
src — تصویری فائل کا راستہ مخصوص کرتا ہے (URL یا مقامی پاتھ)alt — متبادل متن فراہم کرتا ہے اگر تصویر ظاہر نہ ہو سکےwidth — تصویر کی چوڑائی پکسلز میں سیٹ کرتا ہےheight — تصویر کی اونچائی پکسلز میں سیٹ کرتا ہےalt صفت کو رسائی کے لیے استعمال کریں۔ یہ اسکرین ریڈرز کو بصارت سے محروم صارفین کے لیے تصویر کی وضاحت کرنے میں مدد کرتا ہے اور اگر تصویر لوڈ نہ ہو تو بھی ظاہر ہوتا ہے۔
src: "image1.jpg"
چوڑائی: 200px، اونچائی: 120px
src: "image2.png"
چوڑائی: 200px، اونچائی: 120px
src: "image3.gif"
چوڑائی: 200px، اونچائی: 120px
src: "image4.webp"
چوڑائی: 200px، اونچائی: 120px
<img src="photo.jpg" alt="پہاڑوں پر ایک خوبصورت غروب آفتاب">
<img src="photo.jpg"> (alt متن موجود نہیں)
80x80 (مربع)
150x80 (چوڑا)
80x120 (لمبا)
src صفت میں آن لائن تصویری URLs استعمال کریں۔| صفت | مقصد | مثال |
|---|---|---|
src |
تصویری فائل کا راستہ یا URL مخصوص کرتا ہے | src="image.jpg" |
alt |
رسائی کے لیے متبادل متن | alt="ایک خوبصورت غروب آفتاب" |
width |
پکسلز میں تصویر کی چوڑائی سیٹ کرتا ہے | width="300" |
height |
پکسلز میں تصویر کی اونچائی سیٹ کرتا ہے | height="200" |
title |
ماؤس ہوور پر ٹولٹپ متن | title="بڑا کرنے کے لیے کلک کریں" |
| ماخذ کی قسم | مثال | وضاحت |
|---|---|---|
| مقامی فائل | src="image.jpg" |
HTML فائل والے فولڈر میں تصویر |
| ذیلی فولڈر | src="images/photo.jpg" |
"images" نامی ذیلی فولڈر میں تصویر |
| مکمل پاتھ | src="C:/Users/Pictures/photo.jpg" |
مکمل فائل پاتھ (تجویز نہیں) |
| آن لائن URL | src="https://example.com/image.jpg" |
ویب پر ہوسٹ کردہ تصویر |
| غلطی | یہ کیوں غلط ہے | صحیح طریقہ |
|---|---|---|
alt صفت بھول جانا |
خراب رسائی | ہمیشہ alt متن شامل کریں |
| غلط فائل پاتھ | تصویر ظاہر نہیں ہوگی | فائل پاتھ اور ایکسٹینشن چیک کریں |
| ٹیگ بند نہ کرنا | <img> خود بند ہونے والا ہے |
<img ...> استعمال کریں، <img>...</img> نہیں |
| بگڑی ہوئی تصویر | چوڑائی اور اونچائی کا میل نہ ہونا | height: auto; استعمال کریں یا تناسب برقرار رکھیں |
| فائل ناموں میں اسپیس کا استعمال | لوڈنگ کے مسائل کا سبب بن سکتا ہے | انڈر سکور یا ہائفن استعمال کریں (image_file.jpg) |
<img> ٹیگ خود بند ہونے والا ہے اور اسے بند کرنے والے ٹیگ کی ضرورت نہیں۔src صفت ضروری ہے — اس کے بغیر تصویر لوڈ نہیں ہوگی۔alt صفت رسائی اور SEO کے لیے اہم ہے۔<a> ٹیگ میں لپیٹ کر۔آپ نے کامیابی سے:
<img> ٹیگ کا استعمال کرتے ہوئے تصاویر شامل کرنے والا ایک HTML ویب صفحہ بنایا۔src، alt، width، اور height صفات کا استعمال کیا۔alt صفت استعمال کریں۔ معنی خیز، وضاحتی متن استعمال کریں جو بتاتا ہے کہ تصویر کیا دکھاتی ہے۔ یہ بصارت سے محروم صارفین اور سرچ انجنوں کو آپ کے مواد کو سمجھنے میں مدد کرتا ہے۔