Tutorials

Latest Word » Tutorials » Tick Tock – A CSS & JS Experiment
Tick Tock – A CSS & JS Experiment

Tick Tock – A CSS & JS Experiment

Recently while trying to come up with clever ways to show the calendar date, I came up with an idea of creating a clock with CSS.  At first I figured this is probably not as usable as I hoped (and possibly pointless until some of its major flaws are fixed), but I decided to take a stab at it just for my own amusement. 

This clock was created using a combination of CSS and Javascript. The Javascript gets the user’s local time (you could use PHP, but that would be server time) and updates the CSS <div> tags with the appropriate hour and minute classes.

The Breakdown

Basically this clock is broken down into 3 layers. Using transparent PNG files, I created the hour/min/sec arms and layered them on top of each other using absolute positioning. The original Clock image was created by Cemagraphics, they did a beautiful job creating this icon.

HTML

<div id="clockbase">
	    <div id="hours"></div>
	    <div id="minutes"></div>
	    <div id="seconds"></div>
</div>

CSS
Using the CSS Sprites technique, I layed out each of the arms in one image and shifted them according to the time frame using the background positioning.

body {
	margin: 0;
	padding: 0;
	background: #3e5e84 url(bg_stretch.gif) repeat-x;
}
* {
	margin: 0;
	padding: 0;
}
#clockbase {
	width: 512px;
	height: 512px;
	position: relative;
	margin: 0 auto;
	background: url(clock_bg.jpg) no-repeat;
}
#minutes {
	width: 229px;
	height: 229px;
	position: absolute;
	top: 200px;
	left: 137px;
	background: url(minutes-arm.png) no-repeat;
}
#hours {
	width: 200px;
	height: 200px;
	position: absolute;
	top: 220px;
	left: 150px;
	background: url(hours-arm.png) no-repeat left bottom;
}
#seconds {
	width: 260px;
	height: 260px;
	position: absolute;
	top: 184px;
	left: 120px;
	background: url(SECS.gif) no-repeat;

}
#clockbase .min05 {background-position: left top;}
#clockbase .min10 {background-position: left -229px;}
#clockbase .min15 {background-position: left -458px;}
#clockbase .min20 {background-position: left -687px;}
#clockbase .min25 {background-position: left -916px;}
#clockbase .min30 {background-position: left -1145px;}
#clockbase .min35 {background-position: left -1374px;}
#clockbase .min40 {background-position: left -1603px;}
#clockbase .min45 {background-position: left -1832px;}
#clockbase .min50 {background-position: left -2061px;}
#clockbase .min55 {background-position: left -2290px;}
#clockbase .min00 {background-position: left -2519px;}

#clockbase .hr1 {background-position: left top;}
#clockbase .hr2 {background-position: left -200px;}
#clockbase .hr3 {background-position: left -400px;}
#clockbase .hr4 {background-position: left -600px;}
#clockbase .hr5 {background-position: left -800px;}
#clockbase .hr6 {background-position: left -1000px;}
#clockbase .hr7 {background-position: left -1200px;}
#clockbase .hr8 {background-position: left -1400px;}
#clockbase .hr9 {background-position: left -1600px;}
#clockbase .hr10 {background-position: left -1800px;}
#clockbase .hr11 {background-position: left -2000px;}
#clockbase .hr12 {background-position: left -2200px;}

/*--Replaced the PNG with a GIF for IE6--*/
*html #minutes {
	background: url(minutes-arm.gif) no-repeat;
}
*html #hours {
	background: url(hours-arm.gif) no-repeat left bottom;
}

Conclusion

As you may have noticed, the clock moves in increments of five minutes. I was hesitant to make 60 segments of the arm since this was just an experiment. If you have any questions, comments, or input, I would love to hear your thoughts on this~

*Update – Alternative Using CSS3

Toby Pitman just notified me that he had also created a CSS Clock just recently, with a completely different approach. Do check out his version as well! He utilizes the “rotate” feature in CSS3 (Supported in Safari) and uses jQuery to get the local time. Check out his post on CSS-Trick’s Forum, and his demo can be found here.

Popular Posts

Author Bio

My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!

You can learn more about me or Twitter  Follow me on twitter for more updates and resources!

Did You Enjoy This Post?

subscribe  Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.

+ Add Comment25 Peeps Have Spoken Their Minds...

  1. Pedro Rogério

    Wowwwwwww, god job.

  2. Scot

    Nice! I’ve made similar clocks in flash before, but this CSS / JavaScript one looks way better. You rock! :-).

  3. Megges

    This ist just awesome. Great Work!

  4. Tim Hart

    Looking at this CSS & JS usage do you then think it would be possible to write a script that would allow real-time live Doppler radar instead if the update every 15 min. crap? Looks like it would have to pull constantly changing images from the live source. If someone undersands or knows how lets talk.

  5. Soh

    Thanks for all the comments~

    Tim, your absolutely correct and I’m with you on that one. For this clock to be usable that is what would be needed, which is why this was just an experiment for my own amusement.

    After I gave it my first initial thought, I was going to pass on the idea due to its flaws, but I gave it a shot for fun. :-)

    “At first I figured this is probably not as usable I hoped, but I decided to take a stab at it just for my own amusement.”

    I appreciate the feedback, thanks!

    *update – For a “real-time live Doppler radar” version, check out Toby Pitman’s version of the CSS Clock~

  6. Marco

    Just amazing what you just show can be achieved with CSS and Javascript. Not only the design is beautiful, but the code is neat and simple. WAY TO GO!

  7. Thomas Milburn

    I think using the canvas tag could solve the rotation problem. Can’t you load a png image into a canvas and rotate it?

  8. Soh

    Hey Thomas~

    I just looked up the <canvas> tag, I didn’t know anything about it until you mentioned it, thanks!

    https://developer.mozilla.org/en/Canvas_tutorial
    http://www.w3schools.com/tags/html5_canvas.asp

    Thanks for your input, I will be looking into this~ :-)

  9. Thomas Milburn

    A clock in action can be found at
    http://folk.uib.no/nfylk/concordle_dev/clock_canvas.html
    Only works in firefox and safari although you can use ExplorerCanvas
    http://excanvas.sourceforge.net/
    to make it IE compatible.

  10. garaj kapısı

    :) wow i like it

  11. Tiago Celestino

    Tutorial muito bom. Vou usar em um projeto de minha companhia. :D

  12. Ömür UÇUM

    I like it :)

  13. Aytekinet

    Woowww I LIKE IT 1!!

  14. arma bariyer

    thanks

  15. KVron

    Great!!! Excellent tutorial, I’ve learned a lot with your page, thank you!!

  16. nizaradil

    I like it

  17. s

    good

  18. FLiTz Ferdinand

    cool. you’re soh brilliant..

  19. popper

    good job ~

  20. GeLZa

    what a good job soh….
    you rock…!!

  21. GeLZa

    when the page refreshes, the second hand back to number twelve???

  22. Tim Watchmens

    A clock using CSS, pretty interesting idea!

  23. tzBlogz

    This clock in conjunction with some sort of Countdown Timer would be way cool•ness!
    There are a few jquery countdown scripts around, maybe the countdown numbers could be place on the face of the clock or something?
    One like this maybe:
    http://www.littlewebthings.com/2010/02/jquery-countdown-script/
    hmmm…..

  24. Faisal Shahzad

    Hey.. Dude.. nice work.. :)

Speak Your Mind...

Post HTMLNeed to Post HTML Code? Use Postable to post code friendly html. *Wrap all html code in <pre></pre> tags.
Post HTMLNeed to Keep Updated with Comments? Subscribe to comments now.

CSS Gallery