php - CSS @import 不适用于 PHP

标签 php html css

我正在使用以下 html/css 尝试导入 PHP 文件以充当样式表。这样我就可以动态地编辑样式表中的样式。我是 @import 的新手,但需要它根据当前设备的显示宽度更改样式表。是否有任何明显的错误,或者我不能使用@import 从 PHP 文件导入源代码?

<!DOCTYPE html>
<html>
    <head>
    <title></title>
    <base href="http://www.shadespeed.com/loyalty_points/" target="_blank" />
    <meta name="viewport" content="width=device.width"><meta charset="UTF-8">
    <style type="text/css">    
        @import url('../stylesheets/stylesheet_160_low_res.php')(max-width: 319px);
        @import url('../stylesheets/stylesheet_320_small_smartphone.php')(min-width: 320px) and (max-width: 479px);
        @import url('../stylesheets/stylesheet_480_smartphone.php')(min-width: 480px) and (max-width: 767px);
        @import url('../stylesheets/stylesheet_768_tablet.php')(min-width: 768px) and (max-width: 1023px);
        @import url('../stylesheets/stylesheet_1024_monitor.php')(min-width: 1024px) and (max-width: 1279px);
        @import url('../stylesheets/stylesheet_1280_large_monitor.php')(min-width: 1280px);
    </style>
</head>
<body>

</body>
</html>

在此之前,我使用了一个 Javascript 脚本来获取显示宽度和高度并将它们存储在 PHP 可以读取的 COOKIES 中。 cookie 仅在它们不存在时才被设置。问题是,一旦设置了 cookie,它们就不会改变。如果页面刷新,我需要它们能够改变,这样布局就可以改变,比如用户正在平板电脑或智能手机上查看网页,并且他们旋转屏幕。

编辑:通过用 PHP 样式表文件的完整 URL 替换 @import URL,我刚刚让它开始工作。

<style type="text/css">

@import url('http://www.shadespeed.com/loyalty_points/stylesheets/stylesheet_160_low_res.php')(max-width: 319px);
@import url('http://www.shadespeed.com/loyalty_points/stylesheets/stylesheet_320_small_smartphone.php')(min-width: 320px) and (max-width: 479px);
@import url('http://www.shadespeed.com/loyalty_points/stylesheets/stylesheet_480_smartphone.php')(min-width: 480px) and (max-width: 767px);
@import url('http://www.shadespeed.com/loyalty_points/stylesheets/stylesheet_768_tablet.php')(min-width: 768px) and (max-width: 1023px);
@import url('http://www.shadespeed.com/loyalty_points/stylesheets/stylesheet_1024_monitor.php')(min-width: 1024px) and (max-width: 1279px);
@import url('http://www.shadespeed.com/loyalty_points/stylesheets/stylesheet_1280_large_monitor.php')(min-width: 1280px);

</style>

我确定我之前试过这个,所以我想将 header 添加到 PHP 文件是关键。非常感谢!

非常感谢,

-丹。

最佳答案

确保您为这些文件发送了正确的内容类型 header 。将此行放在这些 CSS 文件的顶部:

<?php header("Content-type: text/css"); ?>

关于php - CSS @import 不适用于 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20384581/

相关文章:

javascript - div 成一个 js 按钮

php - Shopify订单数组问题

php - mysql多维数据

php - 如何更正存储过程错误#1934? (SQL 服务器/PHP)

html - css animation-name 属性不允许转换属性

css - 无限 CSS3 动画随时间随机停止或出现错误

php - 如何通过PHP将正则表达式数据的完整内容插入wordpress数据库?

javascript - Div 只移动一次

html - 悬停时图片显示文本为 "rollover"

html - 使用 z-index 反转元素的 html 顺序