Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Aaron Loften
Aaron Loften
12,864 Points

In the video, he said to use "../" to go up one directory. Is that better than using "/" to go back to the root?

I have seen this done about a thousand times by people I consider very solid in their development. Is it not better to just use "/" in the pathing to go back to the root directory?

Speaking with the idea of reusable code in mind, I am more worried that there may be a future directory clean up, or maybe a code becomes global code.

Is there a reason why I shouldn't use "/" instead of "../"?

It is just not the same...

Usually the code is placed in a directory, and assets and other files are dependent of that directory. You can use "../" to go back one folder, so to speak to the "root" of your project. In a similar manner you can use "./" to stay at your current directory...

If you use "/" you will finish in the system's root directory, that may not be the place where you wanted to be...

Just one example... If you have your files in "/user/project1/html" and you what to access the file "/user/project1/css/stylesheet.css" from there, you will write "../css/stylesheet.css" in your code.

If you write "/css/stylesheet.css", you are telling the program to look precisely for the file "stylesheet.css" inside the folder "css", directly dependent of the system root ("/css/stylesheet.css"), which is outside your working directory ("/user/project1"), and may even not exist.

Hope it makes sense...

Aaron Loften
Aaron Loften
12,864 Points

Christian Andersson I know its not the same.

My point is, if you had...

eg: root/mypages/old/bam.html

and a css file in... root/css/style.css

If you wanted to move or copy code from one said directory to lets say... root/mypages/new/

The css file and all other files should remain intact when using the "/" pathing, yes?

Im wondering what the advantage would be of using "../" instead?

1 Answer

Ok, I think that I understand your point now...

The advantage of using relative paths ("../") is that you can migrate your code to other server, or change the working directory and it will keep working.

If you use the absolute path ("/"), you should ensure that the structure remains always the same.

So, if you want to access to "/user/css/stylesheet.css" form "/user/html/" you can say "../css/stylesheet.css" or "/user/css/stylesheet.css". It will be the same.

But, if when you finish your project, you upload it to a shared server, and your project happens to exist in a obscure system of paths, you will have to change manually all the absolute paths to reflect the new structure. If you used relative paths, you will not need to do that work. You will just need to copy the "html" and "css" folders in the same directory.

So, in the end, if you use relative paths, your code will be reusable. If you use the absolute one, you may need to make changes.

Of course, if you upload your project to a dedicated server with the same file structures than the development machine, there will be no difference.

I am not sure if I am answering the question, is quite late in Portugal now, and I had a loooong day... Please, excuse me if I am far away from the subject.

Aaron Loften
Aaron Loften
12,864 Points

Very valid point. Thanks. I mainly wanted to see the other side of things. :)