Forested
Forested

Reputation: 353

php include with html css

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?

Upvotes: 1

Views: 2414

Answers (6)

gtklocker
gtklocker

Reputation: 101

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

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

Upvotes: 0

CaNNaDaRk
CaNNaDaRk

Reputation: 1322

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

Upvotes: 2

xdazz
xdazz

Reputation: 160943

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

Upvotes: 3

DaveRandom
DaveRandom

Reputation: 88697

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" />

Upvotes: 1

Brian Glaz
Brian Glaz

Reputation: 15696

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

Upvotes: 0

RiaD
RiaD

Reputation: 47658

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

It means styles/main.css from root of website

Upvotes: 2

Related Questions