.css - using PHP to make dynamic style sheets
Archive - Originally posted on "The Horse's Mouth" - 2005-11-21 18:56:52 - Graham EllisDid you know that you can use PHP to generate dynamic style sheets? If you look at the accessibility options on our new-look website, you'll find you can change the colour and font size on all the pages, and you can select wide and narrow too. And if you're using an older browser that doesn't fully support styles you'll get an older-look menu than if you have IE6 or similar.
A lot of hard work? Oh yes, but all in one set of pages. Our style sheets are not simply flat files - but rather they're php files that generate the necessary style combination from a single master file, with parameters passed in to set all the various parameters. Here, for example, is the start of the file and part that sets one of the font sizes.
<?php
header("content-type: text/css");
$mencolour = "#ffffcc";
$texcolour = "#000000";
$bfsize = 10;
if ($_REQUEST[fsize] == 4) $bfsize = 8;
if ($_REQUEST[fsize] == 2) $bfsize = 12;
if ($_REQUEST[fsize] == 1) $bfsize = 16;
?>
.tierlinks {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: <?= $bfsize ?>px;
color: #000000;
text-align: left;
text-indent: 2pt;
}
Other issues that will arise if you use this "trick" are cache controls - making sure that the style sheet isn't sent our for every page - and ensuring that the file is parted by the PHP handler even if you choose to give it a .css extension.