Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

html - How do I link to a file in a different directory without using the whole address?

I'm building a website on my computer's local server, with the plan to upload to my webhost with all the links and relative pathways intact. However, I've run into a problem with how to link to pages and images in directories higher up the hierarchy chain.

For example, let say my website is about food and all my logos and stylesheets are in the main .com directory. Then I add the subdirectory "fruit". Sure, I could keep all my Fruit images in the fruit directory, but how would I link back to my logo and stylesheet, and still have links that work when I upload everything to my webhost? When I would edit websites online, just plugging in the whole address was no problem (caused issues if I ever wanted to change the domain, but it would work), but building a website offline on my computer is causing some organizational issues.

Help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use ../ to go up on level in a directory

 <a href="../your/path.html">Go Link</a>

If you want to go straight to the root directory. you have to start your URL with /

  <a href="/your/path.html">Go Link</a>

PS: Be aware that relative path inside CSS file will consider the .css file as starting point.

  body {
     /* Look for image.jpg in the same directory as css file */
     background-image: url('image.jpg');
  }

  div {
     /* Look for image.jpg one directory up in the hierarchy */
     background-image: url('../image.jpg');
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...