HTML Images
In this page:
<img src="image-url" alt="Alternative text" width="width" height="height">
img Tag से Images Embed करना
किसी webpage पर images display करने के लिए, आप img tag का उपयोग करते हैं। यह एक self-closing element है, यानी यह content को नहीं लपेटता और इसे closing tag की ज़रूरत नहीं होती।
यह file path की ओर इशारा करने के लिए src (source) attribute और accessibility के लिए graphic का वर्णन करने वाले alt (alternative text) attribute पर निर्भर करता है।
- हर image के लिए स्पष्ट, contextual alt text लिखें ताकि screen readers और search bots आपके graphics को समझ सकें।
- Accepted attributes:
- src — image file का URL
- alt — image लोड न होने पर या screen readers द्वारा पढ़े जाने के लिए text alternative
- width / height — pixels में intrinsic size (integers, बिना units के)
- loading — lazy | eager
उदाहरण: Embedding Images with the img Tag
<img src="photo.jpg" alt="A photo description">
Dimensions से Layout Shifts रोकना
जब कोई image लोड होती है, तो उसे download होने में कुछ seconds लगते हैं। अगर आप HTML में इसकी width और height specify नहीं करते, तो ब्राउज़र को पता नहीं चलेगा कि कितनी जगह reserve करनी है, जिससे page layout इधर-उधर उछलने लगता है।
ये dimensions explicitly सेट करने से ब्राउज़र को पहले से ठीक-ठीक पता चल जाता है कि कितनी जगह lock करनी है।
- HTML tags के अंदर हमेशा native aspect ratio वाली width और height integers के रूप में specify करें, और image को dynamically scale करने के लिए CSS का उपयोग करें।
- Accepted attributes:
- width — pixels में integer
- height — pixels में integer
- aspect ratio के ज़रिए ब्राउज़र को जगह reserve करने देने के लिए दोनों सेट करें
उदाहरण: Preventing Layout Shifts with Dimensions
<img src="photo.jpg" alt="Photo" width="600" height="400">
सबसे अच्छे Web Image Formats चुनना
Web performance काफी हद तक सही image format चुनने पर निर्भर करती है।
Photographic content के लिए JPEG, transparent logos के लिए PNG, scalable vector icons के लिए SVG, और file sizes छोटा व load speeds तेज़ रखने वाले rich compression के लिए WebP या AVIF जैसे modern formats का उपयोग करें।
उदाहरण: Choosing the Best Web Image Formats
<img src="photo.jpg" alt="Photo">
<img src="logo.png" alt="Logo">
<img src="icon.svg" alt="Icon">
<img src="banner.webp" alt="Banner">
Figure के साथ Semantic Image Grouping
किसी image को visible text caption के साथ bundle करने के लिए, आपको semantic figure और figcaption elements का उपयोग करना चाहिए।
यह image और उसके text को एक single, self-contained unit में लपेटता है, जो search engines और assistive devices को बताता है कि text सीधे image का वर्णन करता है।
उदाहरण: Semantic Image Grouping with Figure
<figure>
<img src="chart.jpg" alt="Sales chart">
<figcaption>Quarterly sales chart</figcaption>
</figure>
Offscreen Images को Lazy Load करना
अगर किसी page में कई images हैं, तो उन सभी को एक साथ download करना initial load speed को धीमा कर सकता है। loading="lazy" attribute जोड़कर, आप ब्राउज़र को निर्देश देते हैं कि जब तक user scroll करके पास न आए, तब तक image लोड करने में देर करे।
- fold के नीचे स्थित सभी images के लिए lazy loading का उपयोग करें, लेकिन इसे page के बिल्कुल ऊपर वाली hero images पर apply न करें।
- Accepted values:
- loading — lazy | eager (default)
- iframe पर भी valid
- fetchpriority — high | low | auto
उदाहरण: Lazy Loading Offscreen Images
<img src="offscreen.jpg" alt="Offscreen image" loading="lazy">
srcset से Responsive Images
srcset attribute आपको अलग-अलग screen resolutions के लिए अलग-अलग image sizes देने देता है। ब्राउज़र स्वचालित रूप से सबसे उपयुक्त size चुनता है और mobile devices पर bandwidth बचाता है।
- important images के लिए हमेशा कम से कम 1x और 2x version दें ताकि retina screens पर वे ठीक दिखें।
- Accepted attributes:
- srcset — w या x descriptors वाले comma-separated URLs
- sizes — slot widths वाली media conditions, जैसे (max-width: 600px) 100vw, 50vw
- src — fallback image URL
उदाहरण: Responsive Images with srcset
<img src="photo.jpg" srcset="photo.jpg 1x, [email protected] 2x" alt="Photo">
Image Maps (map और area)
एक image map आपको map element को हर clickable shape define करने वाले area tags के साथ मिलाकर, किसी single image के अलग-अलग regions को अलग-अलग clickable links में बदलने देता है।
यह किसी diagram जैसी चीज़ के लिए उपयोगी है जहां अलग-अलग हिस्से अलग-अलग pages से link हों।
- हर area element में हमेशा एक alt attribute जोड़ें ताकि screen reader users को समझ आए कि हर clickable region क्या करता है।
- Accepted attributes:
- map name — usemap="#name" द्वारा referenced identifier
- area shape — rect | circle | poly | default
- area coords — shape के अनुसार comma-separated numbers
- area href / alt — link target और आवश्यक alt text
उदाहरण: Image Maps (map and area)
<img src="diagram.jpg" alt="Diagram" usemap="#map1">
<map name="map1">
<area shape="rect" coords="0,0,100,100" href="part1.html" alt="Part 1">
</map>
Background बनाम Content Images
img tag से जोड़ी गई एक content image page के असल अर्थ का हिस्सा होती है और उसके पास हमेशा descriptive alt text होना चाहिए — जैसे product photos या diagrams।
CSS background-image से जोड़ी गई एक background image विशुद्ध decorative styling है और screen readers के लिए invisible होती है, जो subtle patterns या hero section backdrops जैसी चीज़ों के लिए बिल्कुल सही है।
उदाहरण: Background vs Content Images
<img src="product.jpg" alt="Red running shoe">
<div style="background-image: url('pattern.png');"></div>
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- HTML Favicon
- HTML Page Title
- HTML Tables
- HTML Lists
- HTML Block & Inline
- HTML Comments
- HTML Colors
- HTML CSS
- HTML Links
- HTML Images
- HTML RGB Colors
- HTML HEX Colors
- HTML HSL Colors
- HTML Link Colors
- HTML Link Bookmarks
- HTML Background Images
- HTML Table Borders
- HTML Table Sizes
- HTML Table Headers
- HTML Table Padding & Spacing
- HTML Colspan & Rowspan
- HTML Table Styling
- HTML Table Colgroup
- HTML Unordered Lists
- HTML Ordered Lists
- HTML Other Lists / Description Lists