How to import/include a CSS file using PHP code an

2019-01-07 21:48发布

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.

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.

Any clues?

Many thanks.

14条回答
我只想做你的唯一
2楼-- · 2019-01-07 22:44

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.

查看更多
叼着烟拽天下
3楼-- · 2019-01-07 22:45

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

<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


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

<html>
<head>
 <title>Sample</title>
 <?php inculde('css/main.css');>
</head>
<body>
...
...
</body>
</html>
查看更多
登录 后发表回答