Follow us on Instagram for updates link

Contact Us Form Using HTML, CSS, and JavaScript

Creating a "Contact Us" Form Using HTML, CSS, and JavaScript

Published on January 1, 2025

A "Contact Us" form is essential for businesses and websites to communicate with their audience. In this post, we'll create a fully functional contact form using HTML, CSS, and JavaScript.



HTML Code



<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Contact Us Form</title>

    <link rel="stylesheet" href="style.css">

</head>

<body>

    <div class="form-container">

        <h2>Contact Us</h2>

        <form id="contactForm">

            <label for="name">Name:</label>

            <input type="text" id="name" name="name" placeholder="Your Name" required>

            <label for="email">Email:</label>

            <input type="email" id="email" name="email" placeholder="Your Email" required>

            <label for="message">Message:</label>

            <textarea id="message" name="message" placeholder="Your Message" rows="4" required></textarea>

            <button type="submit">Send Message</button>

        </form>

        <div id="responseMessage"></div>

    </div>

    <script src="script.js"></script>

</body>

</html>



  

CSS Code



body {

    font-family: Arial, sans-serif;

    display: flex;

    justify-content: center;

    align-items: center;

    height: 100vh;

    margin: 0;

    background-color: #f9f9f9;

}

.form-container {

    background: #fff;

    padding: 20px;

    border-radius: 10px;

    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

    width: 400px;

    text-align: left;

}

h2 {

    color: #333;

    margin-bottom: 20px;

    text-align: center;

}

label {

    display: block;

    margin-bottom: 5px;

    font-weight: bold;

    color: #555;

}

input, textarea {

    width: 100%;

    padding: 10px;

    margin-bottom: 15px;

    border: 1px solid #ccc;

    border-radius: 5px;

    font-size: 14px;

}

button {

    background: #007BFF;

    color: #fff;

    border: none;

    padding: 10px 20px;

    border-radius: 5px;

    cursor: pointer;

    width: 100%;

    font-size: 16px;

    transition: background 0.3s ease;

}

button:hover {

    background: #0056b3;

}



  

JavaScript Code



document.getElementById("contactForm").addEventListener("submit", function (e) {

    e.preventDefault();

    const name = document.getElementById("name").value;

    const email = document.getElementById("email").value;

    const message = document.getElementById("message").value;

    if (name && email && message) {

        document.getElementById("responseMessage").textContent = "Thank you for contacting us!";

        document.getElementById("responseMessage").style.color = "green";

        this.reset();

    } else {

        document.getElementById("responseMessage").textContent = "Please fill in all fields.";

        document.getElementById("responseMessage").style.color = "red";

    }

});



  

How It Works

1. **HTML**: The form structure includes input fields for the user's name, email, and message, along with a submit button. 2. **CSS**: The styling ensures the form is visually appealing, responsive, and user-friendly. 3. **JavaScript**: The script validates the form fields and displays a success or error message upon submission without refreshing the page.