Password Show/Hide Toggle Feature Using HTML, CSS, and JavaScript
In today’s modern web applications, improving user experience and accessibility is just as important as ensuring security. One small yet essential feature that helps bridge this gap is the "Password Show/Hide Toggle" functionality. This feature allows users to view their entered password by clicking a button, typically represented with a text label or eye icon. In this article, we will explore the importance of this feature, how to build it using HTML, CSS, and JavaScript, and best practices for implementing it in real-world applications.
Why Password Visibility Toggle is Important
Users often struggle to input passwords correctly, especially on mobile devices. Typos can lead to login failures or frustration during account creation. Providing the option to show or hide the password allows users to double-check their input, minimizing errors and improving user satisfaction.
Some key benefits of this feature include:- Reduces login failures
- Improves accessibility for users with visual impairments
- Enhances user confidence when entering long or complex passwords
- Helps mobile users type with fewer mistakes
How the Password Toggle Works
The functionality is simple: the password input field is initially set to type
Building the Password Toggle Feature
1. HTML Structure
We'll start with a basic form layout that includes a password field and a toggle button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Password Toggle Feature</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="password-container">
<label for="password">Password</label>
<input type="password" id="password" placeholder="Enter your password" />
<button type="button" id="togglePassword">Show</button>
</div>
<script src="script.js"></script>
</body>
</html>
2. CSS Styling
This CSS gives the input and button a clean, mobile-friendly appearance.
body {
font-family: Arial, sans-serif;
background: #f2f5f9;
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
margin: 0;
padding: 0;
}
.password-container {
background: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 350px;
box-sizing: border-box;
}
.password-container label {
display: block;
font-size: 16px;
margin-bottom: 8px;
}
.password-container input {
width: 100%;
padding: 10px;
font-size: 16px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
.password-container button {
width: 100%;
padding: 10px;
font-size: 16px;
background: #007BFF;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s ease;
}
.password-container button:hover {
background: #0056b3;
}
3. JavaScript Logic
This script toggles the password field between visible and hidden states.
const passwordInput = document.getElementById('password');
const toggleButton = document.getElementById('togglePassword');
toggleButton.addEventListener('click', function () {
const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
passwordInput.setAttribute('type', type);
this.textContent = type === 'password' ? 'Show' : 'Hide';
});
Accessibility Considerations
To make your toggle feature more accessible:- Use
aria-label to describe the toggle button. - Ensure keyboard accessibility with
tabindex . - Provide screen reader announcements using ARIA live regions if needed.
Security Tips
- Keep the default state of the password field hidden (type="password").
- Only show passwords on user action, never by default.
- Do not log or store plain text passwords on the frontend.
Mobile Optimization
To ensure the toggle feature is mobile-friendly:- Use responsive styling with relative units (%, rem, etc.).
- Use
meta viewport for proper scaling. - Ensure buttons are large enough to tap easily on small screens.
Demo
Please click the button below to view the demo.