Written by Arno Zijlstra Thursday, 04 December 2008 00:00
Today I heard Amy Stephen talk about how cool it would be to be able to use dynamic or conditional CSS. It happens I have done this before so I thought I'd share with anyone and her.
With so called dynamic CSS you will be able to do things in a "CSS" file that you can't normally do. Suppose you want a section of your site to have a green color pallet while the rest of the site has a blue color pallet. One option would be to create a green.css and a blue.css file and load this file in your template based on for example the $option variable.
Put this code between the head and end head tags beneath the other CSS file calls in your_template/index.php and make sure the files exist in the CSS directory.
<?php
$option = JRequest::getCmd('option');
if($option == 'com_contact')
{
?>
<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/<?php echo $this->template; ?>/css/green.css" type="text/css" media="screen,projection" />
<?php
}
else
{
?>
<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/<?php echo $this->template; ?>/css/blue.css" type="text/css" media="screen,projection" />
<?php
}
?>
So what happens here is that when your contact component is triggered by a visitor the little switch above will use the green CSS file and else it will use the blue CSS file.
This solution is using two CSS files to change a color pallet while it could be done with one CSS file.
For the dynamic css we are going to serve the browser a PHP file while we will let the browser think it is a CSS file.
Create a file, for example dynamic.php in your_template/css directory. In the file you add the following code to serve the PHP file as CSS to the browser.
<?php
header('Content-type: text/css');
?>
Now we have a CSS file where you can do some PHP trickery for changing the same color pallet as in the first example. Lets add some code.
<?php
header('Content-type: text/css');
$var = $_GET['option'];
if($var == 'com_contact')
{
$color = '#99CC33'; // Green
}
else
{
$color = '#94B8F9'; // Blue
}
print <<<_CSS
#content-block {
color: ${color};
}
.module-heading {
background: ${color};
}
_CSS;
?>
We are getting the $option to switch color from $_GET['option'] but where do we get it?
We will have to add the dynamic CSS file to the template head just like we did in the first example of this post. One difference is that we will call the dynamic.php file and passing the $option variable with it.
<?php
$option = JRequest::getCmd('option');
?>
<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/<?php echo $this->template; ?>/css/dynamic.php?option=<?php echo $option; ?>" type="text/css" media="screen,projection" />
Look at the URL for the CSS.
/css/dynamic.php?option=<?php echo $option; ?>
We are passing the $option to the "CSS" file.
This example is pretty simple but you can probably imagine some very cool things using this dynamic stylesheet trick.
Arno Zijlstra is the creative mind and founder of alvaana.com development lab for the web. He is one of the original founders of the Joomla! Open Source Content Management System and has been involved in a couple of other Open Source projects. His passion is creating sites and interfaces that look different and are built with accessibility, usability and web-standards being truly important. Always on the look out for new things and open to learn from anyone, anytime.
Follow me at http://twitter.com/me_arno
More about Arno ZijlstraYes, thanks Thomas for the addition.
It is indeed something you should test firmly before using it in a live environment. I used it to load a large site header css background image based on the article id so the sites overall look was changed to fit the article and that works nice.
It is possible, but I would be surprised if your CSS was cached because they would have different URLs.
It was just a three line css file to override the css for one div and only when someone was reading a full article so not to many problems there. I wouldn't do it with complete site css files though.
It has been our common practice since the Mambo days to do something less compicated, yet powerful enough, which lies to what CSS can already do...
If you "echo" the component as an id and category, section, id or even itemid as class in the tag, you have a fine way of achieving in one CSS file different layouts.
That means, if you want the component title to be red in, say all articles, you do:
body#com_content .componentheading {color:red;}
But if you want the title in the newsfeeds component to be blue, you siply do:
body#com_newsfeeds .componentheading {color:blue;}
And all this in one CSS file... ;)
Of course, there are many many ways to achieve things and that is what makes web-development so interesting :-)
Thanks guys that was extremely helpful!
sandeep verma
(http://sandeepverma.wordpress.com)
We have a team that works on the blogs presented on this site. Below you will find all present members who are actively working on blogs on this site.
Please contact us if you are interested in helping us out with the creation of the blogs.
jfoobar has readers from all over the world and in many languages. If you create a translation of one of our posts and link to it than please let us know so we can add a link back to the translation at the original post.
Copyright © 2008 jfoobar - All Rights Reserved - Joomla! is a registered trademark of Open Source Matters, Inc - Disclaimer
Re: Caching
# 1 - Posted by: Thomas on 2008-12-04 21:30:35
Hi,
as far as i can see, most "dynamic CSS" ideas have the problem with the caching in the browser. Adding "Last-Modified" or "Expires" to the header doesn't always work. We have tried a lot of different things that didn't really work in all browsers.
We had the best results by adding the css-filetype to php-parser, so we could use php in the CSS. But again, the browser didn't always reload the files...
Thomas