DEV Community

Igbonekwu Christopher
Igbonekwu Christopher

Posted on

How to Create a Responsive Login/Signup Form Using HTML and CSS

In today's digital age, creating a user-friendly and responsive login/signup form is crucial for websites that require user authentication. A well-designed form not only enhances user experience but also contributes to the overall aesthetics of your website. In this comprehensive guide, we will walk you through the step-by-step process of creating a responsive login/signup form using HTML and CSS, complete with detailed explanations and code examples.

Step 1: Set Up the HTML Structure

The first step is to create the basic HTML structure for our login/signup form. We'll include input fields for email/username, password, and buttons for login/signup. Here's an example of how the HTML code looks:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login/Signup Form</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="form-container">
  <form id="login-form" class="form">
    <h2>Login</h2>
    <input type="text" id="username" placeholder="Email or Username" required>
    <input type="password" id="password" placeholder="Password" required>
    <button type="submit">Login</button>
  </form>
  <form id="signup-form" class="form">
    <h2>Sign Up</h2>
    <input type="text" id="new-username" placeholder="Email or Username" required>
    <input type="password" id="new-password" placeholder="Password" required>
    <button type="submit">Sign Up</button>
  </form>
</div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this HTML code snippet:

  • We have an <input> element of type "text" for the email/username and another <input> element of type "password" for the password in both the login and signup forms.
  • Each form has a unique ID (login-form for login and signup-form for signup) for JavaScript interactions and a class (form) for styling purposes.
  • The <button> elements inside each form are used for form submission.

Step 2: Style the Form with CSS

Next, let's add styles to our form using CSS to make it visually appealing and responsive. Create a new file named styles.css and link it to your HTML file using the <link> tag in the <head> section.

Here's an example of how the CSS code looks:

/* styles.css */
body {
  font-family: Arial, sans-serif;
  background-color: #f2f2f2;
  margin: 0;
  padding: 0;
}

.form-container {
  max-width: 400px;
  margin: 50px auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.form {
  display: flex;
  flex-direction: column;
}

h2 {
  margin-bottom: 20px;
  text-align: center;
}

input[type="text"],
input[type="password"],
button {
  margin-bottom: 15px;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 3px;
  font-size: 16px;
}

button {
  background-color: #4CAF50;
  color: white;
  border: none;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

@media screen and (max-width: 600px) {
  .form-container {
    width: 90%;
  }
}
Enter fullscreen mode Exit fullscreen mode

In this CSS code snippet:

  • We set the font family to Arial or sans-serif for better readability.
  • The .form-container class styles the container that holds the login/signup forms, setting its maximum width, margin, padding, background color, border radius, and box shadow.
  • The .form class styles the form elements inside the container, making them display in a column layout.
  • The @media query ensures that the form container becomes 90% of the width on screens with a maximum width of 600px, making the form responsive on smaller devices.

Step 3: Make the Form Functional and Responsive

Now that we have the HTML structure and CSS styles in place, let's add functionality to our form using JavaScript and make it fully responsive across different devices.

JavaScript Functionality (Optional)

You can add JavaScript code to handle form validation and submission. For example, you can validate the email format, check if passwords match in the signup form, and prevent form submission if any field is empty or invalid.

Here's an example of JavaScript code for form validation:

// JavaScript for form validation
document.getElementById('login-form').addEventListener('submit', function(event) {
  var username = document.getElementById('username').value;
  var password = document.getElementById('password').value;

  if (!username || !password) {
    alert('Please enter both username/email and password.');
    event.preventDefault();
  }
});

document.getElementById('signup-form').addEventListener('submit', function(event) {
  var newUsername = document.getElementById('new-username').value;
  var newPassword = document.getElementById('new-password').value;
  var confirmPassword = document.getElementById('confirm-password').value;

  if (!newUsername || !newPassword || !confirmPassword) {
    alert('Please fill in all fields.');
    event.preventDefault();
  } else if (newPassword !== confirmPassword) {
    alert('Passwords do not match.');
    event.preventDefault();
  }
});
Enter fullscreen mode Exit fullscreen mode

In this JavaScript code:

  • We use the addEventListener method to listen for form submission events (submit) on the login and signup forms.
  • Inside each event listener function, we get the values of input fields (username, password, newUsername, newPassword, confirmPassword) and perform validation checks.
  • If any validation condition fails, we display an alert message and prevent the form from being submitted (event.preventDefault()).

Responsive Design

Our CSS code already includes media queries to make the form responsive. However, you can further enhance responsiveness by testing your form on various devices and adjusting styles as needed. Consider using CSS frameworks like Bootstrap for a more robust and responsive design.

Conclusion

By following the steps outlined in this guide and using the provided HTML, CSS, and optional JavaScript code, you can create a responsive login/signup form for your website. Remember to test your form thoroughly, especially on different devices and screen sizes, to ensure a seamless user experience. Customizing the design and adding advanced functionality based on your website's requirements will further enhance the form's usability and appeal.

Top comments (0)