How to Create a Stylish Digital Watch with HTML, CSS, and JavaScript
June 11, 2024 ⚊ 2 Min read ⚊ CSS HTML JAVASCRIPTCreating a digital watch similar to an Apple Watch using HTML, CSS, and JavaScript involves several steps. We’ll build a simple, functional digital clock that displays the current time, styled to resemble the sleek design of an Apple Watch.
HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Digital Watch</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="watch">
<div class="screen">
<div class="watermark"></div>
<div id="time" class="time"></div>
<div id="date" class="date"></div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #333;
margin: 0;
font-family: Arial, sans-serif;
}
.watch {
width: 300px;
height: 350px;
background: linear-gradient(145deg, #1c1c1c, #444);
border-radius: 30px;
box-shadow: 10px 10px 20px #111, -10px -10px 20px #555;
display: flex;
justify-content: center;
align-items: center;
}
.screen {
position: relative;
width: 260px;
height: 310px;
background: #000;
border-radius: 25px;
box-shadow: inset 5px 5px 10px #111, inset -5px -5px 10px #555;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
color: #00ff00;
overflow: hidden;
}
.watermark {
position: absolute;
width: 100%;
height: 100%;
background: url('watermark.png') no-repeat center center;
background-size: contain;
opacity: 0.1;
}
.time {
font-size: 36px;
font-weight: bold;
margin-bottom: 10px;
z-index: 1;
}
.date {
font-size: 18px;
z-index: 1;
}
JavaScript (script.js)
function updateTime() {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
const timeString = `${hours}:${minutes}:${seconds}`;
const dateOptions = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
const dateString = now.toLocaleDateString(undefined, dateOptions);
document.getElementById('time').textContent = timeString;
document.getElementById('date').textContent = dateString;
}
setInterval(updateTime, 1000);
updateTime(); // Initial call to display the time immediately
This code provides a simple digital watch interface. You can further enhance it with additional features or more advanced styling to better mimic the look and feel of an Apple Watch.