How to include style sheet as php file

for some reason i want to include css file but want to name this file as css.php
how i can send headers for this file to browser to make it look like a css file.

i tried header(‘Content-Type: text/css’);
but it din’t work.
Please help

Why do you want to do this? Usually the extension shouldnt matter. Can you post your code?


     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <title> User Login</title>
     <link type="text/css" rel=stylesheet" href="<?php /css/css.php" />
     <script language="JavaScript" src="/javascript/javascript.js"></script>
     <body onLoad="onLoad('loginform','username');">
     <div class="test">test</div>
     	<div class="loginform">
 		<form name="loginform" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
 			<input type="text" name="username" value="<?php echo $uName;?>" class="text" /><br />
 			<input type="password" name="password" value="" class="text" /><br />
 			<input type="submit" name="submit" value="Submit" class="button" />
 			<input type="reset" name="reset" value="Clear" class="button" />

     	header('Content-Type: text/css');
     .text{font-size: 20pt;}
     .test{font-size: 30pt;}

it is not working. the same code works if change the extension to .css and take the header line out. i even tried this file with .php and without header file and did not work

The link tag seems rather messy. Try this and remove the PHP code from your CSS file.

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

However I still dont know why you need a CSS file called .php.

now it started working automatically.
the reason for using this approach is different themes. it takes a url parameter and just changes the theme.automatically. no need for different scripts. everything in one script. and i inherited this method from PHPMyAdmin. they use this approach.
thanks for the help.

Okay, I see. So its working now?

I am trying to do the exact same thing and am having the problem of it not working as well

I’m using php in the css because I want to script some things which are easily solved with for loops and xml files

Here is the online implementation

Document html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href='stylesheet.php' rel='stylesheet' type='text/css' />


<div class="center">This text should be somewhat centered on the page</div>

PHP stylesheet:


//scripted content will go here


.center {
	position: absolute;
	top: 50%;
	left: 50%;
	vertical-align: middle;
	text-align: center;
	width: 200px;
	height: 200px;
	margin-left: -100px;

I do believe you cannot use .php as CSS. You must use .css as the extension and register a handler/type for .css to be parsed as PHP.

Apache Example:

AddType application/x-httpd-php .css

Aha! I got it to work!

I added this to my php file:

header('Content-type: text/css');

and now things work! :slight_smile:

yes you cannot use php as a css but you can let thebrowser think it is aa css file with the folloowing header and that’s what i did

header(‘Content-Type: text/css’);

and that’s it. it should work smoothly.

You must use .css as the extension and register a handler/type for .css to be parsed as PHP

That would cause even regular .css files to be sent with a mime-type of text/html, which is not what you want.

I use headers for JavaScript files as well as CSS, images, all kinds of stuff. It works fine.