1

I have a php file that i include in other php files. This php file is the menu. It links to my stylesheet with <link href="styles/main.css" rel="stylesheet/index" type="text/css" />. But this only works if the php file that includes it is correct according to the path. How can I use absolute paths?

Say if I include the menu.php from another folder how can you automatically update the path to the css file?

6 Answers 6

3

Just use the absolute path, href="/style/main.css"

Sign up to request clarification or add additional context in comments.

Comments

2
<link href="/styles/main.css" rel="stylesheet/index" type="text/css" />

It means styles/main.css from root of website

Comments

2

An absolute URI is like this:

href="http://example.com/absolute/path/to/file.css"

an URI relative to the current directory is like this:

href="relative/url/to/file.css"

an URI relative to your site's root (http://example.com/) starts with a /:

href="/relative/path/from/yoursite/to/file.css"

Inside your css file all URLS are relative to it's own position so it's behaviour doesn't change when the path of the file which includes it changes.
In example if your menu.css file is located into

http://example.com/styles/menu.css

just use

<link href="/styles/menu.css" rel="stylesheet/index" type="text/css" />

And browsers will always look for menu.css in http://yoursite.com/styles/menu.css

3 Comments

Those are both relative URIs. An absolute URI will stand alone (i.e. it must start with a scheme).
That's right, I only wanted to emphasize but used wrong terms. Edited and added a couple of examples.
@Quentin that's quite absolute path
1

Use the full path relative to your document root instead of the relative path. E.g.

<link href="/styles/main.css" rel="stylesheet/index" type="text/css" />

Comments

0

as you suggested, just you absolute paths. So when you link to your stylesheet, just use the the full path like http://www.mysite.com/css/myCssFile.css

1 Comment

That's an absolute URI, not just an absolute path.
0

By the way always make sure you put a base tag on your HTML, like that:

<base href="http://www.mysite.com/" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.