如何使用PHP代码而不是HTML代码导入/包含CSS文件?

时间:2021-10-09 08:47:01

I have googled a lot but it seems that I am doing something wrong.

我用谷歌搜索了很多次,但似乎我做错了什么。

I want to do this:

我想这样做:

<?php
include 'header.php';
include'CSS/main.css';
...
?>

However, my page prints the CSS code.

但是,我的页面打印CSS代码。

Note: I want to use PHP to include the CSS file, and not use I also do you want to rename my CSS file to a PHP file as some website mentioned.

注意:我想要使用PHP来包含CSS文件,而不是使用I,你也想像某些网站提到的那样将我的CSS文件重命名为PHP文件。

Any clues?

有线索吗?

Many thanks.

多谢。

13 个解决方案

#1


47  

You have to surround the CSS with a <style> tag:

你必须用

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

PHP include works fine with .css ending too. In this way you can even use PHP in your CSS file. That can be really helpful to organize e.g. colors as variables.

PHP包含与.css结尾也很好。这样,您甚至可以在CSS文件中使用PHP。这对于将颜色作为变量进行组织是很有帮助的。

#2


35  

You are including the CSS code as text in your PHP page. Why not just link it in the traditional fashion?

您将CSS代码作为文本包含在PHP页面中。为什么不把它和传统的时尚联系起来呢?

<link rel="stylesheet" href="CSS/main.css" type="text/css">

#3


8  

you can use:

您可以使用:

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

and assuming that css file doesn't have it already, wrap the above in:

假设css文件还没有,将上面的内容封装在:

<style type="text/css">
...
</style>

#4


5  

To use "include" to include CSS, you have to tell PHP you're using CSS code. Add this to your header of your CSS file and make it main.php (or styles.css, or whatever):

要使用“include”来包含CSS,您必须告诉PHP您正在使用CSS代码。将此添加到您的CSS文件的头部,并使其成为main。php(或风格。css,或者其他):

header("Content-type: text/css; charset: UTF-8");

This might help with some user's connections, but it theoretically (read: I haven't tested it) adds processor overhead to your server and according to Steve Souder, because your computer can download multiple files at once, using include could be slower. If you have your CSS split into a dozen files, maybe it would be faster?

这可能对某些用户的连接有所帮助,但从理论上讲(读:我还没有测试它)会增加服务器的处理器开销,根据Steve Souder的说法,因为您的计算机可以同时下载多个文件,使用include可能会更慢。如果你把你的CSS分成十几份文件,也许会更快?

Steve's blog post: http://www.stevesouders.com/blog/2009/04/09/dont-use-import/ Source: http://css-tricks.com/css-variables-with-php/

Steve的博客:http://www.stevesouders.com/blog/2009/04/09/dont-use-import/来源:http://css- php/

#5


4  

    <?php
    define('CSSPATH', 'template/css/'); //define css path

    $cssItem = 'style.css'; //css item to display

    ?>

<html>
<head>
 <title>Including css</title>
  <link rel="stylesheet" href="<?php echo (CSSPATH . "$cssItem"); ?>" type="text/css">
</head>
<body>
...
...
</body>
</html>

YOUR CSS ITEM IS INCLUDED

您的CSS项目包括在内。

#6


2  

If you want to import a CSS file like that, just give the file itself a .php extension and import it anyway. It will work just fine :)

如果您想导入这样的CSS文件,只需给文件本身一个.php扩展名并导入它即可。它会很好地工作:

#7


2  

You can also do the following:

你也可以这样做:

  1. Create a php file in includes folder, name it bootstrap_css.php for example
  2. 在include文件夹中创建一个php文件,命名为bootstrap_css。php为例
  3. paste the css code files to file created above

    将css代码文件粘贴到上面创建的文件中

     <?php 
      $minCss=' <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">';
    
     $business = '<link href="bootstrap/css/modern-business.css" rel="stylesheet">';
    
      echo $minCss;
      echo $business;
    ?>
    
  4. in the html header, include the css files as follows

    在html头部,包括css文件如下

     <?php include_once 'includes/bootstrap_css.php'; ?>
    

#8


2  

This is an older post, however as the info is still relevant today an additional option may help others.

这是一个旧的贴子,但是由于信息仍然是相关的今天,一个额外的选择可能帮助其他人。

  • Define a constant for the file path per Stefan's answer. The definition can be placed at the top of the PHP page itself, or within an included/required external file such as config.php. (http://php.net/manual/en/function.include.php)

    根据Stefan的答案为文件路径定义一个常量。定义可以放在PHP页面本身的顶部,也可以放在包含/需要的外部文件(如config.php)中。(http://php.net/manual/en/function.include.php)

  • Echo the constant in PHP tags, then add the filename directly after. That's it!
    Works for other linked files such as JavaScript as well.

    在PHP标记中回显常量,然后直接在后面添加文件名。就是这样!也适用于其他链接文件,如JavaScript。

<?php
define('CSS_PATH', 'template/css/'); //define CSS path 
define('JS_PATH', 'template/js/'); //define JavaScript path 
?>
<!-- Doctype should be declared, even in PHP file -->
<!DOCTYPE html> 
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo CSS_PATH; ?>main.css">
<script type="text/javascript" src="<?php echo JS_PATH; ?>main.js"></script>
</head>
<body>
</body>
</html>

#9


1  

You could do this

你可以这样做

<?php include("Includes/styles.inc"); ?>

< ?php的包括(“包括/ styles.inc”);? >

And then in this include file, have a link to the your css file(s).

然后在这个包含文件中,有一个到css文件的链接。

#10


0  

I don't know why you would need this but to do this, you could edit your css file:-

我不知道你为什么需要这个,但是要做这个,你可以编辑你的css文件:-

<style type="text/css">
body{
...;
...;
}
</style>

You have just added here and saved it as main.php. You can continue with main.css but it is better as .php since it does not remain a css file after you do that edit

您刚刚添加了这里,并将其保存为main.php。您可以继续使用main。css但它更像。php,因为它在你编辑之后不再是一个css文件。


Then edit your HTML file like this. NOTE: Make the include statement inside the tag

然后像这样编辑HTML文件。注意:在标记中创建include语句

<html>
<head>
 <title>Sample</title>
 <?php inculde('css/main.css');>
</head>
<body>
...
...
</body>
</html>

#11


0  

I solved a similar problem by enveloping all css instructions in a php echo and then saving it as a php file (ofcourse starting and ending the file with the php tags), and then included the php file. This was a necessity as a redirect followed (header ("somefilename.php")) and no html code is allowed before a redirect.

我解决了一个类似的问题:将所有css指令封装在php echo中,然后将其保存为php文件(当然,从php标记开始到结束),然后包含php文件。这是一个必要的重定向(标题(“somefilename.php”)),在重定向之前不允许使用html代码。

#12


0  

The best way to do it is:

最好的方法是:

Step 1: Rename your main.css to main.php

第一步:重命名你的主。css main.php

Step 2: in your main.php add

第二步:主要的。php添加

<style> ... </style>

Step 3: include it as usual

第三步:像往常一样包含它

<?php include 'main.php'; ?>

That is how i did it, and it works smoothly..

我就是这么做的,而且效果很好。

#13


-2  

_trace its directory, I guess

我想,可以跟踪它的目录。

echo css('lib/datatables_rqs/jquery.dataTables.css');

#1


47  

You have to surround the CSS with a <style> tag:

你必须用

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

PHP include works fine with .css ending too. In this way you can even use PHP in your CSS file. That can be really helpful to organize e.g. colors as variables.

PHP包含与.css结尾也很好。这样,您甚至可以在CSS文件中使用PHP。这对于将颜色作为变量进行组织是很有帮助的。

#2


35  

You are including the CSS code as text in your PHP page. Why not just link it in the traditional fashion?

您将CSS代码作为文本包含在PHP页面中。为什么不把它和传统的时尚联系起来呢?

<link rel="stylesheet" href="CSS/main.css" type="text/css">

#3


8  

you can use:

您可以使用:

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

and assuming that css file doesn't have it already, wrap the above in:

假设css文件还没有,将上面的内容封装在:

<style type="text/css">
...
</style>

#4


5  

To use "include" to include CSS, you have to tell PHP you're using CSS code. Add this to your header of your CSS file and make it main.php (or styles.css, or whatever):

要使用“include”来包含CSS,您必须告诉PHP您正在使用CSS代码。将此添加到您的CSS文件的头部,并使其成为main。php(或风格。css,或者其他):

header("Content-type: text/css; charset: UTF-8");

This might help with some user's connections, but it theoretically (read: I haven't tested it) adds processor overhead to your server and according to Steve Souder, because your computer can download multiple files at once, using include could be slower. If you have your CSS split into a dozen files, maybe it would be faster?

这可能对某些用户的连接有所帮助,但从理论上讲(读:我还没有测试它)会增加服务器的处理器开销,根据Steve Souder的说法,因为您的计算机可以同时下载多个文件,使用include可能会更慢。如果你把你的CSS分成十几份文件,也许会更快?

Steve's blog post: http://www.stevesouders.com/blog/2009/04/09/dont-use-import/ Source: http://css-tricks.com/css-variables-with-php/

Steve的博客:http://www.stevesouders.com/blog/2009/04/09/dont-use-import/来源:http://css- php/

#5


4  

    <?php
    define('CSSPATH', 'template/css/'); //define css path

    $cssItem = 'style.css'; //css item to display

    ?>

<html>
<head>
 <title>Including css</title>
  <link rel="stylesheet" href="<?php echo (CSSPATH . "$cssItem"); ?>" type="text/css">
</head>
<body>
...
...
</body>
</html>

YOUR CSS ITEM IS INCLUDED

您的CSS项目包括在内。

#6


2  

If you want to import a CSS file like that, just give the file itself a .php extension and import it anyway. It will work just fine :)

如果您想导入这样的CSS文件,只需给文件本身一个.php扩展名并导入它即可。它会很好地工作:

#7


2  

You can also do the following:

你也可以这样做:

  1. Create a php file in includes folder, name it bootstrap_css.php for example
  2. 在include文件夹中创建一个php文件,命名为bootstrap_css。php为例
  3. paste the css code files to file created above

    将css代码文件粘贴到上面创建的文件中

     <?php 
      $minCss=' <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">';
    
     $business = '<link href="bootstrap/css/modern-business.css" rel="stylesheet">';
    
      echo $minCss;
      echo $business;
    ?>
    
  4. in the html header, include the css files as follows

    在html头部,包括css文件如下

     <?php include_once 'includes/bootstrap_css.php'; ?>
    

#8


2  

This is an older post, however as the info is still relevant today an additional option may help others.

这是一个旧的贴子,但是由于信息仍然是相关的今天,一个额外的选择可能帮助其他人。

  • Define a constant for the file path per Stefan's answer. The definition can be placed at the top of the PHP page itself, or within an included/required external file such as config.php. (http://php.net/manual/en/function.include.php)

    根据Stefan的答案为文件路径定义一个常量。定义可以放在PHP页面本身的顶部,也可以放在包含/需要的外部文件(如config.php)中。(http://php.net/manual/en/function.include.php)

  • Echo the constant in PHP tags, then add the filename directly after. That's it!
    Works for other linked files such as JavaScript as well.

    在PHP标记中回显常量,然后直接在后面添加文件名。就是这样!也适用于其他链接文件,如JavaScript。

<?php
define('CSS_PATH', 'template/css/'); //define CSS path 
define('JS_PATH', 'template/js/'); //define JavaScript path 
?>
<!-- Doctype should be declared, even in PHP file -->
<!DOCTYPE html> 
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo CSS_PATH; ?>main.css">
<script type="text/javascript" src="<?php echo JS_PATH; ?>main.js"></script>
</head>
<body>
</body>
</html>

#9


1  

You could do this

你可以这样做

<?php include("Includes/styles.inc"); ?>

< ?php的包括(“包括/ styles.inc”);? >

And then in this include file, have a link to the your css file(s).

然后在这个包含文件中,有一个到css文件的链接。

#10


0  

I don't know why you would need this but to do this, you could edit your css file:-

我不知道你为什么需要这个,但是要做这个,你可以编辑你的css文件:-

<style type="text/css">
body{
...;
...;
}
</style>

You have just added here and saved it as main.php. You can continue with main.css but it is better as .php since it does not remain a css file after you do that edit

您刚刚添加了这里,并将其保存为main.php。您可以继续使用main。css但它更像。php,因为它在你编辑之后不再是一个css文件。


Then edit your HTML file like this. NOTE: Make the include statement inside the tag

然后像这样编辑HTML文件。注意:在标记中创建include语句

<html>
<head>
 <title>Sample</title>
 <?php inculde('css/main.css');>
</head>
<body>
...
...
</body>
</html>

#11


0  

I solved a similar problem by enveloping all css instructions in a php echo and then saving it as a php file (ofcourse starting and ending the file with the php tags), and then included the php file. This was a necessity as a redirect followed (header ("somefilename.php")) and no html code is allowed before a redirect.

我解决了一个类似的问题:将所有css指令封装在php echo中,然后将其保存为php文件(当然,从php标记开始到结束),然后包含php文件。这是一个必要的重定向(标题(“somefilename.php”)),在重定向之前不允许使用html代码。

#12


0  

The best way to do it is:

最好的方法是:

Step 1: Rename your main.css to main.php

第一步:重命名你的主。css main.php

Step 2: in your main.php add

第二步:主要的。php添加

<style> ... </style>

Step 3: include it as usual

第三步:像往常一样包含它

<?php include 'main.php'; ?>

That is how i did it, and it works smoothly..

我就是这么做的,而且效果很好。

#13


-2  

_trace its directory, I guess

我想,可以跟踪它的目录。

echo css('lib/datatables_rqs/jquery.dataTables.css');