Relax with Javascript: Randomize Your CSS Style

Relax with Javascript: Randomize Your CSS Style


Spread it!

  • Share

This is just a small tip for fun. Sometime, we are bored with the only-one style in our website, but today, we will make some changes. We will create a javascript to randomize the CSS every time we refresh the web page. For what? Just make our style always get new.

Try to refresh the demo page to see the effect.

We will use javascript function Math.random() to generate a random number between 0 and number of elements of an array which is an arrangement of stylesheet items.

Create a new javascript file which contains this function:

function rand(){
    return Math.round(Math.random()*(css.length-1));
}

This gets us numbers from 0 to < (css.length-1) (Because the elements start from 0) which is this array below:

var css = new Array(
    '<link rel="stylesheet" type="text/css" href="default.css">',
    '<link rel="stylesheet" type="text/css" href="style2.css">',
    '<link rel="stylesheet" type="text/css" href="style3.css">',
    '<link rel="stylesheet" type="text/css" href="style4.css">'
);

Then write it in HTML

rand = rand();

document.write(css[rand]);

That’s it! Enjoy the random CSS by refresh your webpage. Your random effect will show clearly if you have many elements in array. The more elements, the less repeat.

  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • Share on your Facebook
  • Submit this post to Dzone
  • Submit this post to Designbump
  • Submit this post to TheWebBlend

Author: Lam Nguyen

I'm Lam Nguyen, a 21 year old web developer writing about everything related to web design. I am owner of AEXT.NET and WhoFreelance.com Web Community News. You can catch me on twitter.


13 User Comments

  1. Kawsar Ali 07. Nov, 2009 at 1:55 am #

    Nice . A suggestion. should added a text or something that says refresh the page to see the effect. Might be better for some user

  2. Lam Nguyen 07. Nov, 2009 at 3:19 am #

    Yes, that’s good for some users. Thanks for the suggestion Kawsar!

  3. Org 07. Nov, 2009 at 3:44 pm #

    Nice, clean and easy!..
    I make something like that in my website!..
    http://pinguinox.com.ar ( work in progress website!.. )

    Saludos!..

  4. Michel Morelli 08. Nov, 2009 at 12:20 pm #

    Great tips.
    Tnx.

  5. clippingimages 10. Nov, 2009 at 2:32 pm #

    Awesome tips. Thanks for sharing this nice post.

  6. joyoge bookmark 24. Nov, 2009 at 3:54 pm #

    thanks for tips

Trackbacks/Pingbacks

  1. Tweets that mention Relax with Javascript: Randomize Your CSS Style | AEXT.NET -- Topsy.com - 06. Nov, 2009

    [...] This post was mentioned on Twitter by Shurandy Thode, Web Design News. Web Design News said: Relax with Javascript: Randomize Your CSS Style http://bit.ly/4nA3Y1 [...]

  2. uberVU - social comments - 07. Nov, 2009

    Social comments and analytics for this post…

    This post was mentioned on Twitter by allwebdesign: Relax with Javascript: Randomize Your CSS Style http://bit.ly/4nA3Y1...

  3. Relax with Javascript: Randomize Your CSS Style - 07. Nov, 2009

    [...] Visit Source. [...]

  4. Relax with Javascript: Randomize Your CSS Style | Programming Blog - 07. Nov, 2009

    [...] DIRECT LINK » Go to Source Submit this to Script & StyleShare this on BlinklistShare this on del.icio.usDigg this!Post this on DiigoShare this on RedditBuzz up!Stumble upon something good? Share it on StumbleUponShare this on TechnoratiShare this on MixxPost this to MySpaceSubmit this to DesignFloatShare this on FacebookTweet This!Subscribe to the comments for this post?Share this on LinkedinSeed this on NewsvineShare this on DevmarksAdd this to Google BookmarksAdd this to Mister WongAdd this to IzebyShare this on TipdShare this on PFBuzzShare this on FriendFeedMark this on BlogMarksSubmit this to TwittleyShare this on FwispMoo this on DesignMoo!Share this on BobrDobrAdd this to Yandex.BookmarksAdd this to Memory.ruAdd this to 100 bookmarksAdd this to MyPlaceSubmit this to Hacker NewsSend this page to Print FriendlyBump this on DesignBumpAdd this to NingPost this to IdenticaIf you enjoyed this post, make sure you subscribe to my RSS feed! Tags: JavaScript, link, randomize, style, today, website [...]

  5. 網站製作學習誌 » [Web] 連結分享 - 09. Nov, 2009

    [...] Relax with Javascript: Randomize Your CSS Style [...]

  6. 55+ More Great Community Links for Designers and Developers | Master Design - 12. Nov, 2009

    [...] Relax with Javascript: Randomize Your CSS Style [...]

  7. Relax with Javascript: Randomize Your CSS Style | styl.eti.me - 26. Nov, 2009

    [...] Link to original Post: [...]

Leave a Reply

CommentLuv Enabled