简体   繁体   中英

How to properly link css and images in my website

I have some problem specifying css and image paths in my website. It works well when i'm in the same folder level but quickly breaks up when i'm in another folder. I'm working in localhost and my directory structure is like this:

 ROOT
  main.php
  /subfolder/file2.php
  /templates/template1.php
  /style/style.css 

I have already called style.css in template1.php.

I can call main.php from template1.php (using include) and CSS works fine. The problem is when I call file2.php from template1.php, the CSS is no longer called.

How can I specify the paths so that CSS or images is properly linked no matter at which folder I am in?

Using an absolute path to the css file might be the simplest, if it's possible. Otherwise, with more complex structures, Rasclatt's method is the way to go. I often just assign a path variable at the top of the php page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM