[SOLVED] How to indicate root directory in html while linking external css, js and images? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[SOLVED] How to indicate root directory in html while linking external css, js and images?

I have my pages in the following order: -index.html -index.js -logo.jpg |__discussion |_discuss.html |_comment.html Like this similar structure. But all the external css files required by the pages are in the root directory. So, my question is how can I define root directory while linking theese files in my html pages. The one way I know is to use this syntax: ../../script.js But this causes problem when I need to move the the pages to different sub-folders. So, I need the master way to define the resources directly in the root directory (where the index.html page is located ).

8th Oct 2020, 2:02 AM
Bibek Oli
Bibek Oli - avatar
4 Answers
+ 4
Typically, you would do any of the following: 1. For pages served from a web server, start from the root path: /css/styles.css ... regardless of where the referring file is located. This will make it super easy to do: npm install - g http-server 2. For SPA, a single index.html file will refer to a bundled.css file. The pages will be loaded using a client side router. But, you might not be there yet. 3. For server side MVC apps, most will refer to a root path using some convention like ~/css/styles.css. 4. Otherwise, you might need to go old school with maintaining the path references relative to each respective file. yuck
8th Oct 2020, 7:11 AM
David Carroll
David Carroll - avatar
+ 3
Automate the boring stuff But seriously, the first alternative shared by David Carroll sounds saner
8th Oct 2020, 6:42 AM
Kevin ★
+ 2
Ipang Yes, but it would be difficult to manage. If I need to update the them, I need to update all of them, which is a bit booring.
8th Oct 2020, 6:37 AM
Bibek Oli
Bibek Oli - avatar
+ 1
8th Oct 2020, 6:47 AM
Bibek Oli
Bibek Oli - avatar