T. Hinton
T. Hinton

Reputation: 3

Including external stylesheets in a PHP file

<link href="/app/app.css" rel="stylesheet" type="text/css">

The above is the code I'm using to access a stylesheet, but it's not working at the .

Some extra details: This is in a .php file, but it's located within the head of an html section I'm working on a temporary url (i.e. 'my.ipa.dd.res/mydomain.com/dir/'). This might be the reason it's not working.

Edit: It's a stylesheet I'd like to use on several pages, which is why I'm trying to point to a root directory (so that I don't need the file in every single folder I create).

Upvotes: 0

Views: 72

Answers (3)

CFl
CFl

Reputation: 48

I've been having this same problem recently. Here's what worked for me

Now on the index page, replace link rel="/app/app.css" href="stylesheet" type="text/css"

with

?php include 'style.php' ?

And on the style.php page insert the following tags:

style and /style

You can put the regular CSS between those two tags. See if that works. BTW for some reason I can't insert the greater or less than symbols without making the code disappear... Forgive me, I'm new here..

Upvotes: 0

Tristan
Tristan

Reputation: 3321

If you remove the leading slash it will look for the css file in the folder relative to the current.

<link href="app/app.css" rel="stylesheet" type="text/css">

Edit: to reuse in multiple scripts in different dirs you would need to specify an absolute path, so to avoid having to change it in multiple places when you go live (ie stop using the temporary url) you should define a variable.

<?php
// set absolute path to the stylesheet
$cssPath = 'http://my.ipa.dd.res/mydomain.com/dir/';
?>

And

<link href="<?php echo $cssPath; ?>app/app.css" rel="stylesheet" type="text/css">

Depending on your php architecture you may need to define $cssPath as a global or you may be able to add it to an existing config variable. This is completely dependent on your framework.

Upvotes: 0

J&#233;r&#233;my Halin
J&#233;r&#233;my Halin

Reputation: 563

Well I think you need to store your root directory path as a string to include your css file with an absolute URL. Something like :

<link href="{$absoluteRootPath}/css/styles.css" rel="stylesheet" type="text/css">

Upvotes: 1

Related Questions