Follow us on Instagram for updates link

Newsletter Signup Form Using HTML, CSS, and Js

Creating a "Newsletter Signup" Form Using HTML, CSS, and JavaScript

Published on December 31, 2024

A "Newsletter Signup" form is a great way to grow your audience and keep them updated. In this post, we'll show how to build a responsive newsletter signup 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>Newsletter Signup</title>

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

</head>

<body>

    <div class="form-container">

        <h2>Subscribe to Our Newsletter</h2>

        <form id="newsletterForm">

            <label for="email">Enter your email:</label>

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

            <button type="submit">Subscribe</button>

        </form>

        <div id="message"></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: #f3f3f3;

}

.form-container {

    background: #fff;

    padding: 20px;

    border-radius: 10px;

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

    text-align: center;

    width: 300px;

}

h2 {

    color: #333;

}

input[type="email"] {

    width: 100%;

    padding: 10px;

    margin: 10px 0;

    border: 1px solid #ccc;

    border-radius: 5px;

}

button {

    background: #28a745;

    color: #fff;

    border: none;

    padding: 10px 20px;

    border-radius: 5px;

    cursor: pointer;

    transition: background 0.3s ease;

}

button:hover {

    background: #218838;

}



  

JavaScript Code



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

    e.preventDefault();

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

    if (email) {

        document.getElementById("message").textContent = "Thank you for subscribing!";

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

    } else {

        document.getElementById("message").textContent = "Please enter a valid email.";

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

    }

});



  

How It Works

1. **HTML**: The structure includes an email input field and a submit button wrapped in a form element. 2. **CSS**: Styles make the form visually appealing, with responsive adjustments and hover effects. 3. **JavaScript**: On form submission, an event listener validates the input and displays a success or error message without reloading the page.