Learn how to use the height, width, and alt attributes with the <img> tag to control image display and accessibility.
To create an HTML webpage that demonstrates the use of height, width, and alt attributes with the <img> tag.
The <img> tag uses three important attributes to control how images are displayed:
width — Specifies the width of the image in pixels (or percentage).height — Specifies the height of the image in pixels (or percentage).alt — Provides alternative text if the image cannot be displayed; essential for accessibility.width and height helps the browser reserve space for the image before it loads, preventing layout shifts. The alt attribute is required for accessibility and SEO.
width="80" height="80"
width="150" height="80"
width="200" height="100"
width="80" height="120"
width="150" height="150"
<img src="sunset.jpg" alt="A beautiful sunset over the mountains with orange and purple sky">
<img src="sunset.jpg" alt="image">
<img src="sunset.jpg">
width="120" height="80"
✓ Correct ratio
width="120" height="120"
⚠️ Stretched (distorted)
| Attribute | Purpose | Example | Best Practice |
|---|---|---|---|
width |
Sets image width in pixels or percentage | width="300" or style="width: 50%;" |
Use with height: auto; to maintain aspect ratio |
height |
Sets image height in pixels or percentage | height="200" or style="height: auto;" |
Let the browser calculate height automatically |
alt |
Provides alternative text for accessibility | alt="A red apple on a wooden table" |
Be descriptive; explain what the image shows |
| Width | Height | Result | Recommendation |
|---|---|---|---|
| Set | Set | Image resized exactly (may stretch) | Use only if you know the exact aspect ratio |
| Set | auto |
Image scales proportionally | ✅ Recommended — preserves aspect ratio |
| Set as percentage | auto |
Responsive image | ✅ Best for responsive design |
| Not set | Not set | Image displays at original size | ⚠️ May cause layout shifts |
| Good Alt Text | Poor Alt Text | Why |
|---|---|---|
| "A golden retriever puppy playing in a grassy park" | "dog" | Too vague; doesn't describe the scene |
| "The Eiffel Tower illuminated at night with a full moon" | "tower" | Doesn't provide enough context |
| "A fresh chocolate chip cookie on a cooling rack" | "cookie" | Missing important details |
| "Bar chart showing company growth from 2019 to 2025" | "chart" | Should describe the data being shown |
| Mistake | Why It's Wrong | Correct Way |
|---|---|---|
Missing alt attribute |
Poor accessibility and SEO | Always include descriptive alt text |
| Using width and height that don't match aspect ratio | Image becomes stretched or squashed | Use height: auto; or correct ratio |
Using alt="image" or alt="photo" |
Not descriptive enough | Describe what the image actually shows |
| Setting both width and height as percentages | May cause unpredictable sizing | Use width as percentage, height as auto |
| Leaving alt empty for decorative images | Should use alt="" for decorative images |
Use alt="" to hide from screen readers |
width and height attributes control the display size of images.height: auto; with width preserves the aspect ratio.alt attribute is essential for accessibility and SEO.alt text helps visually impaired users understand the content.You have successfully:
height, width, and alt attributes with the <img> tag.alt attribute with descriptive text. Use height: auto; to maintain aspect ratio when setting width. This ensures your images look good and your content is accessible to everyone.
object-fit property to control how images fill their containers. Practice writing descriptive alt text for different types of images!
ایک HTML ویب صفحہ بنانا جو height، width، اور alt صفات کو <img> ٹیگ کے ساتھ استعمال کرنے کا طریقہ ظاہر کرتا ہے۔
<img> ٹیگ تین اہم صفات استعمال کرتا ہے جو تصاویر کی نمائش کو کنٹرول کرتی ہیں:
width — تصویر کی چوڑائی پکسلز (یا فیصد) میں مخصوص کرتا ہے۔height — تصویر کی اونچائی پکسلز (یا فیصد) میں مخصوص کرتا ہے۔alt — اگر تصویر ظاہر نہ ہو سکے تو متبادل متن فراہم کرتا ہے؛ رسائی کے لیے ضروری ہے۔width اور height کا استعمال براؤزر کو تصویر لوڈ ہونے سے پہلے جگہ محفوظ کرنے میں مدد کرتا ہے، جس سے لے آؤٹ میں تبدیلی نہیں آتی۔ alt صفت رسائی اور SEO کے لیے ضروری ہے۔
width="80" height="80"
width="150" height="80"
width="200" height="100"
width="80" height="120"
width="150" height="150"
<img src="sunset.jpg" alt="پہاڑوں پر نارنجی اور جامنی آسمان کے ساتھ ایک خوبصورت غروب آفتاب">
<img src="sunset.jpg" alt="تصویر">
<img src="sunset.jpg">
width="120" height="80"
✓ صحیح تناسب
width="120" height="120"
⚠️ بگڑی ہوئی (مسخ شدہ)
| صفت | مقصد | مثال | بہترین عمل |
|---|---|---|---|
width |
پکسلز یا فیصد میں تصویر کی چوڑائی سیٹ کرتا ہے | width="300" یا style="width: 50%;" |
تناسب برقرار رکھنے کے لیے height: auto; کے ساتھ استعمال کریں |
height |
پکسلز یا فیصد میں تصویر کی اونچائی سیٹ کرتا ہے | height="200" یا style="height: auto;" |
براؤزر کو خود بخود اونچائی کا حساب لگانے دیں |
alt |
رسائی کے لیے متبادل متن فراہم کرتا ہے | alt="لکڑی کی میز پر ایک سرخ سیب" |
وضاحتی بنیں؛ بتائیں کہ تصویر کیا دکھاتی ہے |
| چوڑائی | اونچائی | نتیجہ | توصیف |
|---|---|---|---|
| مقرر | مقرر | تصویر بالکل سائز تبدیل ہوتی ہے (بگڑ سکتی ہے) | صرف اس صورت میں استعمال کریں جب آپ کو صحیح تناسب معلوم ہو |
| مقرر | auto |
تصویر متناسب طور پر پیمانہ کرتی ہے | ✅ تجویز کردہ — تناسب محفوظ رکھتی ہے |
| فیصد کے طور پر مقرر | auto |
ذمہ دار تصویر | ✅ ذمہ دار ڈیزائن کے لیے بہترین |
| مقرر نہیں | مقرر نہیں | تصویر اصل سائز میں ظاہر ہوتی ہے | ⚠️ لے آؤٹ میں تبدیلی کا سبب بن سکتی ہے |
| اچھا Alt متن | ناقص Alt متن | کیوں |
|---|---|---|
| "گھاس کے پارک میں ایک سنہری ریٹریور کتے کا بچہ کھیل رہا ہے" | "کتا" | بہت مبہم؛ منظر کی وضاحت نہیں کرتا |
| "پورے چاند کے ساتھ رات میں روشن آئیفل ٹاور" | "ٹاور" | کافی سیاق و سباق فراہم نہیں کرتا |
| "کولنگ ریک پر ایک تازہ چاکلیٹ چپ کوکی" | "کوکی" | اہم تفصیلات غائب ہیں |
| "2019 سے 2025 تک کمپنی کی نمو دکھانے والا بار چارٹ" | "چارٹ" | دکھائے گئے ڈیٹا کی وضاحت کرنی چاہیے |
| غلطی | یہ کیوں غلط ہے | صحیح طریقہ |
|---|---|---|
alt صفت غائب |
ناقص رسائی اور SEO | ہمیشہ وضاحتی alt متن شامل کریں |
| چوڑائی اور اونچائی کا استعمال جو تناسب سے میل نہیں کھاتا | تصویر بگڑ جاتی ہے یا دب جاتی ہے | height: auto; یا صحیح تناسب استعمال کریں |
alt="image" یا alt="photo" کا استعمال |
کافی وضاحتی نہیں | بیان کریں کہ تصویر اصل میں کیا دکھاتی ہے |
| چوڑائی اور اونچائی دونوں کو فیصد میں سیٹ کرنا | پیش گوئی کے قابل سائز کا سبب بن سکتا ہے | چوڑائی کو فیصد کے طور پر، اونچائی کو auto کے طور پر استعمال کریں |
| آرائشی تصاویر کے لیے alt خالی چھوڑنا | آرائشی تصاویر کے لیے alt="" استعمال کرنا چاہیے |
اسکرین ریڈرز سے چھپانے کے لیے alt="" استعمال کریں |
width اور height صفات تصاویر کی نمائش کے سائز کو کنٹرول کرتی ہیں۔height: auto; کے ساتھ width کا استعمال تناسب کو محفوظ رکھتا ہے۔alt صفت رسائی اور SEO کے لیے ضروری ہے۔alt متن بصارت سے محروم صارفین کو مواد کو سمجھنے میں مدد کرتا ہے۔آپ نے کامیابی سے:
height، width، اور alt صفات کو <img> ٹیگ کے ساتھ استعمال کیا۔alt صفت کو وضاحتی متن کے ساتھ شامل کریں۔ width سیٹ کرتے وقت تناسب برقرار رکھنے کے لیے height: auto; استعمال کریں۔ اس سے یقینی ہوتا ہے کہ آپ کی تصاویر اچھی لگتی ہیں اور آپ کا مواد سب کے لیے قابل رسائی ہے۔
object-fit پراپرٹی کے ساتھ تجربہ کریں۔ مختلف قسم کی تصاویر کے لیے وضاحتی alt متن لکھنے کی مشق کریں!