In this tutorial, we will walk through the steps to create a responsive modal sign-up form that can be used on any website. Modal popups are great for capturing user information, especially for newsletter sign-ups or account creation, while maintaining an elegant user experience.

We’ll use HTML, CSS, and a little JavaScript to build the modal form, ensuring that it works well on both desktop and mobile devices.


1. HTML Structure

Start by building the basic structure of your modal in HTML.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Modal Sign-Up Form</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>

    <!-- Button to open the modal -->
    <button id="openModalBtn" class="open-modal-btn">Sign Up</button>

    <!-- Modal Structure -->
    <div id="myModal" class="modal">
        <div class="modal-content">
            <span class="close-btn">&times;</span>
            <h2>Sign Up</h2>
            <form id="signUpForm">
                <label for="name">Name:</label>
                <input type="text" id="name" name="name" required>

                <label for="email">Email:</label>
                <input type="email" id="email" name="email" required>

                <label for="password">Password:</label>
                <input type="password" id="password" name="password" required>

                <button type="submit">Submit</button>
            </form>
        </div>
    </div>

    <script src="scripts.js"></script>
</body>
</html>
  • The Sign Up button triggers the modal to open.
  • The modal itself is hidden by default. It consists of a div with a form inside it, which contains text input fields for name, email, and password.

2. CSS Styling

Now, let’s style the modal using CSS, making sure it’s responsive and looks good on different screen sizes.

/* Basic Reset */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, sans-serif;
}

/* Button to open modal */
.open-modal-btn {
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    background-color: #28a745;
    color: white;
    border: none;
    border-radius: 5px;
    margin: 20px;
}

/* Modal styling */
.modal {
    display: none; /* Hidden by default */
    position: fixed;
    z-index: 1; /* On top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    background-color: rgba(0, 0, 0, 0.5); /* Black background with opacity */
}

/* Modal Content Box */
.modal-content {
    position: relative;
    margin: 10% auto; /* 10% from the top and centered */
    padding: 20px;
    width: 90%;
    max-width: 500px;
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* Close button */
.close-btn {
    position: absolute;
    top: 10px;
    right: 20px;
    font-size: 30px;
    cursor: pointer;
    color: #333;
}

/* Form styling */
form {
    display: flex;
    flex-direction: column;
}

form label {
    margin: 10px 0 5px;
}

form input {
    padding: 10px;
    margin-bottom: 20px;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 5px;
}

form button {
    padding: 10px;
    background-color: #007bff;
    color: white;
    font-size: 16px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

/* Responsive adjustments */
@media (max-width: 600px) {
    .modal-content {
        width: 90%;
        padding: 15px;
    }
    
    form button {
        font-size: 14px;
        padding: 8px;
    }
}
  • The modal is initially set to display: none, which will be toggled using JavaScript.
  • The modal content is centered on the screen with a maximum width of 500px, making it responsive.
  • We use @media queries to adjust the layout for smaller screens, ensuring the form remains user-friendly on mobile devices.

3. JavaScript for Modal Functionality

Now, we’ll add the JavaScript to handle the opening and closing of the modal.

// JavaScript for opening and closing the modal
document.addEventListener('DOMContentLoaded', function () {
    const modal = document.getElementById('myModal');
    const openModalBtn = document.getElementById('openModalBtn');
    const closeModalBtn = document.querySelector('.close-btn');

    // Open modal when the button is clicked
    openModalBtn.addEventListener('click', function () {
        modal.style.display = 'block';
    });

    // Close modal when the close button (x) is clicked
    closeModalBtn.addEventListener('click', function () {
        modal.style.display = 'none';
    });

    // Close modal when clicking outside the modal content
    window.addEventListener('click', function (event) {
        if (event.target === modal) {
            modal.style.display = 'none';
        }
    });
});
  • openModalBtn listens for a click on the sign-up button and displays the modal by setting modal.style.display = 'block';.
  • closeModalBtn listens for a click on the close button (×) and hides the modal by setting modal.style.display = 'none';.
  • Clicking outside the modal content also closes the modal.

4. Testing Responsiveness

You can test the form by opening it on various devices or using the browser’s developer tools (press F12 and toggle the device toolbar). The modal form should adjust its width and padding according to the screen size, maintaining a user-friendly design on both mobile and desktop.


You’ve just created a fully functional and responsive modal sign-up form for your website! Modals are a great way to collect user input without navigating away from the current page, making them perfect for newsletter sign-ups or account creation.

Feel free to modify this tutorial to suit your specific design needs, such as changing colors, adding validation, or enhancing the form with more fields.

Additional Enhancements

  • Form Validation: You can add client-side validation using JavaScript or HTML5 validation attributes.
  • Animations: Add some fade-in/fade-out animations for a smoother user experience when the modal opens or closes.
  • Backend Integration: Hook the form to a backend API to handle sign-ups, store data in a database, or trigger emails.

Enjoy creating your modal forms and enhancing the user experience on your website!

Leave a Reply

Your email address will not be published. Required fields are marked *