Digital clock using html css javascript |
In this article, we given a Digital Clock using html css and javascript as a project with source code. In this project, we created a digital clock structure using html, styled with css and added a functonality with javascript. It is a digital clock which shows the actual timing.
So, Must try this project for practice and use in vs code to check how it works. We have given a complete source code of Digital Clock using html css and javascript. Let's go and check the source code as given below -
To add this project in vs code, follow the given process :-
1) Create a New folder in file manager name as - Digital-Clock.
2) Open the folder in VS code.
3) Add file name as - index.html and copy the html code as given.
4) Add another file name as - style.css and copy the css code as given.
5) Add next file name as - script.js and copy the javascript code as given.
6) Then run the code or check the preview with code preview extension in vs code.
Digital Clock using html css and javascript source code
Here we given a html, css and js source code of Digital Clock project as given -
HTML
In HTML, we created a structure by adding the container. In this container, we added numbers to show timing. Copy the html code and paste it into index.html file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Digital Clock</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="Time-container"> <div class="digit"> <li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li> </div> <div class="digit"> <li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li> </div> <div class="colon"> : </div> <div class="digit"> <li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li> </div><div class="digit"> <li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li> </div> <div class="colon"> : </div> <div class="digit"> <li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li> </div><div class="digit"><li>0</li><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li> </div> <div class="digit"> <li>AM</li> <li>PM</li> </div> </div> </body> <script src="script.js"></script> </html>
CSS
In css, we added a styling with css properties. Copy the given css code and paste it into style.css file.
* { padding: 0; margin: 0; user-select: none; } body { display: grid; place-items: center; height: 100vh; background-color: rgb(0, 0, 0); } .Time-container { width: auto; height: 50px; padding: 5px; display: flex; overflow: hidden; background-color: #ff0000; border-radius: 30px; color: rgb(255, 255, 255); font-size: 30px; font-family: sans-serif; } .Time-container li { list-style-type: none; height: 100%; display: grid; place-items: center; padding: 0px 10px; transition-duration: 0.3s; } .Time-container .colon { font-size: 30px; display: grid; place-items: center; font-weight: 800; } .digit li { position: relative; }
Javascript
In javascript, we added a functionality to show the actual timing with the help of functions. Copy the given javascript code and paste it into script.js file.
setInterval(function () { var Time = new Date() var hour = Time.getHours() var minutes = Time.getMinutes() var seconds = Time.getSeconds() var amOrpm = 0; if (hour > 12) { hour = Time.getHours() - 12 amOrpm = 1 } if (hour < 10) { hour = "0" + hour } if (minutes < 10) { minutes = "0" + minutes } if (seconds < 10) { seconds = "0" + seconds } var Time = [hour, minutes, seconds, amOrpm] var digitDivs = Array.from(document.querySelectorAll(".digit")) var digitDivs = digitDivs.slice(0, digitDivs.length) var Time = Time.join("").split("") for (let i in Time) { Array.from(digitDivs[i].querySelectorAll("li")).forEach((e) => { e.style.top = -(parseInt(Time[i]) * 50) + "px" }) } }, 100)
Output
Check this output of Digital Clock project -
Run the code -
To run the code, we given a online compiler where you can paste the code and check the preview.
Click here - HTML CSS JS ONLINE COMPILER
Also see this -
Hope this helps you to know about Digital Clock using html css and javascript with source code. If you have any confusion or query then you can contact us on our coding community.
Checkout others projects on our website for more and join our coding community on telegram to get knowledge about coding with proper material such as notes, tutorials, projects and more.
Tags:
projects