使用PHP创建一个外部动态生成的CSS文件。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PHP创建一个外部动态生成的CSS文件。相关的知识,希望对你有一定的参考价值。
One other thing to note, is that the php generated css file can make use of query parameters, for instance:<link rel="stylesheet" href="base.css.php?type=alternate" type="text/css" >
in your PHP file you can use the variable:
if ($_GET['type'] == 'alternate'){
// set some options etc. ....
}
-- 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 // 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创建一个外部动态生成的CSS文件。的主要内容,如果未能解决你的问题,请参考以下文章