Login Page with Password Eye Icon HTML CSS Javascript

Azeem Akhtar
4 min readMar 27, 2023

--

In today’s digital era, most websites and applications require users to log in before accessing their services. A login page is a fundamental component of any website or application that requires user authentication. Users typically input their email address, username, and password combination to access their accounts.

However, entering a password can be inconvenient and prone to typing errors. To simplify the process, developers have introduced an eye icon that toggles password visibility. This article will discuss implementing a login page with a password eye icon using HTML, CSS, and JavaScript.

HTML:

HTML, or Hypertext Markup Language, is the backbone of any web page. It is used to create the structure and content of a web page. To create a login page with a password eye icon, we must create an HTML form with a password input field and an eye icon. The password input field should have an ID that can be referenced in our JavaScript code.

CSS:

CSS, or Cascading Style Sheets, styles and lays out HTML pages. To add the password eye icon, we will create a CSS class that styles the icon and adds it to the password input field. We will create a class to change the icon’s appearance when the user clicks on it, which will toggle the password visibility.

JavaScript:

JavaScript is a programming language used to make web pages interactive. On our login page, we will use JavaScript to toggle the password visibility when the user clicks the eye icon. We will also use JavaScript to add an event listener to the icon that listens for a click event.

Implementation:

  1. Create an HTML form with a password input field and an eye icon.
  2. Style the password input field and the eye icon using CSS.
  3. Use JavaScript to toggle the password visibility when the user clicks the eye icon.
  4. Add an event listener to the eye icon that listens for a click event.

HTML Code:

<!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" />
<title>Login Page</title>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body>
<div class="container">
<div class="login-box">
<h2>Login</h2>
<form action="" method="post">
<div class="user-box">
<input type="text" name="" required="" />
<label>Username</label>
</div>
<div class="user-box">
<input type="password" name="" id="password" required="" />
<label>Password</label>
<span class="password-toggle-icon"><i class="fas fa-eye"></i></span>
</div>
<a href="#">
<span></span>
<span></span>
<span></span>
<span></span>
Submit
</a>
</form>
</div>
</div>
<script src="script.js"></script>
</body>
</html>

CSS Code:

@import url("https://fonts.googleapis.com/css?family=Poppins:400,500&display=swap");

* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}

body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f1f1f1;
}

.container {
width: 100%;
max-width: 400px;
padding: 20px;
}

.login-box {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.login-box h2 {
margin: 0 0 15px;
padding: 0;
color: #333;
text-align: center;
text-transform: uppercase;
}

.user-box {
position: relative;
margin-bottom: 30px;
}

.user-box input {
width: 100%;
padding: 10px 0;
font-size: 16px;
color: #333;
margin-bottom: 30px;
border: none;
border-bottom: 2px solid #333;
outline: none;
background: transparent;
}

.user-box label {
position: absolute;
top: 0;
left: 0;
padding: 10px 0;
font-size: 16px;
color: #333;
pointer-events: none;
transition: 0.5s;
}

.user-box input:focus ~ label,
.user-box input:valid ~ label {
transform: translateY(-20px);
font-size: 14px;
color: #333;
}

a {
display: inline-block;
background-color: #333;
color: #fff;
padding: 10px 20px;
font-size: 16px;
text-transform: uppercase;
text-decoration: none;
position: relative;
overflow: hidden;
transition: 0.5s;
letter-spacing: 2px;
border-radius: 5px;
}

a:hover {
background-color: #fff;
color: #333;
border: 1px solid #333;
}

a span {
position: absolute;
display: block;
}

a span:nth-child(1) {
top: 0;
left: -100%;
width: 100%;
height: 2px;
background-color: #333;
animation: animate1 1s linear infinite;
}

@keyframes animate1 {
0% {
left: -100%;
}
50%,
100% {
left: 100%;
}
}

a span:nth-child(2) {
top: -100%;
right: 0;
width: 2px;
height: 100%;
background-color: #333;
animation: animate2 1s linear infinite;
animation-delay: 0.25s;
}

@keyframes animate2 {
0% {
top: -100%;
}
50%,
100% {
top: 100%;
}
}

a span:nth-child(3) {
bottom: 0;
right: -100%;
width: 100%;
height: 2px;
background-color: #333;
animation: animate3 1s linear infinite;
animation-delay: 0.5s;
}

@keyframes animate3 {
0% {
right: -100%;
}
50%,
100% {
right: 100%;
}
}

a span:nth-child(4) {
bottom: -100%;
left: 0;
width: 2px;
height: 100%;
background-color: #333;
animation: animate4 1s linear infinite;
animation-delay: 0.75s;
}

@keyframes animate4 {
0% {
bottom: -100%;
}
50%,
100% {
bottom: 100%;
}
}

.password-toggle-icon {
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
cursor: pointer;
}

.password-toggle-icon i {
font-size: 18px;
line-height: 1;
color: #333;
transition: color 0.3s ease-in-out;
margin-bottom: 20px;
}

.password-toggle-icon i:hover {
color: #000;
}

Javascript Code:

const passwordField = document.getElementById("password");
const togglePassword = document.querySelector(".password-toggle-icon i");

togglePassword.addEventListener("click", function () {
if (passwordField.type === "password") {
passwordField.type = "text";
togglePassword.classList.remove("fa-eye");
togglePassword.classList.add("fa-eye-slash");
} else {
passwordField.type = "password";
togglePassword.classList.remove("fa-eye-slash");
togglePassword.classList.add("fa-eye");
}
});

In conclusion, a login page with a password eye icon can enhance the user experience by allowing them to toggle their password visibility easily. We can create an interactive login page that provides a seamless and convenient user experience using HTML, CSS, and JavaScript by following the implementation steps outlined in this article.

Download the Whole Source code on Github Repo

--

--

Azeem Akhtar
Azeem Akhtar

Written by Azeem Akhtar

Python, Machine Learning, Deep Learning, Data Science, Django, Artificial Intelligence

Responses (1)