How to create a Login Form with Labels in HTML and CSS

Login Form with Labels | Coding Zemigle

A login form is an essential component of many websites, allowing users to authenticate themselves before accessing restricted content or services. A well-designed login form improves user experience, ensures accessibility, and enhances security. In this tutorial, we will guide you through the process of building a simple yet functional login form using HTML and CSS.

A login form serves as a gateway to secure areas of a website. It helps in user authentication by ensuring only authorized users can access specific content. It also improves security by protecting user accounts with secure login mechanisms.

Additionally, a well-structured login form enhances user experience by providing a clear and intuitive way for users to sign in. Ensuring accessibility is another key aspect, making sure all users, including those with disabilities, can interact with the form effectively.

Key Elements of a Login Form

To create a login form, we need a few essential elements. The username or email field allows users to enter their credentials, while the password field provides a masked input field for entering passwords securely. Labels are crucial as they clearly indicate what each input field is for.

Advertisement

The submit button enables users to send their credentials for verification. Optional elements like a “Remember Me” checkbox allow users to stay logged in, and a “Forgot Password” link provides a way to recover lost credentials.

Step 1: Structuring the Login Form with HTML

We start by setting up the HTML structure of our login form. A well-structured form ensures readability and accessibility. It is important to use the <form> element to wrap the input fields and submit button.

✲    index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="style.css">
  <title>Login Form | Coding Zemigle</title>
</head>
<body>
  <form action="/login" method="post">
    <div class="container">
      <h2>Login Form</h2>
      <label for="Email address">Email address</label>
      <input type="email" name="email" id="email" required>
      <label for="Password">Password</label>
      <input type="password" name="password" id="password" required>
      <button type="submit">LOGIN</button>
      <div class="register-link">Don't have an account › <a href="#">Registration</a></div>
    </div>
  </form>
</body>
</html>

Each input field should have a <label> element linked using the for attribute, which improves accessibility. To maintain styling consistency, grouping form elements inside a <div> with a class is a good practice. Using semantic HTML elements ensures clean and accessible code.

Advertisement

Step 2: Styling the Login Form with CSS

Once the HTML structure is in place, we enhance the form’s appearance using CSS. Styling makes the form visually appealing and improves user interaction. A centered layout can be achieved using display: flex, justify-content: center, and align-items: center to position the form properly on the page. To create a card-like appearance, adding a background color, padding, border radius, and box shadow enhances the design.

✲    style.css
body{
  font-family: "Roboto", sans-serif;
  background: #225560;
  display: flex;
  justify-items: center;
  align-items: center;
  padding: 15px 20px;
  height: 100%;
  margin: 0;
}

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

.container{
  background: #FFF;
  border-radius: 10px;
  padding: 10px 25px 40px 25px;
  width: 345px;
  position: absolute;
  top: 31%; /* adjust accordingly */
  left: 50%;
  transform: translate(-50%, -50%);
}

h2{
  text-align: center;
  font-size: 30px;
  margin-bottom: 40px;
}

label{
  display: block;
  margin-bottom: 10px;
}

input{
  padding: 15px;
  margin-bottom: 35px;
  font-size: 15px;
  width: 90%;
}

button{
  background: #225560;
  width: 100%;
  padding: 17px;
  border: none;
  margin-top: 10px;
  border-radius: 3px;
  font-size: 18px;
  color: #FFF;
  font-weight: 600;
  border: none;
  cursor: pointer;
}

.register-link{
  text-align: center;
  margin-top: 30px;
}

.register-link a{
  color: royalblue;
  text-decoration-line: none;
}

Typography should be consistent, using a clear font with appropriate sizes for readability. Proper spacing should be ensured between form fields and labels. The submit button should stand out by using distinct colors and hover effects for interactivity. Finally, the design should be responsive to ensure the form adapts well to different screen sizes.

Accessibility Considerations

To make the login form accessible to all users, labels should be associated with input fields using the for attribute. Sufficient color contrast should be maintained for readability. Larger clickable areas should be provided for mobile users, ensuring ease of interaction. A logical tab order should be used for seamless keyboard navigation, making the form usable for everyone.

Advertisement

Enhancing User Experience

A well-designed login form enhances user experience by following some important principles. Keeping the form simple is essential, avoiding unnecessary fields. Typically, a login form requires only an email or username and a password. Using clear labels helps users understand what each input field is for; for example, “Email Address” is more descriptive than just “Email”.

Highlighting active input fields with CSS focus styles improves visibility and guides users as they enter data. The submit button should be prominent, with a distinct color that makes it easily identifiable. Optimizing the form for mobile devices is crucial, ensuring input fields are large enough for touch interactions and that the form scales properly on smaller screens.

Creating a login form with labels using HTML and CSS is a fundamental skill for web developers. By structuring the form correctly and applying effective styling, you can create a user-friendly and visually appealing authentication component. To further enhance the form, consider adding additional styling, JavaScript validation, and integrating it with a backend for user authentication.

Keep experimenting and improving your web development skills. Happy coding!