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

HTML File Paths

सोचिए आप अपने friend को अपनी kitchen में coffee mug ढूंढने का तरीका बता रहे हैं। आप उन्हें mug के exact global GPS coordinates दे सकते हैं, या बस इतना कह सकते हैं: 'अपने सिर के ठीक ऊपर वाला cabinet खोलो।' HTML file paths बिल्कुल इसी तरह काम करते हैं। ये web browser को बताते हैं कि आपकी stylesheets, scripts, और logos कहाँ खोजें। आप 'absolute paths' (global GPS coordinates) या 'relative paths' (current webpage फ़ाइल कहाँ है, उसके आधार पर instructions) का उपयोग कर सकते हैं। इसे सही करना ही एक खूबसूरती से render होती website और missing-image symbols से भरे टूटे हुए page के बीच का फर्क तय करता है।
Syntax
markup
<img src="folder/file.ext">
<img src="/folder/file.ext">
<img src="../file.ext">
<img src="https://domain.com/file.ext">

Relative Paths - एक जैसी Directory

जब HTML फ़ाइल और वह asset जिसे आप load करना चाहते हैं, दोनों एक ही फ़ोल्डर में स्थित हों, तो आपको किसी भी फ़ोल्डर नाम या slashes लिखने की जरूरत नहीं है। आप सीधे filename को reference कर देते हैं।

Note: सिर्फ filename लिखना सबसे सरल relative path है, जो पास-पास मौजूद फ़ाइलों को perfectly जोड़े रखता है।
Warning: सुनिश्चित करें कि filename की spelling और extension बिल्कुल सही हों, क्योंकि web servers highly case-sensitive होते हैं।

उदाहरण: Relative Paths - Same Directory

markup
<img src="photo.jpg" alt="Photo">

Relative Paths - Subfolders में नीचे जाना

व्यवस्थित web projects में, assets आमतौर पर images या css जैसे subfolders में sort किए जाते हैं। इन्हें target करने के लिए, आप subfolder का नाम, उसके बाद एक forward slash, और फिर filename लिखते हैं।

Note: एक जैसे assets को अलग-अलग subdirectories में sort करके अपने root फ़ोल्डर को साफ रखें।
Warning: अपने paths में कभी भी backslashes का उपयोग न करें, क्योंकि ये local Windows systems के लिए डिज़ाइन किए गए हैं और public web servers पर टूट जाएंगे।

उदाहरण: Relative Paths - Down into Subfolders

markup
<img src="images/photo.jpg" alt="Photo">

Relative Paths - Parent Folders में ऊपर जाना

अगर आपकी HTML फ़ाइल किसी nested subfolder के अंदर है और उसे directory hierarchy में एक स्तर ऊपर स्थित किसी asset को load करना है, तो आप browser को मौजूदा फ़ोल्डर से बाहर निकलने के लिए एक double dot sequence (../) इस्तेमाल करते हैं।

Note: हर double-dot sequence browser को directory में ठीक एक level ऊपर ले जाता है।
Warning: अगर आप बहुत ज्यादा levels ऊपर जाते हैं, तो browser आपकी local directory structure का track खो देगा और फ़ाइल load करने में असफल रहेगा।

उदाहरण: Relative Paths - Up into Parent Folders

markup
<img src="../images/photo.jpg" alt="Photo">

Absolute File Paths

एक absolute path एक पूरा, global web address होता है जिसमें secure domain name और protocol शामिल होता है। यह internet पर किसी fixed location को point करता है, जिससे आप दुनिया में कहीं भी hosted assets को load कर सकते हैं।

Note: उन बाहरी resources को link करते समय absolute paths इस्तेमाल करें जिन्हें आप खुद अपने server पर host नहीं करते।
Warning: अगर third-party server offline हो जाए या अपना URL बदल दे, तो आपके absolute path links तुरंत टूट जाएंगे।

उदाहरण: Absolute File Paths

markup
<img src="https://example.com/images/photo.jpg" alt="Photo">

Best Practice में Relative Paths क्यों पसंद किए जाते हैं

जब आप कोई website बनाते हैं, तो प्रोजेक्ट को public domain पर upload करने से पहले उस पर अपने computer में locally काम करते हैं।

Relative paths यह सुनिश्चित करते हैं कि आपके links आपकी local hard drive पर पूरी तरह काम करें और बिना किसी बदलाव के automatically internet पर भी सही तरीके से transfer हो जाएं।

Note: अपने पूरे प्रोजेक्ट फ़ोल्डर को portable बनाने के लिए कोड में relative paths का इस्तेमाल करें।
Warning: C:/projects/index.html जैसे absolute computer paths का उपयोग करना, आपके code को live host पर upload करते ही पूरी तरह टूट जाएगा।

उदाहरण: Why Best Practice Prefers Relative Paths

markup
<link rel="stylesheet" href="css/styles.css">
<img src="images/logo.png" alt="Logo">

URL बनाम File Path: क्या फ़र्क है

एक file path यह बताता है कि कोई फ़ाइल किसी computer के storage पर भौतिक रूप से कहाँ है, जैसे Windows पर C:/projects/site/logo.png।

एक URL यह बताता है कि किसी resource को internet पर कहाँ ढूंढा और request किया जा सकता है, जैसे https://cookiescursor.com/logo.png। जब आप HTML में कोई relative link लिखते हैं, तो browser उसे एक literal file path में नहीं बल्कि पूरे URL में बदल देता है।

Note: HTML में जो src या href value आप लिखते हैं, उसे हमेशा एक URL का हिस्सा बनते हुए सोचें, भले ही वह देखने में एक साधारण relative file path जैसा लगे।
Warning: जो path आपके अपने computer पर फ़ाइल सीधे खोलने पर बिल्कुल सही काम करता है, वह असली web server पर upload करने के बाद पूरी तरह टूट सकता है, क्योंकि server की folder structure शायद ही कभी local development folder से बिल्कुल मेल खाती है।

उदाहरण: URL vs File Path: What's the Difference

markup
<!-- File path on disk: C:/projects/site/logo.png -->
<!-- Becomes this URL in the browser: -->
<img src="logo.png" alt="Logo">
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.