Relative path vs Absolute Path?


#1

Can someone elborate on the difference of a relative path vs an absolute path?


#2

Relative to current/source location vs fully writing it out


#3

In the tutorial for a relative link I notice it’s with only one dot

./index.html

I always thought it was with two dots

…/index.html

Do the number of dots have to do with the number of directories the link it in?


#4

. refers to the current directory and .. refers to the parent directory
Or if you omit the dots entirely, then you’re still referring to the same host, but starting at its root directory
With two leading slashes you’re now referring to a new host, but still using the same protocol (like https)
An absolute path would start with the protocol, then the host, then the path (https://google.com/search?q=kittens)


#5

Cool!

So if I were in a grandchild directory would I need three dots? … Or do the two dots apply?


#6

No, you’d go to the parent of the parent.


#7

Can you give me an example in HTML?


#8

You append a slash and a directory name. Going to the parent is to add /.., add another and that’s the grandparent.
Or perhaps you’d consider the slash the end of the directory name. Kind of blurry there when it comes to the web, typically it’s not backed up by a directory structure, but a program which does… whatever it wants. There might for example not be a parent location.


#9

don’t you mean …/
is your example in reverse? :slight_smile:

Edit: Oh and why do three dots show up whenever I try to post two dots?


#10

There definitely needs to be slashes between directory names, but not necessarily after. It gets worse yet when the locations aren’t directories at all, but programs

For example:
https://discuss.codecademy.com/t/relative-path-vs-absolute-path/265544
https://discuss.codecademy.com/t/relative-path-vs-absolute-path/265544/7
the latter link adds /7, the slash is being treated as a separator, not as a terminator

For other pages you’ll find that you get redirected to an url ending in a slash. Not so in this case.

It’s up to you to decide what makes sense there. Ideally you’d automate link generation so that it is consistent.
They do mean different things, but it doesn’t translate to what’s actually happening, the difference in meaning is sometimes lost


#11

You got me confused. :slight_smile: I counted 6.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.