PHP 使用PHP创建外部动态生成的CSS文件。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 使用PHP创建外部动态生成的CSS文件。相关的知识,希望对你有一定的参考价值。

-- This is a very easy way to create a dynamically generated CSS file in PHP
-- 
-- The important parts of the code below:
-- 	1. note that the PHP generated CSS file must start out with 
--     header('Content-type: text/css');
--
--  2. The file name should end in .php not .css , since this is a PHP file.
--
-- Why this works: when the CSS file is requested by the remote server, the 
-- server sees the .php extension and preprocesses it before sending it to
-- the client browser. Since the PHP file sets the Content-type as text/css
-- the client browser treats it like a CSS file.
-- 
-- Code written by dbug13(Jamie Allison)

----- Begin Dynamic CSS file: base.css.php -----
<?php
header('Content-type: text/css');

// Try changing the variables below, and refresh your html file.
$bg_color = "purple";
$base_font="Verdana,Arial,sans";
?>
body{
	font-family: <?php echo $base_font; ?>;
	background-color: <?php echo $bg_color; ?>;
	
}
?>
----- End Dynamic CSS file

----- Begin HTML file that uses above CSS file: index.html -----
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<!-- Include the dynamic CSS file -->
	<link rel="stylesheet" href="base.css.php" type="text/css" media="screen" title="no title" charset="utf-8">
	<title>untitled</title>
	
</head>

<body>

<h1>Hello World</h1>

</body>
</html>
----- End HTML file -----

以上是关于PHP 使用PHP创建外部动态生成的CSS文件。的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP 从外部公共文件夹提供 css 图像

php简介

使用 JavaScript 或 PHP 动态生成 CSS 网格

PHP全栈开发:CSS Ⅱ 创建

PHP代码生成

使用 PHP 动态创建站点地图 [重复]