How To Make Age Calculator Using HTML CSS JS

 How To Make Age Calculator Using HTML CSS JS


As a programmer, developer, or front-end enthusiast, you may often come across the need to create interactive and useful tools for your websites or web applications. How To Make Age Calculator Using HTML CSS JS.One such tool that can be handy is an age calculator. In this article, we will explore how you can create an age calculator using HTML, CSS, and JavaScript.

How To Make Age Calculator Using HTML CSS JS


To begin with, let's set up the basic HTML structure for our age calculator. Open your favorite text editor and create a new HTML file. Start by adding the HTML doctype declaration and the opening and closing HTML tags. Inside the HTML tags, create the head and body sections.How To Make Age Calculator Using HTML CSS JS

HTML CODE

<!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>Age Calculator</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <h1>Age Calculator</h1>
        <div class="form">
            <label for="birthday">Enter you date of birth</label>
            <input type="date" id="birthday" name="birthday">
            <button id="btn">Calculate Age</button>
            <p id="result">Your age is 21 years old</p>
        </div>
    </div>

    <script src="index.js"></script>
</body>
</html>


Within the head section, add a title for your age calculator. You can use something like "Age Calculator" or be creative with your own title. Next, link a CSS file to style our age calculator. Don't worry if you haven't created the CSS file yet; we will do it shortly.How To Make Age Calculator Using HTML CSS JS

CSS CODE


body {
  margin: 0;
  padding: 20px;
  font-family: "Montserrat", sans-serif;
  background-color: #f7f7f7;
}

.container {
  background-color: white;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  padding: 20px;
  max-width: 600px;
  margin: 0 auto;
  border-radius: 5px;
  margin-top: 50px;
}

h1 {
  font-size: 36px;
  text-align: center;
  margin-top: 0;
  margin-bottom: 20px;
}

.form {
  display: flex;
  flex-direction: column;
  align-items: center;
}

label {
  font-weight: bold;
  margin-bottom: 10px;
}

input {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 5px;
  width: 100%;
  max-width: 300px;
}

button {
  background-color: #007bff;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  margin-top: 10px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #0062cc;
}

#result {
  margin-top: 20px;
  font-size: 24px;
  font-weight: bold;
}


Moving on to the body section, create a div element with a class or id of "container" to hold all the elements of our age calculator. Inside the container div, create a form element. This form will contain the input fields and the button to calculate the age.

Within the form, add a label and an input field for the user to enter their birthdate. Give the input field a unique id, such as "birthdate", to easily access it in our JavaScript code later. Repeat the same process for the current date input field.

Next, add a button inside the form with an id of "calculate-btn". This button will trigger the calculation of the age when clicked. Feel free to style the button using CSS to make it visually appealing.How To Make Age Calculator Using HTML CSS JS

JS CODE

const btnEl = document.getElementById("btn");
const birthdayEl = document.getElementById("birthday");
const resultEl = document.getElementById("result");

function calculateAge() {
  const birthdayValue = birthdayEl.value;
  if (birthdayValue === "") {
    alert("Please enter your birthday");
  } else {
    const age = getAge(birthdayValue);
    resultEl.innerText = `Your age is ${age} ${age > 1 ? "years" : "year"} old`;
  }
}

function getAge(birthdayValue) {
  const currentDate = new Date();
  const birthdayDate = new Date(birthdayValue);
  let age = currentDate.getFullYear() - birthdayDate.getFullYear();
  const month = currentDate.getMonth() - birthdayDate.getMonth();

  if (
    month < 0 ||
    (month === 0 && currentDate.getDate() < birthdayDate.getDate())
  ) {
    age--;
  }

  return age;
}

btnEl.addEventListener("click", calculateAge);

Now that our HTML structure is ready, let's move on to the CSS part. Create a new CSS file and link it to your HTML file. In the CSS file, target the container div and center it on the page using flexbox or any other CSS technique you prefer. Style the input fields, labels, and button to make them visually appealing and user-friendly.

Once we have styled our age calculator, it's time to add the JavaScript functionality. Create a new JavaScript file and link it to your HTML file. In the JavaScript file, start by selecting the calculate button using the document.getElementById() method and adding a click event listener to it.

Inside the event listener, retrieve the values entered by the user in the birthdate and current date input fields using the document.getElementById() method. Convert these values to Date objects using the new Date() constructor.

Now, we can calculate the age by subtracting the birthdate from the current date. To do this, we can use the getTime() method to get the timestamps of the birthdate and current date, subtract them, and divide the result by the number of milliseconds in a year.

Finally, display the calculated age to the user. You can create a new element, such as a paragraph or a span, and append it to the container div with the calculated age as its text content.

Congratulations! You have successfully created an age calculator using HTML, CSS, and JavaScript. Feel free to customize and enhance it further to suit your needs and preferences.How To Make Age Calculator Using HTML CSS JS

In conclusion, creating an age calculator using HTML, CSS, and JavaScript is a great way to practice your front-end development skills and provide a useful tool for your websites or web applications. By following the steps outlined in this article, you can easily build an age calculator that is both functional and visually appealing. Happy coding!

Post a Comment

Previous Post Next Post