It's Friday - what a day to visit jfoobar!

 

How to set up dynamic css

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.

This would work like this:

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.

The dynamic CSS way

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.

Remember that this is just a concept and you should test and play with it on a local install first. When working with Post or Get data in Joomla! make sure you use the Joomla! framework API to check and validate this data!

About the author Arno Zijlstra

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 Zijlstra

Like it? Share it!

There are 7 comments posted.

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

# 2 - Posted by: Arno Zijlstra on 2008-12-04 21:37:23

Yes, 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.

Re: How to set up dynamic css

# 3 - Posted by: Ian Maclennan on 2008-12-04 21:51:43

It is possible, but I would be surprised if your CSS was cached because they would have different URLs.

# 4 - Posted by: Arno Zijlstra on 2008-12-04 21:58:14

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.

# 5 - Posted by: JoomlaWorks on 2008-12-04 23:35:17

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... ;)

# 6 - Posted by: Arno Zijlstra on 2008-12-04 23:50:12

Of course, there are many many ways to achieve things and that is what makes web-development so interesting :-)

Re: best

# 7 - Posted by: sandeep on 2009-04-30 11:24:41

Thanks guys that was extremely helpful!

sandeep verma

(http://sandeepverma.wordpress.com)

Help for creating beautiful comments.

Enter Your Details:
Enter Your Comments:
I'm finished with the form Your form will be checked and you'll get a preview.
moovur promo

Blogging team

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.

Post translations

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.

JFoobar friends on Twitter

Follow JFoobar on twitter

Sponsored Links

Latest Comments

Aaron wrote:
2009-12-23 13:19:22 - Genius! Thanks, Wilco. I've been dying to take .
Posted in How to downlo .
Amy Stephen wrote:
2009-12-22 18:39:37 - Happy Birthday to one of Joomla!'s most noble - .
Posted in Mister Joomla .
Antonie de Wilde wrote:
2009-12-22 09:30:26 - Congrats Robin. Have a good day and watch out w .
Posted in Mister Joomla .
Robert wrote:
2009-12-22 08:51:02 - Happy Birthday Robin .
Posted in Mister Joomla .
Arno wrote:
2009-12-22 08:43:28 - Happy Birthday Robin, love your suit, you wife .
Posted in Mister Joomla .
Brian Teeman wrote:
2009-12-22 00:17:41 - Happy Birthday Robin, Welcome to the big four oh .
Posted in Mister Joomla .