Subscribe Us

LightBlog

Breaking

LightBlog

Friday, July 19, 2024

Neon Clock Using Pure CSS

html> Creating a Neon Clock with Pure CSS and JavaScript

Creating a Neon Clock with Pure CSS

Author: AMAL AJI | Published Date: January 24, 2024

neon clock using css

Introduction

In this comprehensive guide, we will walk you through the process of creating a visually stunning neon clock using pure HTML, CSS, and JavaScript. This project will not only enhance your web design skills but also give your website a futuristic touch with vibrant neon effects and smooth animations. Whether you're a beginner or an experienced developer, you'll find the detailed explanations and step-by-step instructions helpful in creating a dynamic and stylish clock for your web projects.

We'll cover everything from setting up the HTML structure to applying intricate CSS styles and integrating JavaScript for real-time updates. By the end of this guide, you'll be equipped with the knowledge to create your own neon clock and apply similar techniques to other web design projects. So, let's dive in and start building this eye-catching clock!

HTML Structure

The HTML structure for our neon clock is quite simple yet effective. We create a main container that holds our clock's digits and colon separators. This setup allows us to easily apply CSS styles and JavaScript functionality. Here's a breakdown of the HTML components:

  • Wrapper: The main container for our clock.
  • Main: This element will contain the clock's digits and colon separators.

Here's the basic HTML structure for our neon clock:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Neon Clock</title>
  <link href="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@300;400;600;700&display=swap" rel="stylesheet">
  <style>
    /* Include your CSS here */
  </style>
</head>
<body>
  <div class="wrapper">
    <main></main>
  </div>
  <script src="script.js"></script>
</body>
</html>
          
Copied!

JavaScript for Real-Time Updates

To make our neon clock functional, we'll use JavaScript to update the time dynamically. The JavaScript code will handle the clock's digits and colon separators, ensuring the time is always accurate. Below is the explanation of the JavaScript code:

  • Add Digits: Function to create digit elements and display them.
  • Add Colon: Function to create and display colon separators.
  • Init Function: Initializes the clock and starts the update loop.
  • Update Function: Updates the clock every second to keep the time accurate.

Here is the JavaScript code:

const bars = [
  ['end', 'top'],
  ['side', 'top', 'left'],
  ['side', 'top', 'right'],
  ['side', 'bottom', 'left'],
  ['side', 'bottom', 'right'],
  ['side', 'top', 'left', 'right'],
  ['top', 'bottom', 'left', 'right']
];

const wrapper = document.querySelector('.wrapper');
const main = document.querySelector('main');
let seconds = 0;

function addDigit(digit, position) {
  const digitElement = document.createElement('div');
  digitElement.classList.add('digit', digit-${digit});
  digitElement.style.position = 'relative';
  main.appendChild(digitElement);

  for (const bar of bars[digit]) {
    const barElement = document.createElement('div');
    barElement.classList.add('bar', bar-${bar});
    digitElement.appendChild(barElement);
  }

  digitElement.style.left = ${position * 150}px;
}

function addColon() {
  const colonElement = document.createElement('div');
  colonElement.classList.add('colon');
  main.appendChild(colonElement);
}

function init() {
  for (let i = 0; i < 6; i++) {
    addDigit(0, i);
  }

  addColon();
  update();
}

function update() {
  const now = new Date();
  const hours = now.getHours();
  const minutes = now.getMinutes();
  const seconds = now.getSeconds();

  main.querySelectorAll('.digit').forEach((digit, index) => {
    digit.classList.remove('digit-' + index);
    digit.classList.add('digit-' + (index === 0 ? hours : index === 1 ? minutes : seconds));
  });

  setTimeout(update, 1000);
}

init();
          
Copied!

Adding Styles with CSS

The CSS will handle the visual aspects of our neon clock, including the neon glow effect and layout. We use CSS to style the digits, colon separators, and overall clock appearance. Below are the key aspects of our CSS styling:

  • Neon Effect: Achieved using text-shadow and box-shadow properties.
  • Digit Styling: Creates a neon glow for each digit.
  • Colon Styling: Adds a glowing effect to the colon separators.

Here is the CSS code:

body {
  background: #000;
  color: #fff;
  font-family: 'Nunito Sans', sans-serif;
}

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

main {
  display: flex;
  align-items: center;
  font-size: 4rem;
}

.digit {
  position: relative;
  width: 100px;
  height: 150px;
  background: rgba(255, 255, 255, 0.1);
  margin: 0 10px;
  border-radius: 5px;
  box-shadow: 0 0 20px rgba(255, 255, 255, 0.8);
}

.bar {
  position: absolute;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 3px;
}

.bar-end {
  width: 10px;
  height: 30px;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
}

.bar-top {
  width: 80px;
  height: 10px;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
}

.bar-side {
  width: 10px;
  height: 80px;
  top: 0;
}

.bar-left {
  left: 0;
}

.bar-right {
  right: 0;
}

.colon {
  width: 20px;
  height: 100px;
  background: rgba(255, 255, 255, 0.8);
  margin: 0 10px;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(255, 255, 255, 0.8);
}
          
Copied!

Enhancing User Interaction

To make our neon clock more interactive and engaging, we can add features such as:

  • Live Clock Update: Ensure the clock updates every second to reflect the current time accurately.
  • Customization Options: Allow users to customize the clock's appearance, such as changing colors or sizes.
  • Download Button: Provide an option for users to download the code and try it on their own websites.

Here's how you can add a download button:

<a href="neon-clock.zip" class="download-btn" download>Download Neon Clock Code</a>
          

With these features, users will find the clock more engaging and useful for their projects. Adding customization options can make your clock stand out and cater to a wider audience.

Conclusion

In this guide, we've walked through the process of creating a neon clock using HTML, CSS, and JavaScript. From setting up the HTML structure to applying CSS styles and adding JavaScript for real-time updates, we've covered every aspect of building a functional and visually stunning neon clock.

By following these steps, you should now have a fully operational neon clock that you can use in your web projects. This project demonstrates how combining simple web technologies can produce impressive and engaging effects. Feel free to experiment with different styles and functionalities to make the clock your own.

We hope you found this guide helpful and informative. If you have any questions or feedback, please leave a comment below. Happy coding!


Follow our Blog and Stay Updated with the Latest Insights!

No comments:

Post a Comment

Search here..

Adbox