This tutorial will guide you through creating a visually appealing analog clock using HTML, CSS, and JavaScript. We’ll build an interactive clock that displays the current time, date, and day of the week. This is a great project to enhance your front-end development skills.
Step 1: Setting up the Project
Adding Header Assets
First, let’s add some necessary assets to the header of your HTML document. This includes linking to external CSS for styling and a JavaScript library.
<link rel="stylesheet" href="https://public.codepenassets.com/css/reset-2.0.min.css"> <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Poiret+One'> <script src="https://public.codepenassets.com/js/prefixfree-1.0.7.min.js"></script>
Step 2: Creating the HTML Structure
Here, we’ll structure the HTML to represent the clock’s components: the clock face, hands, numbers, and a display for the date and day.
<div class="clock">
<div>
<div class="info date"></div>
<div class="info day"></div>
</div>
<div class="dot"></div>
<div>
<div class="hour-hand"></div>
<div class="minute-hand"></div>
<div class="second-hand"></div>
</div>
<div>
<span class="h3">3</span>
<span class="h6">6</span>
<span class="h9">9</span>
<span class="h12">12</span>
</div>
<div class="diallines"></div>
</div>
Step 3: Styling with CSS
Now, let’s add the CSS to style our clock. This will make it visually appealing and functional. We will style the clock face, hands, numbers, and other elements.
.clock {
background: #ececec;
width: 300px;
height: 300px;
margin: 8% auto 0;
border-radius: 50%;
border: 14px solid #333;
position: relative;
box-shadow: 0 2vw 4vw -1vw rgba(0,0,0,0.8);
}
.dot {
width: 14px;
height: 14px;
border-radius: 50%;
background: #ccc;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
position: absolute;
z-index: 10;
box-shadow: 0 2px 4px -1px black;
}
.hour-hand {
position: absolute;
z-index: 5;
width: 4px;
height: 65px;
background: #333;
top: 79px;
transform-origin: 50% 72px;
left: 50%;
margin-left: -2px;
border-top-left-radius: 50%;
border-top-right-radius: 50%;
}
.minute-hand {
position: absolute;
z-index: 6;
width: 4px;
height: 100px;
background: #666;
top: 46px;
left: 50%;
margin-left: -2px;
border-top-left-radius: 50%;
border-top-right-radius: 50%;
transform-origin: 50% 105px;
}
.second-hand {
position: absolute;
z-index: 7;
width: 2px;
height: 120px;
background: gold;
top: 26px;
lefT: 50%;
margin-left: -1px;
border-top-left-radius: 50%;
border-top-right-radius: 50%;
transform-origin: 50% 125px;
}
span {
display: inline-block;
position: absolute;
color: #333;
font-size: 22px;
font-family: 'Poiret One';
font-weight: 700;
z-index: 4;
}
.h12 {
top: 30px;
left: 50%;
margin-left: -9px;
}
.h3 {
top: 140px;
right: 30px;
}
.h6 {
bottom: 30px;
left: 50%;
margin-left: -5px;
}
.h9 {
left: 32px;
top: 140px;
}
.diallines {
position: absolute;
z-index: 2;
width: 2px;
height: 15px;
background: #666;
left: 50%;
margin-left: -1px;
transform-origin: 50% 150px;
}
.diallines:nth-of-type(5n) {
position: absolute;
z-index: 2;
width: 4px;
height: 25px;
background: #666;
left: 50%;
margin-left: -1px;
transform-origin: 50% 150px;
}
.info {
position: absolute;
width: 120px;
height: 20px;
border-radius: 7px;
background: #ccc;
text-align: center;
line-height: 20px;
color: #000;
font-size: 11px;
top: 200px;
left: 50%;
margin-left: -60px;
font-family: "Poiret One";
font-weight: 700;
z-index: 3;
letter-spacing: 3px;
margin-left: -60px;
left: 50%;
}
.date {
top: 80px;
}
.day {
top: 200px;
}
Step 4: Adding JavaScript Functionality
Finally, we’ll write the JavaScript code to make the clock hands move in real-time and update the date and day. This involves calculating the angles for the hands and updating the display elements.
var dialLines = document.getElementsByClassName('diallines');
var clockEl = document.getElementsByClassName('clock')[0];
for (var i = 1; i < 60; i++) {
clockEl.innerHTML += "<div class='diallines'></div>";
dialLines[i].style.transform = "rotate(" + 6 * i + "deg)";
}
function clock() {
var weekday = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
d = new Date(),
h = d.getHours(),
m = d.getMinutes(),
s = d.getSeconds(),
date = d.getDate(),
month = d.getMonth() + 1,
year = d.getFullYear(),
hDeg = h * 30 + m * (360/720),
mDeg = m * 6 + s * (360/3600),
sDeg = s * 6,
hEl = document.querySelector('.hour-hand'),
mEl = document.querySelector('.minute-hand'),
sEl = document.querySelector('.second-hand'),
dateEl = document.querySelector('.date'),
dayEl = document.querySelector('.day');
var day = weekday[d.getDay()];
if(month < 9) {
month = "0" + month;
}
hEl.style.transform = "rotate("+hDeg+"deg)";
mEl.style.transform = "rotate("+mDeg+"deg)";
sEl.style.transform = "rotate("+sDeg+"deg)";
dateEl.innerHTML = date+"/"+month+"/"+year;
dayEl.innerHTML = day;
}
setInterval("clock()", 100);
Congratulations! You’ve successfully created your analog clock. Feel free to customize the styles and add more features.



