【问题标题】:How to use @import inside php stylesheet?如何在php样式表中使用@import?
【发布时间】:2017-03-15 06:20:04
【问题描述】:

出于某种原因,我创建了带有 .php 扩展名的样式表。
在 style.php 上

<?php
/*** set the content type header ***/
/*** Without this header, it wont work ***/
header("Content-type: text/css");

$color = '#ff0000';
$path = 'http://mysite.localhost.com/includes/css/';
?>
//this works
body {
    background-color:<?=$color?>;
}
 //these don't work
@import url("<?=$path?>foundation.css");
@import url("<?=$path?>app.css");

我不明白为什么@import 根本不起作用。

即使是没有变量的普通 url,它也不起作用。

@import url("http://mysite.localhost.com/includes/css/foundation.css");

我以前使用过 .css 文件。但是我无法像这样使用php变量/常量来替换url。

关于 style.css

@import url("<?php $path?>style.css");  

【问题讨论】:

  • 您应该尝试使用 SASS 或 LESS,而不是使用 PHP。
  • @mayersdesign,可以浏览
  • 您是否检查了浏览器开发者控制台中的网络选项卡以查看是否正在请求这些文件?
  • @MagnusEriksson,刚刚检查过,没有请求那些 css 文件

标签: php css import


【解决方案1】:

再试一次

   <?php
    $css = file_get_contents('CSS/style.css');
    echo $css;
    ?>

【讨论】:

  • 这行得通,非常感谢.. 我只是想了解为什么 @import 在这里不起作用?
【解决方案2】:

试试这个:

<style>
    <?php include 'CSS/style.css'; ?>
    </style>

【讨论】:

  • header("Content-type: text/css");已经告诉它一个样式表,因此不需要使用
  • 我很确定 OP 将文件包含为 css-file (<style> 用于 html 文档,而不是外部 css 文件。
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
  • 2013-09-12
相关资源
最近更新 更多