Search code examples
htmlsyntaxtagsrelative-path

Syntax to reference a file which is in the same folder as the file you are currently working on


Q. Using relative referencing, what syntax would you use to reference a file called image.gif which is in the same folder (named pass2) as the file you are currently working on?

A.

<img src ="image.gif">

B.

<img src="/image.gif">

C.

<img src="pass2/image.gif">

D.

<img src="/pass2/image.gif">

I will select option A because I am working on the same folder. So, no need to change the folders from the root. But there is no answers so can't verify this.


Solution

  • if the image is in the same folder , then you can use below

    <img src="image.gif" alt="My test image">
    

    From MDN :

    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.

    To link to a target file in the directory above the invoking HTML file, write two dots. So for example, if index.html was inside a subfolder of test-site and my-image.jpg was inside test-site, you could reference my-image.jpg from index.html using ../my-image.jpg.

    You can combine these as much as you like, for example ../subdirectory/another-subdirectory/my-image.jpg.

    For More