Show/Hide Password Toggle

Password input with an eye button: the classic tailwind show hide password pattern.

Tailwind CSS
<div class="relative w-full max-w-xs">
  <input type="password" placeholder="Enter password" class="w-full px-4 py-2.5 pr-12 rounded-xl border border-slate-300 bg-white text-slate-900 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-transparent" />
  <button type="button" aria-label="Show password" class="absolute right-2 top-1/2 -translate-y-1/2 p-1.5 rounded-lg text-slate-400 hover:text-slate-600 hover:bg-slate-200 transition-colors">
    <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
      <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
      <path stroke-linecap="round" stroke-linejoin="round" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
    </svg>
  </button>
</div>

Free to use in personal and commercial projects. No attribution required.

Variants

Same component, different looks. Preview each one live and copy the code you like.

Dark

For dark forms and login pages with deep backgrounds.

Tailwind CSS

Labeled

Adds a label and hint text for accessible standalone forms.

Must be at least 8 characters.

Tailwind CSS

Underline

Borderless underline style for minimal and material style forms.

Tailwind CSS

About this show/hide password toggle

The tailwind show hide password pattern appears on nearly every login and signup form, because letting users check their typing cuts down on failed logins and reset requests. A password visibility toggle is a small eye button pinned inside the right edge of the input; clicking it flips the input between type password and type text.

The layout is a relative wrapper with the input padded on the right (pr-12) so text never slides under the button. The button is absolutely positioned, vertically centered with top-1/2 and -translate-y-1/2, and gets a subtle hover background so it reads as tappable. The eye SVG is the standard outline icon, and swapping it for an eye-off icon on toggle gives clear feedback.

Keep the button inside the input boundary on all screen sizes, and never let it overlap the typed text. Use type button so it never submits the form by accident. For accessibility, update the aria-label between 'Show password' and 'Hide password' as the state changes, so screen reader users always know what the button will do.

Typing a password blind is a reliable way to fail a login. On mobile especially, typos hide behind dots, and the user only discovers the mistake after submitting. That failure costs a password reset flow, a support ticket, or an abandoned account. The eye toggle converts all of that into a one-second glance: check, fix, submit.

The input gets pr-12 of right padding so typed text never slides under the icon. The button is absolutely positioned with top-1/2 and -translate-y-1/2, which centers it vertically no matter how tall the input gets across breakpoints. Clicking flips the input type between password and text and swaps the eye icon for an eye-off icon. Using type button on the toggle keeps it from submitting the form by accident.

Re-hide the password automatically after a few seconds of inactivity; a revealed password left visible on a shared screen is a shoulder-surfing invitation. Add a Caps Lock warning next to the toggle, since case mistakes are the top reason logins fail and users blame the password. On the confirm-password field, mirror the visibility state from the first field rather than adding a second toggle; two toggles double the confusion. Never persist the visible state across page loads; always default back to hidden.

A sneaky bug: in some frameworks, swapping the input type triggers a re-render that wipes the field. The user taps the eye to check their typing and their password vanishes, which is the opposite of helpful. Keep the input controlled, or preserve the value explicitly across the type change. Test the toggle with a filled field before shipping, not just on an empty one.

Where to use it

  • ✓ Login forms
  • ✓ Signup and registration forms
  • ✓ Password reset pages
  • ✓ Account settings password change

Customization tips

Change the focus ring color to your brand. Swap the eye SVG for an eye-off variant when the password is visible. If your inputs are larger, increase pr-12 to pr-14 so long passwords never touch the icon.

Design notes

When to use

The show and hide password toggle belongs on every login, signup, and reset form, because letting people check their typing cuts failed logins and password reset requests. It is most valuable on mobile, where typos are frequent and invisible. Place the eye button inside the field so the association is obvious.

What to avoid

Do not default the field to visible, since shoulder surfing is a real risk on shared screens. Avoid putting the toggle on PIN or one-time code fields where visibility adds risk without benefit. And do not make the hit area tiny, because a 16 pixel eye icon frustrates the exact mobile users who need it most.

Accessibility

Expose the toggle as a button with aria-pressed and a clear label like Show password, and announce the state change to screen readers. Keep focus in the input when toggling so keyboard users are not thrown out of the field.

Frequently asked questions

How do I build a tailwind show hide password toggle?

Wrap the input in a relative div, add pr-12 to the input, and absolutely position a button with the eye icon on the right. A one-line click handler switches the input type between 'password' and 'text' and swaps the icon.

Can this work with pure CSS and no JavaScript?

Not reliably. Switching an input's type attribute requires JavaScript. The layout and styling are pure Tailwind, but the toggle behavior needs a tiny script, which is why most implementations pair this snippet with a few lines of JS.

Is the toggle button accessible?

Yes, as long as it is a real button element with an aria-label that updates with the state, and it is reachable by keyboard. Avoid using a div with a click handler, which screen readers and keyboard users cannot activate.

How do I switch an input between password and text with JavaScript?

On button click, set input.type to "text" when it is "password" and back again, then swap the eye and eye-off SVGs. Keep focus in the field after toggling so the user can keep typing without interruption. This is a two-line function, no library needed.

Should the eye icon show the current state or the action it will take?

Show the action. An eye icon means "show the password" and an eye-slash means "hide it", matching what happens on click. Match the aria-label to the same action, like aria-label="Show password". Showing the current state instead confuses users about what the button will do.

Does showing the password weaken security?

Only while it is visible on screen, since the value is transmitted exactly the same way either way. The risk is shoulder surfing in shared spaces, so some sites add a brief warning. Browsers still offer to save the password normally regardless of visibility state.

Customize it with our tools