Whenever the icon overlaps the input field, it gets hidden beneath the input tag. To resolve this issue, you should adjust the <i>
tag with a higher z-index value. You can try using z-index: 100
to ensure that it remains visible above the input even when clicked.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d6b6263796c7a687e626068206b7f68684d38233c382339">[email protected]</a>/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8cac7c7dcdbdcdac9d8e89d86998698">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="input-group mb-3">
<span class="input-group-text"><i class="fas fa-lock"></i></span>
<input class="form-control" id="password" name="password" placeholder="Password" value="">
<i class="far fa-eye" id="togglePassword"
style="cursor: pointer; margin-left: -30px; z-index: 100;"></i>
</div>
UPDATE: If you wish to keep the password and username fields equal in width, consider placing the icon within the input-group-text
component and removing the styles
margin-left: -30px; z-index: 100;
. This is because
input-group-text
covers the input field.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4d44455f4a5c4e5844464e064d594e4e6b1e051a1e051f">[email protected]</a>/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7c5c8c8d3d4d3d5c6d7e79289968997">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="input-group mb-3">
<span class="input-group-text"><i class="fas fa-lock"></i></span>
<input class="form-control" id="username" name="username" placeholder="Username" value="">
</div>
<div class="input-group mb-3">
<span class="input-group-text"><i class="fas fa-lock"></i></span>
<input class="form-control" id="password" name="password" placeholder="Password" value="">
<span class="input-group-text">
<i class="far fa-eye" id="togglePassword"
style="cursor: pointer"></i>
</span>
</div>
Update 2: Your JavaScript code hides the eye icon by utilizing this.classList.toggle('fa-eye')
. This method removes the 'fa-eye' class if present in the 'classList' of the 'togglePassword' element and adds it if not present.
If your intention is to toggle between showing and hiding password icons, such as the eye-slash
icon for text display, you can include,
this.classList.toggle('fa-eye-slash');
like so
const togglePassword = document.querySelector("#togglePassword");
const password = document.querySelector("#password");
togglePassword.addEventListener("click", function () {
// toggle the type attribute
const type = password.getAttribute("type") === "password" ? "text" : "password";
password.setAttribute("type", type);
// toggle the eye icon
this.classList.toggle('fa-eye');
this.classList.toggle('fa-eye-slash');
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84e2ebeaf0e5f3e1f7ebe9e1a9e2f6e1e1c4b1aab5b1aab0">[email protected]</a>/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5c7cacad1d6d1d7c4d5e5908b948b95">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="input-group mb-3">
<span class="input-group-text"><i class="fa fa-lock"></i></span>
<input class="form-control" id="username" name="username" placeholder="Username" value="">
</div>
<div class="input-group mb-3">
<span class="input-group-text"><i class="fa fa-lock"></i></span>
<input class="form-control" id="password" name="password" placeholder="Password" value="">
<span class="input-group-text">
<i class="fa fa-eye" id="togglePassword"
style="cursor: pointer"></i>
</span>
</div>