简体   繁体   English

将PHP代码嵌入CSS文件中

[英]Embedding PHP code in a CSS file

Can we embed php code in a css file. 我们可以将php代码嵌入到CSS文件中吗? I need to add conditions to css properties. 我需要为CSS属性添加条件。

styles.css styles.css

<?php
 if($suserid == 2) 
 { ?>
   .proverb { border:0px solid blue; margin-left:34px; width:250px !important; margin-top:6px; } <?php
 }
 else
 { ?>
   .proverb { border:0px solid blue; margin-left:0px; } <?php
 }
?>

Your request is bad practice. 您的要求是不好的做法。 Do not proceed. 不要继续。

While it is technically possible to do this, you should look into alternatives. 尽管从技术上讲可行的 ,但您应该研究替代方法。 Do not dynamically generate CSS files . 不要动态生成CSS文件 They will be cached by your browsers, and dynamic changes will not be propagated. 它们将由您的浏览器缓存,并且不会传播动态更改。

Instead, make special-case classes that you can add to the HTML. 而是创建可以添加到HTML的特殊情况的类。 Your CSS would become: 您的CSS将变为:

.proverb {
  border: 0px solid blue;
  margin-left: 0px;
}
.proverb.user-2 {
  margin-left: 34px;
  width: 250px !important;
  margin-top: 6px;
}

In your HTML-generating code you can have: 在生成HTML的代码中,您可以:

<div class="proverb <?php if($suserid == 2) echo "user-2"; ?>"></div>

This will add the user-2 class if the user id is 2 , and gives the same result as what you wanted in your example. 如果用户ID为2 ,则将添加user-2类,并提供与示例中所需结果相同的结果。

A file is considered PHP by the handlers your webserver attaches to a file type or file extension. 您的网络服务器附加到文件类型或文件扩展名的处理程序将文件视为PHP。 If a file is considered PHP, the contents will be executed as PHP code. 如果将文件视为PHP,则内容将作为PHP代码执行。 Assuming you're using apache, you can add the php handler to CSS files by adding the following in your VirtualHost file or in your .htaccess file: <Directory "/path/to/your/css/dir"> AddType application/x-httpd-php .css </Directory> 假设您正在使用apache,则可以通过在VirtualHost文件或.htaccess文件中添加以下内容来将php处理程序添加到CSS文件中: <Directory "/path/to/your/css/dir"> AddType application/x-httpd-php .css </Directory>

The only downside is this changes the output mime-type to text/html of all css files in the given directory. 唯一的缺点是这会将给定目录中所有css文件的输出mime-type更改为text / html。 Therefore you have to override the mime-type so browsers know you're sending css in stead of html. 因此,您必须覆盖mime类型,以便浏览器知道您是在发送css而不是html。 Trough PHP you can use: <?php header('Content-type: text/css'); ?> 通过PHP可以使用: <?php header('Content-type: text/css'); ?> <?php header('Content-type: text/css'); ?> on the first line of every css file. <?php header('Content-type: text/css'); ?>在每个CSS文件的第一行。

Keep in mind changing the handler to php for css files does put more stress on your server, since every file has to be parsed before it can be sent to the output buffer. 请记住,将css文件的处理程序更改为php确实给服务器带来了更多压力,因为必须先解析每个文件,然后才能将其发送到输出缓冲区。 Therefore it's easier and better to just add an extra class to your html output and adjust your css accordingly, like @Rizier123 suggests. 因此,将一个额外的类添加到html输出并相应地调整css就像@ Rizier123所建议的那样,会更加容易和更好。

It's possible. 这是可能的。

First you have to tell your web server to serve *.css files through the PHP ISAPI module, then you can embed the code. 首先,您必须告诉您的Web服务器通过PHP ISAPI模块提供*.css文件,然后才能嵌入代码。

But this is not a good practice and I'd advise not to do so. 但这不是一个好习惯,我建议不要这样做。 There are better solutions, like SASS and LESS . 有更好的解决方案,例如SASS和LESS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM