CSS样式表中的全局变量

发布于 2024-08-12 14:41:47 字数 416 浏览 9 评论 0原文

我的样式表中有一个绝对网址。 http://localhost/images/myimage.jpg

具有以下常量

const SITE_ROOT = 'http://localhost/images'

我还有一个配置类,在我编写的 php 页面上 config::SITE_ROOT 。 “/myimage.jpg” 因此,当我将站点移动到生产服务器时,我可以在一个中心位置更改站点根目录。

有没有办法在我的样式表中执行此操作而不将其放入页面中? 相对 url 不是一个选项,因为我正在使用 mod 重写,而相对 url 不起作用。

I hava an absolute url in my stylesheet. http://localhost/images/myimage.jpg.

I also have a config class with the following constant

const SITE_ROOT = 'http://localhost/images'

On php pages i write
config::SITE_ROOT . "/myimage.jpg" so when i move the site to the production server i can change the site root in one central location.

Is there any way to do this in my style sheet without putting it in the page?
Relative url's are not an option because i am using mod rewrites and relative url's don't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅沫记忆 2024-08-19 14:41:47

您可以通过使用 PHP 解析 CSS 文件来将 SITE_ROOT 常量写入 CSS 文件中。将其添加到 .htaccess

AddHandler application/x-httpd-php .css

然后将 PHP 嵌入到 CSS 文件中,就像在任何 .php 文件中一样。

You can write your SITE_ROOT constant into the CSS files by parsing them with PHP. Add this to .htaccess:

AddHandler application/x-httpd-php .css

Then embed PHP into the CSS files as you would do in any .php file.

╰ゝ天使的微笑 2024-08-19 14:41:47

CSS 无法做到这一点。如果你想做这样的事情,你可以将其制作为 PHP 文件并以这种方式使用变量。

但是不能只使用绝对 URL 吗?

background-image: url(/images/myimage.jpg);

将在任何服务器上工作,不是相对的,并且将从根目录查找文件。

There is no way to do this with CSS. If you want to do something like this you could make it a PHP file and use variables that way.

But couldn't you just use absolute URLs?

background-image: url(/images/myimage.jpg);

Will work on any server, is not relative, and will find the files from the root.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文