Archive

Archive for the ‘CSS’ Category

PHP – Dynamic CSS Error – Stylesheet was not loaded because its MIME type…

July 22, 2011 3 comments

When creating a dynamic css stylesheet you need to remember to include the header() to tell the stylesheet that it is of type text/css. If you do not include this then even if you include text/css in the stylesheet link, Firefox and other browsers will still assume the type is of text/html. If this occurs you will see the following from within Firefox’s error console. (Located under Tools > Error Console):

Error: The stylesheet http://localhost/ukshopdesign/inc/default.css.php was not loaded because its MIME type, "text/html", is not "text/css".
Source File: http://localhost/ukshopdesign/shopfront.php
Line: 0

To overcome this error place the following at the start of your dynamic css stylesheet:

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

Further Reading

http://php.net/manual/en/function.header.php

Advertisements

PHP – Dynamic CSS

July 18, 2011 2 comments

To create dynamic CSS with PHP you need to create the CSS within a PHP file.

You then need to remember to do the following:

  • Wrap the entire code in <?php ?> tags
  • Include the header() at the top to define the content-type as text/css
  • Define the variables at the top of the code
  • Include the print <<<_CSS command before the css
  • When calling the variable encase it in ${}
  • End the dynamic CSS with  _CSS;

Examples

<?php
header('Content-type: text/css');
$variable1 = <variable>;
print <<<_CSS
<object> {
<attribute>: ${<variable1>};
}
_CSS;
?>

Real-life example:

<?php
header('Content-type: text/css');
$color1 = red;
print <<<_CSS
body {
color: ${color1};
}
_CSS;
?>