简体   繁体   中英

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 (ie '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).

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

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.

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..

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