• More shocking case studies – how to game Klout

    Last week I posted an article highlighting how you could game Klout. In my post and the subsequent commentary I argued that: Klout’s model of scoring seems to be based predominantly on levels of network activity which is a rather primitive appr…

    Posted in Google Read More
  • Klout: Here is how you can game Klout

    Dear SEO consultants, If you are taking a healthy dose of digital marketing every fortnight or so then you have probably heard of the new boys in town, yes the infamous Klout! Klout has received a lot of good PR, if I can recall correctly they have…

    Posted in Google Read More
  • Query deserves freshness (QDF) in practice

    Search engine & informational retrieval technologies have come a long way yet they are still in their infancy, particularly when it comes to sentiment and intent analysis. Very often users with different search intents may submit the same search quer…

    Posted in Google Read More

Random Styling with PHP and CSS

Posted on November 7, 2011 by under Tips And Tutorials

While recently designing a website I was required to build a function that would change the sites colour palette each time the visitor goes to a new page. Essentially this means each time a page is loaded I need some random element that I can assign a collection of pre-determined palette styles. The easiest way I could think of, would be to create a PHP function that would randomly return one of a set number of strings that I could then place as class on the body tag. Then in the stylesheet the background colours, logos and images can be set to each of the various classes.

This sounds more completed than it really is. PHP has a nice little function called ‘rand’. The first argument allows you to set a min point and the second argument sets the max point. The function then returns a pseudo-random integer between min and max.

This is how I used the function:

<?php
function random($max){
$random = rand(1, $max);
$bgClass = "randomClass".$random;
return $bgClass;
}
?>

Just call this function in the class attribute of the body tag, replacing max with a number.


<body class="<?php echo random(5); ?>">

Then all that needs to be done is, in the stylesheet , set rules for each of the possible classes.

.randomClass1 div{background:black;}
.randomClass2 div{background:green;}
.randomClass3 div{background:red;}
.randomClass4 div{background:pink;}
.randomClass5 div{background:blue;}

Take a look at this extremely ugly demo to see the function in action. Remember to refresh the page a few times.

A little about Berty …

Berty is the latest member of a growing team at Elevate Local. He's passionate about Art & Design and has a strong background in web-design and photography. When he's not in front of computer screens or looking through a viewfinder he can often be found scaling rock-faces or doing other wholesome outdoor activities.
This post was tagged under: , , ,
No comments have been made on this post.

Want to Contribute? Leave a Comment…

Your email address will not be made public or shared. Inappropriate and irrelevant comments will be removed.