Toggle navigation
☰
Home
HTML
CSS
Scripting
Database
<!DOCTYPE html> <title>My Example</title> <style> .password-toggle-container { max-width: 400px; margin: 40px auto; font-family: sans-serif; } /* Container for the input and button */ .input-group-password { position: relative; } .input-group-password .form-control { width: 100%; padding: 12px; /* Add padding on the right to make space for the button */ padding-right: 50px; border: 1px solid #ccc; border-radius: 6px; font-size: 1rem; box-sizing: border-box; } .input-group-password .toggle-password-btn { position: absolute; top: 50%; right: 10px; transform: translateY(-50%); background: none; border: none; cursor: pointer; padding: 5px; color: #888; display: flex; /* Helps center the SVG */ } .input-group-password .toggle-password-btn:hover { color: #333; } .input-group-password .toggle-password-btn svg { width: 24px; height: 24px; } /* Initially hide the 'eye-off' icon */ .icon-eye-off { display: none; } </style> <div class="password-toggle-container"> <form action="#" method="post"> <label for="password-field">Enter Password</label> <div class="input-group-password"> <input type="password" id="password-field" name="password" class="form-control" required> <button type="button" class="toggle-password-btn" id="togglePassword" aria-label="Show password as plain text."> <!-- Eye On --> <svg class="icon-eye" viewBox="0 0 24 24" fill="currentColor"> <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"></path> </svg> <!-- Eye Off (initially hidden) --> <svg class="icon-eye-off" viewBox="0 0 24 24" fill="currentColor"> <path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.44-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z"></path> </svg> </button> </div> </form> </div> <script> const togglePasswordBtn = document.getElementById('togglePassword'); const passwordInput = document.getElementById('password-field'); const iconEye = document.querySelector('.icon-eye'); const iconEyeOff = document.querySelector('.icon-eye-off'); if (togglePasswordBtn && passwordInput) { togglePasswordBtn.addEventListener('click', () => { // Toggle the type attribute const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password'; passwordInput.setAttribute('type', type); // Toggle the icon visibility if (type === 'password') { iconEye.style.display = 'block'; iconEyeOff.style.display = 'none'; togglePasswordBtn.setAttribute('aria-label', 'Show password as plain text.'); } else { iconEye.style.display = 'none'; iconEyeOff.style.display = 'block'; togglePasswordBtn.setAttribute('aria-label', 'Hide password.'); } }); } </script>