开发者

link stylesheet from included header in PHP

开发者 https://www.devze.com 2023-01-07 13:49 出处:网络
I\'m currently working on updating a \"legacy\" websit开发者_如何学运维e to xhtml/css, so that I can go ahead and proceed on a re-design. All of the pages have the header included via PHP. The issue i

I'm currently working on updating a "legacy" websit开发者_如何学运维e to xhtml/css, so that I can go ahead and proceed on a re-design. All of the pages have the header included via PHP. The issue is is that if I reference the style sheet from the header as "style.css" it looks in the current directory for the style sheet where of course there is no style sheet. Do I need to use an absolute path, or is there a better way to do this?


The line below should work in any HTML/PHP file in any directory, included/required or not, as long as the directory "assets" is in your home directory. I think i'm right in saying this is true for all "href" attributes (i.e. in anchors).

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

If you're including a CSS file with a PHP inluclude, you must know the relative path from every file in which you are running the include function - no absolute URLs are allowed.


The path to the CSS file is relative to the URL which you used to request the main PHP page (the one in browser address bar), not to the local disk file system path where the PHP page is located in the server machine. CSS files are namely loaded by the webbrowser, not by webserver.

So to figure the relative style sheet path which you'd like to use in <link href> in the HTML head, you need to know the absolute URL of both the PHP page and the CSS file so that you can extract the relative CSS path from it.

0

精彩评论

暂无评论...
验证码 换一张
取 消