-1

i have started to add include files like header, nav, footer.php to my index.php in my root directory, i got a admin folder with another index.php, with the same include files except it is using "../" in its target path to go back one before accessing the includes file, what happens is it works except for the css files... i lose my styling, but on my root index.php if i go back to that, the styling is working.

Any idea why this is happening?

my css code is:

<link rel="stylesheet" href="css/main.css">
Dash
  • 314
  • 2
  • 14
thechrishaddad
  • 6,523
  • 7
  • 27
  • 33
  • Can you provide some more code? – Mir Aug 01 '13 at 11:56
  • 1
    Read [Absolute urls, relative urls, and…?](http://stackoverflow.com/questions/904046/absolute-urls-relative-urls-and) and also [Using relative URL in CSS file, what location is it relative to?](http://stackoverflow.com/questions/940451/using-relative-url-in-css-file-what-location-is-it-relative-to) – Luke Shaheen Aug 01 '13 at 11:59

4 Answers4

5

Use an absolute path in your link tag

<link rel="stylesheet" href="/css/main.css" />

As kindly pointed out by John, this is not an absolute path and in all honesty I have no idea what to call it (root relative?). What I do know is that it is relative to the root of the site and not the current folder on the server.

Dale
  • 10,384
  • 21
  • 34
  • This is still considered a "relative" path. I recommend this solution too, but just be aware it's not absolute, it's still a type of a relative URL. – Luke Shaheen Aug 01 '13 at 12:00
  • Not sure if that's true or not, will research! – Dale Aug 01 '13 at 12:01
  • http://tools.ietf.org/html/rfc3986#section-4.2 from ["Absolute urls, relative urls, and…?"](http://stackoverflow.com/questions/904046/absolute-urls-relative-urls-and) – Luke Shaheen Aug 01 '13 at 12:03
  • 1
    well you learn something new everyday, thanks john. SO should I refer to this as an absolute relative? heh – Dale Aug 01 '13 at 12:04
3

The syntax you're using is relative path.

Use either this to fix the relative path:

<link rel="stylesheet" href="../css/main.css">

Or make it absolute if your css directory is in the web docroot:

<link rel="stylesheet" href="/css/main.css">
Károly Nagy
  • 1,734
  • 10
  • 13
  • This is still considered a "relative" path. I recommend this solution too, but just be aware it's not absolute, it's still a type of a relative URL. – Luke Shaheen Aug 01 '13 at 12:01
  • The thing is, if i am using the relative one, it breaks my root index.php styling now, so you dont win? it will go live eventually so what would be my best option to do so i don't have to fix it all up later when it gets more in depth? – thechrishaddad Aug 01 '13 at 12:02
  • @user1265535 If `/css/main.css` doesn't work,then `http://www.example.com/css/main.css` won't work either. Did you try `/css/main.css` - no dots at the beginning, just the leading slash? The leading slash makes your link start at the home directory. – Luke Shaheen Aug 01 '13 at 12:05
  • The best is to use absolute path. Relative paths are only causing problems later on. (To be more specific as John pointed out its absolute in the url but relative to the docroot on you server. However css is being pulled by the browser based on the url so technically this is an absolute path.) – Károly Nagy Aug 01 '13 at 12:07
2

if css folder is outside of your admin folder it should be ../css/main.css, right?

Vipin Kumar KM
  • 356
  • 5
  • 17
0

For a better management, I'll use an abolute path with full url

<link rel="stylesheet" href="{$html_css}/main.css" />
Anonymous
  • 1,405
  • 4
  • 17
  • 26