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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6315772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:58:35  来源:igfitidea点击:

How to import/include a CSS file using PHP code and not HTML code?

phphtmlcssimport

提问by Shailen TJ

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 文件,而不是使用我也想将我的 CSS 文件重命名为某些网站提到的 PHP 文件。

Any clues?

有什么线索吗?

Many thanks.

非常感谢。

回答by flori

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

你必须用<style>标签包围 CSS :

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

PHP includeworks fine with .cssending 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 也include可以很好地.css结束。通过这种方式,您甚至可以在 CSS 文件中使用 PHP。这对于将例如颜色组织为变量非常有帮助。

回答by Flash

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">

回答by ldg

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>

回答by dantiston

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(或 style.css,或其他):

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

This mighthelp 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/

史蒂夫的博文:http: //www.stevesouders.com/blog/2009/04/09/dont-use-import/来源:http: //css-tricks.com/css-variables-with-php/

回答by Stefan

<?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 项目

回答by 1keown

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'sanswer. 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)

  • 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.

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

  • 在 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>
<?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>

回答by Vincent Koeman

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 扩展名并导入它。它会工作得很好:)

回答by Mwiza

You can also do the following:

您还可以执行以下操作:

  1. Create a php file in includes folder, name it bootstrap_css.php for example
  2. paste the css code files to file created above

     <?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;
    ?>
    
  3. in the html header, include the css files as follows

     <?php include_once 'includes/bootstrap_css.php'; ?>
    
  1. 在包含文件夹中创建一个 php 文件,例如将其命名为 bootstrap_css.php
  2. 将 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;
    ?>
    
  3. 在 html 标题中,包含如下 css 文件

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

回答by L.Box

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 代码。

回答by Jason Gennaro

You could do this

你可以这样做

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

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

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

然后在这个include文件中,有一个指向你的 css 文件的链接。