Building colourful digital clock in HTML, CSS and JavaScript

Colourful Digital Clock | Coding Zemigle

Creating a colorful digital clock with HTML, CSS, and JavaScript is a great way to practice and showcase web development skills. This project integrates foundational web technologies to produce an interactive and visually appealing clock, bringing creativity and functionality together. In this article, we will explore the key concepts and approach for building this clock, focusing on the design principles and JavaScript logic that bring it to life.

1. Understanding the HTML for Structure

HTML (Hypertext Markup Language) is the foundation for structuring web content. In this case, we will use HTML to define the essential elements of our digital clock. The idea is to have a simple structure that holds the clock’s numbers and display. A minimal setup in HTML typically includes a few div elements for the hour, minute, and second components, along with an AM/PM indicator for a 12-hour format.

✲    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 Clock | Coding Zemigle</title>
    <link href="https://fonts.googleapis.com/css?family=Share+Tech+Mono" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="style.css">
</head>
<body>

<div>
    <h1></h1>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>

</body>
</html>



Semantic Structure: Ensuring that the HTML structure is easy to understand and modify. You might want to give IDs or classes to the key components (hours, minutes, seconds) for easy targeting with CSS and JavaScript.


2. Designing with CSS: A Splash of Color

CSS (Cascading Style Sheets) is where the design magic happens. This is where we can add vibrant colors, bold fonts, and modern styles to make the clock visually appealing. By using a combination of colors, gradients, and shadows, you can create an engaging clock that not only tells time but looks great while doing so.

✲    style.css
body {
    background: #000;
}

div {
    position: relative;
    background-color: #030303;
    background-image: linear-gradient(rgba(255, 255, 255, .1) 50%, transparent 50%, transparent);
    text-align: center;
    font-family: 'Share Tech Mono', sans-serif;
    font-size: 2.5em;
    width: 80%;
    max-width: 400px;
    margin: 2em auto;
    border-radius: .4em;
    padding: .5em;
}

div:before {
    content: 'snooze';
    font-size: .25em;
    line-height: .9em;
    padding: 0;
    height: .9em;
    width: 9em;
    position: absolute;
    top: -1.05em;
    right: 4.2em;
    color: #777;
    background-color: #000;
    background-image: linear-gradient(rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
    background-size: 1px 3px;
    border: 1px solid #444;
    border-bottom: 0;
    border-top-right-radius: 30px;
    border-top-left-radius: 30px;
}

h1 {
    background-color: #000;
    border: 3px solid #000;
    border-radius: .15em;
    padding: .1em .3em .1em .1em;
    display: inline-block;
    color: #fff;
    text-align: center;
    text-transform: lowercase;
    letter-spacing: -.05em;
    white-space: nowrap;
    margin: 0;
}

h1:before, h1:after {
    content: ' ';
    background: #000;
    background-image: linear-gradient(30deg, rgba(255, 255, 255, .2) 50%, transparent 50%, transparent);
    background-size: 3px 3px;
    position: absolute;
    height: .15em;
    width: .5em;
    bottom: -.15em;
    border-bottom-right-radius: 10px;
    border-bottom-left-radius: 10px;
}

h1:before {
    left: .5em;
}

h1:after {
    right: .5em;
}

/* Responsive adjustments */
@media (max-width: 600px) {
    div {
        font-size: 1.5em;
        padding: 1em;
        width: 90%;
    }
    
    h1 {
        padding: .3em .6em;
    }

    div:before {
        top: -1em;
        right: 2em;
        width: 7em;
    }
}



Typography: Choose a bold, digital-style font for the numbers to give it the feel of a real digital clock.

Color Schemes: Select a palette that enhances readability while adding a dynamic look. You can use contrasting colors for each digit or use gradients for a more modern touch.


Responsive Design: Ensure the clock looks great on all screen sizes. Using relative units like percentages, em, or rem will help in making the design adaptable.

Transitions and Animations: CSS transitions or animations can be used to create smooth updates when the time changes, enhancing the clock's interactivity.

3. JavaScript Logic: Bringing It to Life

JavaScript is the brain behind the digital clock, responsible for the real-time updates. By using built-in JavaScript methods like setInterval() and Date(), you can make the clock update every second. The challenge here lies in formatting the time correctly and displaying it in an intuitive way.

✲    script.js
var setTime = function() {
    var d = new Date();
    var s = d.getSeconds();
    var time = d.toLocaleTimeString();

    // Update styles dynamically based on seconds
    var styles = {
        'color': 'hsla(' + (s * 3) + ', 90%, 50%, 1)',
        'textShadow': '0px 0px 5px hsla(' + (s * 3) + ', 100%, 70%, .5)',
        'borderColor': 'hsla(' + (s * 3) + ', 50%, 20%, .4)',
        'backgroundColor': 'hsla(' + (s * 3) + ', 80%, 20%, .9)'
    };
    
    $('h1').text(time).css(styles);

    setTimeout(setTime, 1000);
}

setTime();



Real-Time Updates: Using setInterval() ensures that the clock is updated every second, maintaining the accuracy of the displayed time.

Time Formatting: You will need to format the time correctly, especially for a 12-hour format. Pay attention to how hours, minutes, and seconds are displayed. For example, adding a leading zero to single-digit numbers will make the clock look professional.

AM/PM Indication: For a 12-hour clock, it’s important to indicate whether the time is AM or PM. You can toggle this based on the hour value.

User Interaction (Optional): To make the clock more interactive, you could allow the user to switch between 12-hour and 24-hour formats or even change color themes dynamically through JavaScript.

Creating a colorful digital clock is a fantastic way to practice front-end development. It combines visual design with functional programming, giving you a well-rounded project to add to your portfolio. Whether you stick to a simple, minimalist design or go for a more complex and colorful clock, this project will not only enhance your coding skills but also offer a beautiful, functional piece of work that can be used on websites, blogs, or as a desktop widget.