← Back to HTML Course | Chapter 3: Page Structure | Lesson 4 of 12

HTML Iframes

सोचिए आप अपने living room में बैठे हैं और दीवार पर एक high-tech TV screen लगा रहे हैं। लेकिन local cable channels दिखाने के बजाय, यह screen एक direct live portal है जो शहर के उस पार किसी park में हो रही घटनाओं को दिखा रही है। HTML iframe वही digital portal है। यह आपके webpage के अंदर embed किया गया एक portal है जो किसी दूसरी पूरी website को display करता है। आप iframes का उपयोग videos, interactive map locators, external widgets, या document previews को अपने page पर embed करने के लिए कर सकते हैं, बिना visitors को आपकी site छोड़े।
Syntax
markup
<iframe src="url" title="Description" width="width" height="height"></iframe>

Iframe क्या है?

एक iframe ('inline frame' का छोटा रूप) आपके मौजूदा webpage के अंदर किसी दूसरे HTML document को embed करने के लिए इस्तेमाल होता है। src attribute browser को उस बाहरी page का web address बताता है जिसे आप दिखाना चाहते हैं।

Note:
  • दूसरी services से maps या video players जैसे interactive widgets embed करने के लिए iframes का इस्तेमाल करें।
  • Accepted attributes:
  • src — embed करने वाले page का URL
  • srcdoc — render करने के लिए inline HTML
  • title — frame का accessible description
  • width / height — pixel dimensions
  • name — links के लिए target name
  • allow, sandbox, loading, referrerpolicy — बाद के sections में देखें
Warning: जो websites secure HTTPS protocols का उपयोग नहीं करतीं, उन्हें embed करने से बचें, क्योंकि browsers security कारणों से उन्हें block कर देंगे।

उदाहरण: What is an Iframe?

markup
<iframe src="https://example.com"></iframe>

Iframe के Dimensions को Control करना

Default रूप से, browsers iframes को एक छोटे, पहले से तय box में दिखाते हैं। आप अपने layout के हिसाब से फिट करने के लिए HTML attributes या CSS properties का इस्तेमाल करके उनकी width और height आसानी से control कर सकते हैं।

Note:
  • fluid, mobile-friendly iframes बनाने के लिए CSS styling इस्तेमाल करना ही सबसे अच्छा तरीका है।
  • Accepted attributes:
  • width — integer pixels (या CSS width)
  • height — integer pixels (या CSS height)
  • frameborder, scrolling — obsolete; इसके बजाय CSS इस्तेमाल करें
Warning: अगर आपका iframe बहुत narrow है, तो embedded website cramped हो जाएगी और users के लिए पढ़ना मुश्किल हो जाएगा।

उदाहरण: Controlling Iframe Dimensions

markup
<iframe src="https://example.com" width="600" height="400"></iframe>

Iframe के Borders को Customize करना

Default रूप से, browsers iframes को एक गहरे रंग के boxy border के साथ render करते हैं, जो आपकी website के design से मेल नहीं खाता। आप CSS properties से इस border को आसानी से हटा या customize कर सकते हैं।

Note: embedded content को अपने page में smoothly blend करने के लिए अपनी CSS में border property को none पर सेट करें।
Warning: border हटाने से users के लिए यह पहचानना मुश्किल हो सकता है कि आपका webpage कहाँ खत्म होता है और embedded page कहाँ शुरू होता है।

उदाहरण: Customizing Iframe Borders

markup
<iframe src="https://example.com" style="border: none;"></iframe>

Security और sandbox Attribute

बाहरी websites को embed करना जोखिम भरा हो सकता है, क्योंकि वे आपके page पर अनचाहे scripts चला सकती हैं।

sandbox attribute embedded page की permissions को सीमित कर देता है, जिससे वह malicious scripts चलाने या pop-ups भेजने से रुक जाता है, जब तक आप स्पष्ट रूप से उसकी अनुमति न दें।

Note:
  • जब भी आप ऐसी websites embed करें जिन पर आपका पूरा control नहीं है, तो हमेशा sandbox attribute इस्तेमाल करें।
  • Accepted values:
  • sandbox (खाली) — सभी restrictions लागू करें
  • allow-scripts — JavaScript की अनुमति दें
  • allow-same-origin — origin बनाए रखें
  • allow-forms — form submission की अनुमति दें
  • allow-popups — window.open और target=_blank की अनुमति दें
  • allow-top-navigation — top page navigate करने की अनुमति दें
  • allow-modals — alert, confirm, prompt की अनुमति दें
Warning: sandbox attribute को पूरी तरह empty छोड़ने से embedded page पर सभी scripts, forms, और pop-ups disable हो जाएंगे, जिससे उसके features टूट सकते हैं।

उदाहरण: Security and the sandbox Attribute

markup
<iframe src="https://example.com" sandbox="allow-scripts"></iframe>

Iframes के अंदर Links को Target करना

आप iframes का इस्तेमाल करके ऐसे interactive portals बना सकते हैं जहाँ आपके main page पर किसी link पर क्लिक करने से नया page सीधे iframe के अंदर load हो जाए। ऐसा करने के लिए आप अपने iframe को एक नाम देते हैं और अपने links के target attributes में उस नाम को reference करते हैं।

Note:
  • यह technique सरल document portals या interactive sidebars बनाने के लिए बढ़िया है।
  • Accepted attributes:
  • iframe name — target के रूप में इस्तेमाल होने वाला identifier
  • a target — iframe name | _self | _blank | _parent | _top
Warning: कई modern websites security headers का उपयोग करती हैं जो उन्हें iframes के अंदर load होने से रोकते हैं, जिससे एक खाली block दिखाई देता है।

उदाहरण: Targeting Links Inside Iframes

markup
<iframe name="portal" src="page1.html"></iframe>
<a href="page2.html" target="portal">Load page 2 in iframe</a>

Lazy Loading Iframes

images की तरह ही, किसी iframe में loading="lazy" जोड़ने से browser को यह संकेत मिलता है कि जब तक iframe स्क्रीन पर scroll होकर आने वाला न हो, उसका content load करना टालें।

यह खासतौर पर page के नीचे embedded maps या videos के लिए काफी फायदेमंद है, जो अन्यथा initial page load को काफी धीमा कर सकते हैं।

Note:
  • पहले load के समय दिखने वाले screen area से नीचे मौजूद iframes को हमेशा lazy load करें, जैसे किसी contact page के नीचे embedded map।
  • Accepted values:
  • loading — lazy | eager (default)
Warning: जो iframe page load होते ही तुरंत दिखाई देता है, उसे lazy load न करें — इससे कोई फायदा नहीं मिलता और कभी-कभी एक छोटा-सा visible delay हो सकता है।

उदाहरण: Lazy Loading Iframes

markup
<iframe src="https://maps.example.com" loading="lazy"></iframe>

Accessibility के लिए title Attribute

हर iframe में एक title attribute होना चाहिए जो यह बताए कि उसमें क्या है, क्योंकि screen readers यह title सुनाते हैं ताकि users interact करने का फैसला लेने से पहले embedded content का purpose समझ सकें।

Note:
  • iframe titles उसी तरह लिखें जैसे अच्छा alt text लिखते हैं — संक्षिप्त और असली content के बारे में स्पष्ट, generic नहीं।
  • Accepted attributes:
  • title — frame content के बारे में छोटा text; screen readers इसे पढ़ते हैं
Warning: बिना title attribute वाले iframe को screen reader users को अक्सर एक unlabeled frame के रूप में announce किया जाता है, जिससे उन्हें यह पता नहीं चलता कि उसमें क्या है।

उदाहरण: The title Attribute for Accessibility

markup
<iframe src="https://example.com" title="Customer support chat widget"></iframe>

referrerpolicy Attribute

referrerpolicy attribute यह control करता है कि आपके page की कितनी जानकारी Referer header में embedded iframe की destination तक भेजी जाए।

no-referrer या strict-origin जैसी strict policy सेट करने से embedded third-party content से आपके users की browsing privacy सुरक्षित रहती है।

Note:
  • एक reasonable default के तौर पर referrerpolicy="strict-origin-when-cross-origin" इस्तेमाल करें, जो privacy और उपयोगी analytics data भेजने के बीच संतुलन बनाता है।
  • Accepted values:
  • no-referrer
  • no-referrer-when-downgrade
  • origin
  • origin-when-cross-origin
  • same-origin
  • strict-origin
  • strict-origin-when-cross-origin
  • unsafe-url
Warning: default referrer behavior आपके page का पूरा URL, यहां तक कि sensitive query parameters सहित, हर embedded iframe की destination तक leak कर सकता है।

उदाहरण: The referrerpolicy Attribute

markup
<iframe src="https://example.com" referrerpolicy="no-referrer"></iframe>

आम Iframe Use Cases

Iframes का इस्तेमाल आमतौर पर maps, video players, payment widgets, social media posts, और third-party comment systems embed करने के लिए किया जाता है — यानी हर वह जगह जहाँ आप किसी दूसरी service द्वारा control किए गए content को खुद बनाए बिना शामिल करना चाहते हैं।

Note: जब भी कोई third-party service official embed code देती है, तो खुद से लिखने के बजाय उनके सुझाए गए exact iframe attributes इस्तेमाल करें।
Warning: एक ही page पर बहुत सारे third-party iframes embed करना, जहां हर एक अपनी scripts और trackers load करता है, आपके overall page performance को गंभीर रूप से धीमा कर सकता है।

उदाहरण: Common Iframe Use Cases

markup
<iframe src="https://maps.example.com/embed" title="Store location map"></iframe>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.