Login Page Html Css Js

As a website developer, I recognize the significance of having a well-crafted and operational login page for every website or app. In this piece, I will guide you through the steps of designing a login page using HTML, CSS, and JavaScript, and also offer some of my own advice and perspectives throughout the process.

HTML Structure

Let’s start by creating the basic structure of our login page using HTML. We’ll need an HTML form element that will contain the input fields for the username and password, as well as the submit button.


<form id="login-form" action="#">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" required>
  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required>
  <input type="submit" value="Login">
</form>

In the code above, we have a form element with an id of “login-form” and an action attribute set to “#”. The action attribute specifies the URL to which the form data will be sent to process the login. Since we’re just focusing on the front-end here, we’ll leave the action attribute empty for now. Feel free to point it to your server-side code later on.

CSS Styling

Now that we have the HTML structure in place, let’s add some visual appeal to our login page using CSS. We’ll start by styling the form itself and then move on to the input fields and the submit button.


form {
  background-color: #f2f2f2;
  padding: 20px;
  width: 300px;
  margin: 0 auto;
  border-radius: 10px;
}

In the code above, we’re giving the form a light gray background color, adding some padding and margin to create some spacing, setting a width of 300 pixels, and applying a border-radius of 10 pixels to give it a rounded look.


input[type="text"], input[type="password"] {
  width: 100%;
  padding: 10px;
  margin-bottom: 10px;
  border-radius: 5px;
}

With the code above, we’re setting the width of the input fields to 100%, adding some padding and margin-bottom for spacing, and applying a border-radius of 5 pixels to give the inputs a slight curve.


input[type="submit"] {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

Lastly, we’re giving the submit button a green background color, white text color, some padding, no border, a border-radius of 5 pixels, and changing the cursor to a pointer on hover to indicate interactivity.

JavaScript Validation

No login page is complete without some form of client-side validation. JavaScript comes to the rescue here. Let’s add some basic validation to our login form to ensure that the user has entered both a username and a password.


const form = document.getElementById('login-form');
form.addEventListener('submit', function(event) {
  event.preventDefault();
  const username = document.getElementById('username').value;
  const password = document.getElementById('password').value;
  if (!username || !password) {
    alert('Please enter both a username and a password.');
  } else {
    alert('Login successful! Redirecting to the dashboard.');
    window.location.href = 'dashboard.html';
  }
});

In the code above, we’re first accessing the login form and adding an event listener to it for the ‘submit’ event. When the form is submitted, we prevent the default form submission behavior using the event.preventDefault() method.

We then retrieve the values of the username and password input fields, and check if either of them is empty. If either field is empty, we display an alert message asking the user to enter both a username and a password. If both fields have values, we display a success message and redirect the user to the dashboard page using the window.location.href property.

Conclusion

Congratulations! You have successfully created a login page using HTML, CSS, and JavaScript. By following these steps, you now have a simple yet functional login page that is visually appealing and includes basic form validation.

Remember, the login page is often the first point of contact for users, so it’s important to make a good impression. Feel free to customize the design, add more advanced validation, or integrate it with your back-end code to make it even more secure and powerful.

Happy coding!