Clock in HTML and CSS

beginnerdeveloper

Beginner Developer

Posted on September 26, 2021

Clock in HTML and CSS

Learn how to create a clock in HTML, CSS and Javascript

HTML and Javascript

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clock</title>
    <link rel="stylesheet" href="style.css" >
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div class="container">
        <div class="clock-container">
            <div class="timers">
                <div class="second"></div>
                <div class="minute"></div>
                <div class="hour"></div>
                <div class="axis"></div>
            </div>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 226.6 233.8" fill="#fff">
                <path d="M105.5 22.7V6.4h-5.9V4.3c.8 0 1.5-.1 2.2-.2.7-.1 1.4-.3 2-.7.6-.3 1.1-.8 1.5-1.3.4-.6.7-1.3.8-2.1h2.1v22.7h-2.7zM114.1 4.8c.3-1 .8-1.8 1.4-2.5.6-.7 1.4-1.3 2.4-1.7.9-.4 2-.6 3.2-.6 1 0 1.9.1 2.8.4.9.3 1.6.7 2.3 1.2.6.5 1.1 1.2 1.5 2 .4.8.6 1.7.6 2.8 0 1-.2 1.9-.5 2.7s-.7 1.5-1.2 2.1c-.5.6-1.1 1.2-1.8 1.6-.7.5-1.3 1-2 1.4-.7.4-1.4.8-2.1 1.3s-1.3.9-1.9 1.3c-.6.5-1.1 1-1.5 1.5s-.7 1.2-.8 1.9h11.6v2.4h-14.8c.1-1.3.3-2.5.7-3.4.4-.9.8-1.8 1.4-2.5s1.2-1.3 2-1.9c.7-.5 1.5-1 2.3-1.5 1-.6 1.8-1.1 2.5-1.6s1.3-1 1.8-1.5.8-1.1 1.1-1.7.4-1.3.4-2.1c0-.6-.1-1.2-.4-1.7-.2-.5-.5-.9-.9-1.3s-.9-.6-1.4-.8c-.5-.2-1.1-.3-1.7-.3-.8 0-1.5.2-2 .5-.6.3-1 .8-1.4 1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.1-.8.1-1.7.1-2.6zm2.9 5.4c.1 1.1.2 2 .5 3 .3.9.8 1.7 1.4 2.4s1.5 1 2.7 1c1.2 0 2-.3 2.7-1s1.1-1.4 1.4-2.4c.3-.9.5-1.9.5-3 .1-1.1.1-2 .1-2.9V63c0-.7-.1-1.3-.2-2s-.2-1.3-.4-2c-.2-.6-.4-1.2-.8-1.7s-.8-.9-1.3-1.2-1.2-.4-2-.4-1.4.1-2 .4c-.5.3-1 .7-1.3 1.2-.4.5-.6 1-.8 1.7-.2.6-.3 1.3-.4 2-.1.7-.1 1.3-.2 2v1.8c.1.9.1 1.9.1 2.9z" />
                <g>
                    <path d="M53.5 38.2V21.9h-5.9v-2.2c.8 0 1.5-.1 2.2-.2.7-.1 1.4-.3 2-.7.6-.3 1.1-.8 1.5-1.3.4-.6.7-1.3.8-2.1h2.1v22.7h-2.7zM69.1 38.2V21.9h-5.9v-2.2c.8 0 1.5-.1 2.2-.2.7-.1 1.4-.3 2-.7.6-.3 1.1-.8 1.5-1.3.4-.6.7-1.3.8-2.1h2.1v22.7h-2.7z" />
                </g>
            </svg>
        </div>
    </div>
    <script>
        var currentSec = getSecondsToday();

        var seconds = (currentSec / 60) % 1;
        var minutes = (currentSec / 3600) % 1;
        var hours = (currentSec / 43200) % 1;

        setTime(60 * seconds, "second");
        setTime(3600 * minutes, "minute");
        setTime(43200 * hours, "hour");

        function setTime(left, hand) {
            $("." + hand).css("animation-delay", "" + left * -1 + "s");
        }

        function getSecondsToday() {
        let now = new Date();

        let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

        let diff = now - today; 
            return Math.round(diff / 1000);
        }
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

CSS Code

body{
    margin: 0;
    padding: 0;
}
.clock-container{
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    max-width: 250px;
    margin: auto;
    position: relative;
}
.timers{
    position: absolute;
    top: 34%;
}
.container {
    background: #1b1e23;
}
p {
    color: #f4eed7;
    font-size: 0.8em;
    opacity: 0.75;
}
.clock {
    height: 200px;
    width: 200px;
    border-radius: 100px;
    display: flex;
    justify-content: center;
    position: relative;
}
.second, .minute, .hour, .indicator {
    position: absolute;
    left: calc(50% - 1px);
    width: 2px;
    background: #f4eed7;
    transform-origin: bottom center;
    z-index: 2;
    border-radius: 1px;
}
.second {
    height: 90px;
    margin-top: 10px;
    background: #4b9aaa;
    animation: time 60s infinite steps(60);
    z-index: 3;
}
.minute {
    height: 80px;
    margin-top: 20px;
    opacity: 0.75;
    animation: time 3600s linear infinite;
}
.hour {
    height: 60px;
    margin-top: 40px;
    animation: time 43200s linear infinite;
}
.indicator {
    height: 98px;
    border-top: 2px solid #4b9aaa;
    background: none;
}
.indicator:nth-of-type(5n) {
    opacity: 1;
    height: 93px;
    border-top: 7px solid #f4eed7;
}
.axis {
    background: #4b9aaa;
    width: 5px;
    height: 5px;
    border-radius: 3px;
    position: absolute;
    z-index: 4;
    top: 97px;
}
@keyframes time {
    to {
        transform: rotateZ(360deg);
   }
}
Enter fullscreen mode Exit fullscreen mode

Output

Clock Output

My Blog:- https://beginners-developer.blogspot.com
Website:- https://ejaazrkhan.github.io/portfolio

💖 💪 🙅 🚩
beginnerdeveloper
Beginner Developer

Posted on September 26, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related