Create a Functional Analog Clock
Build a beautiful analog clock using CSS positioning, transforms, and transitions.
This exercise demonstrates how position: absolute combined with transform-origin
allows you to precisely position and rotate clock hands from a central pivot point.
You'll create hour, minute, and second hands that rotate smoothly around the clock face.
Task 1: 📸 View Reference Picture
💡 Important Tip: Remember to refresh your browser (F5 or Cmd+R) after completing each step to see the CSS changes take effect!
Part A: Clock Container Setup
- Create
.clock-containerwithposition: relative;to establish positioning context for clock hands. - For
.clock-container, setwidth: 350px;andheight: 350px;for the clock size. - For
.clock-container, addmargin: 40px auto;to center the clock horizontally. - For
.clock-container, addbackground: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);for gradient background. - For
.clock-container, addborder-radius: 50%;to create a circular shape. - For
.clock-container, addbox-shadow: 0 10px 40px rgba(0, 0, 0, 0.2), inset 0 0 20px rgba(255, 255, 255, 0.1);for depth. - For
.clock-container, addborder: 10px solid white;for a clean border frame.
Part B: Clock Center Dot
- Create
.clock-centerwithposition: absolute;for absolute positioning. - For
.clock-center, settop: 50%;andleft: 50%;to position at center. - For
.clock-center, addtransform: translate(-50%, -50%);to center the dot perfectly. - For
.clock-center, setwidth: 20px;andheight: 20px;for the center dot size. - For
.clock-center, addbackground: white;for visibility. - For
.clock-center, addborder-radius: 50%;to make it circular. - For
.clock-center, addz-index: 10;to ensure it stays on top of clock hands. - For
.clock-center, addbox-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);for subtle depth.
Part C: Clock Hands Base Styling
- Create
.clock-handwithposition: absolute;for positioning hands. - For
.clock-hand, setbottom: 50%;to position hand starting from center. - For
.clock-hand, setleft: 50%;to align hand at horizontal center. - For
.clock-hand, addtransform-origin: center bottom;— this is crucial for rotation from the base. - For
.clock-hand, addbackground: white;for hand visibility. - For
.clock-hand, addborder-radius: 4px 4px 0 0;for rounded top corners. - For
.clock-hand, addbox-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);for depth effect.
Part D: Individual Hand Styling
- Create
.hour-handwithwidth: 8px;andheight: 70px;for shorter, thicker hour hand. - For
.hour-hand, settransform: translateX(-50%) rotate(0deg);(starting at 12 o'clock position for demo). - For
.hour-hand, addbackground: rgba(255, 255, 255, 0.9);for slightly transparent white. - For
.hour-hand, addz-index: 3;to layer behind minute and second hands. - Create
.minute-handwithwidth: 6px;andheight: 100px;for longer, thinner minute hand. - For
.minute-hand, settransform: translateX(-50%) rotate(90deg);(starting at 3 o'clock for demo). - For
.minute-hand, addbackground: rgba(255, 255, 255, 0.95);for nearly opaque white. - For
.minute-hand, addz-index: 4;to layer above hour hand. - Create
.second-handwithwidth: 3px;andheight: 120px;for longest, thinnest second hand. - For
.second-hand, settransform: translateX(-50%) rotate(180deg);(starting at 6 o'clock for demo). - For
.second-hand, addbackground: #ef4444;for red accent color. - For
.second-hand, addz-index: 5;to layer above all other hands.
Part E: Adding Animations to Clock Hands
- For
.hour-hand, addanimation: rotate-hour-hand 120s linear infinite;to rotate continuously (demo speed: 2 minutes per rotation). - For
.minute-hand, addanimation: rotate-minute-hand 10s linear infinite;to rotate faster (demo speed: 10 seconds per rotation). - For
.second-hand, addanimation: rotate-second-hand 6s linear infinite;to rotate fastest (demo speed: 6 seconds per rotation).
Part F: Hour Markers (Optional Enhancement)
- Create
.hour-markerwithposition: absolute;for positioning markers. - For
.hour-marker, setwidth: 4px;andheight: 15px;for marker dimensions. - For
.hour-marker, addbackground: rgba(255, 255, 255, 0.6);for semi-transparent white. - For
.hour-marker, settop: 10px;andleft: 50%;for positioning at 12 o'clock. - For
.hour-marker, addtransform: translateX(-50%);to center the marker. - For
.hour-marker, addtransform-origin: center 140px;to rotate around clock center. - Use
:nth-childselectors to rotate markers:.hour-marker:nth-child(2) { transform: translateX(-50%) rotate(30deg); } - Continue for all 12 markers with 30-degree increments (60deg, 90deg, 120deg, etc.).
✨ Congratulations! You've built a working analog clock with CSS animations! Notice how the three @keyframes definitions at the top of Part D define the rotation animations that are applied to each clock hand.
Key Learning Points:
position: relativeon the clock container creates a positioning context for absolutely positioned children.position: absolutewithtop/leftallows precise placement of clock hands.transform-origin: center bottommakes clock hands rotate from their base (like real clock hands).z-indexcontrols the layering order (second hand on top, hour hand on bottom).@keyframesdefine the animation from start to finish (0deg to 360deg rotation).animationproperty applies the keyframes with duration, timing function, and iteration count.
Tip: In a real implementation, you would use JavaScript to set the rotation angles based on the current time. The transform: rotate() angles in this demo are static for demonstration purposes!