Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly reference local resources in HTML?

Tags:

html

ref

src

As it turns out, referencing local resources can be a rub point for some. I'm looking for a canonical answer to local resource referencing, and what they mean.

Take these examples, what is the difference between these reference paths?

  • <img src="myfile.png" /> (no leading slash)
  • <img src="/myfile.png" /> (with leading slash)
  • <img src="folder/myfile.png" /> (no leading slash / in subfolder)
  • <img src="/folder/myfile.png" /> (with leading slash / in sub folder)
  • <img src="../folder/myfile.png" /> (with dots and a leading slash / in sub folder)
like image 640
Chase Florell Avatar asked Jan 23 '13 20:01

Chase Florell


People also ask

How do you reference local files in html?

To link to a target file in the same directory as the invoking HTML file, just use the filename, e.g. my-image. jpg . To reference a file in a subdirectory, write the directory name in front of the path, plus a forward slash, e.g. subdirectory/my-image. jpg .

How do I create a local path in html?

We can link any external resource to add in our HTML file with the help of file paths such as images, file, CSS file, JS file, video, etc. The src or href attribute requires an attribute to link any external source to HTML file. Following are the different types to specify file paths: <img src="picture.

How do I open a local file in html?

HTML can be used to open a folder from our local storage. In order to open a folder from our local storage, use 'HREF' attribute of HTML. In the HREF attribute, we specify the path of our folder.


1 Answers

  • A leading slash tells the browser to start at the root directory.
  • If you don't have the leading slash, you're referencing from the current directory.
  • If you add two dots before the leading slash, it means you're referencing the parent of the current directory.

Take the following folder structure

demo folder structure

notice:

  • the ROOT checkmark is green,
  • the second checkmark is orange,
  • the third checkmark is purple,
  • the fourth checkmark is yellow

Now in the index.html.en file you'll want to put the following markup

<p>
    <span>src="check_mark.png"</span>
    <img src="check_mark.png" />
    <span>I'm purple because I'm referenced from this current directory</span>
</p>

<p>
    <span>src="/check_mark.png"</span>
    <img src="/check_mark.png" />
    <span>I'm green because I'm referenced from the ROOT directory</span>
</p>

<p>
    <span>src="subfolder/check_mark.png"</span>
    <img src="subfolder/check_mark.png" />
    <span>I'm yellow because I'm referenced from the child of this current directory</span>
</p>

<p>
    <span>src="/subfolder/check_mark.png"</span>
    <img src="/subfolder/check_mark.png" />
    <span>I'm orange because I'm referenced from the child of the ROOT directory</span>
</p>

<p>
    <span>src="../subfolder/check_mark.png"</span>
    <img src="../subfolder/check_mark.png" />
    <span>I'm purple because I'm referenced from the parent of this current directory</span>
</p>

<p>
    <span>src="subfolder/subfolder/check_mark.png"</span>
    <img src="subfolder/subfolder/check_mark.png" />
    <span>I'm [broken] because there is no subfolder two children down from this current directory</span>
</p>

<p>
    <span>src="/subfolder/subfolder/check_mark.png"</span>
    <img src="/subfolder/subfolder/check_mark.png" />
    <span>I'm purple because I'm referenced two children down from the ROOT directory</span>
</p>

Now if you load up the index.html.en file located in the second subfolder
http://example.com/subfolder/subfolder/

This will be your output

enter image description here

like image 81
Chase Florell Avatar answered Oct 19 '22 14:10

Chase Florell