使用php来提供CSS文件

Cha*_*amp 4 css php dynamic-css

这是一个好主意通过PHP提供css文件?

实际上我有一个大的CSS文件,其中包括所有媒体查询,所以我认为只根据传递到php页面的参数提供选定的部分例如在我原来的php页面我会这样做

<link rel="stylesheet" type="text/css" href="css.php?<?= "theam=".$theam ."&screen=".$screen ?>"/>
Run Code Online (Sandbox Code Playgroud)

并在css.php文件中我将检查条件以给出响应,只需要css这是正确的方法吗?

Mih*_*rga 7

是的,只要你添加标题就可以了:

header('Content-type: text/css');
Run Code Online (Sandbox Code Playgroud)

并确保检查$_GET正确的值

另一个建议是通过重写将url更改为更好的url:

RewriteRule ^css-(.*)-(.*)\.css$  /css.php?theam=$1&screen=$2
Run Code Online (Sandbox Code Playgroud)