Show / Hide Password HTML CSS JavaScript with Source Code

Language

In this comprehensive guide, I will provide you with source codes that demonstrate how to create a show password input field. The source code is composed of HTML, CSS, and JavaScript, making it easy to integrate into your web projects.

What is the Purpose of a Show/Hide Password Feature?

The Show/Hide Password feature is a common functionality in application forms, such as Login and Registration Forms. Password fields are typically masked, meaning the characters entered are replaced with dots or asterisks for security reasons. This feature allows users to toggle the visibility of their entered password, displaying it as plain text, similar to regular input fields. This can be particularly helpful in ensuring that users have typed their passwords correctly, reducing the likelihood of login errors due to mistyped characters.

HTML

The following script is an HTML code that contains the elements of a simple login form. The source code filename is known as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <title> Login Form with Password Show Hide </title>
  4.     <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;500;600&display=swap" rel="stylesheet">
  5.     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
  6.        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
  7.        crossorigin="anonymous" referrerpolicy="no-referrer" />
  8.     <link rel="stylesheet" href="style.css">
  9. </head>
  10.  
  11.     <div class="form-container">
  12.         <form>
  13.             <h3> Login </h3>
  14.             <label for="username">Username</label>
  15.             <input type="text" name="username" id="username" placeholder="Enter email or phone">
  16.  
  17.             <label for="password">Password</label>
  18.             <div class="pass">
  19.                 <input type="password" class="pass" name="password" placeholder="Enter assword" id="password">
  20.                 <i id="show-hide" class="fa-solid fa-eye-slash"></i>
  21.             </div>
  22.             <button type="submit">Log In</button>
  23.             <hr>
  24.             <div class="social-container">
  25.                 <h3> Login using </h3>
  26.                 <div class="social">
  27.                     <div><a  class="go" href="#"><i class="fab fa-google"></i> Google </a></div>
  28.                     <div><a class="fb" href="#"><i class="fab fa-facebook"></i> Facebook</a></div>
  29.                 </div>
  30.             </div>
  31.         </form>
  32.     </div>
  33.  
  34. </body>
  35.     <script src="script.js"></script>
  36. </html>

CSS

Here's the CSS (Cascading Stylesheet) file script for the login form. This file script contains the code for the design of the elements in the form. The script is known as style.css and is loaded in the index file.

  1. * {
  2.     padding: 0;
  3.     margin: 0;
  4.     box-sizing: border-box;
  5.     font-family: 'Poppins', sans-serif;
  6.     }
  7.    
  8.     body {
  9.     background-color: #a499f6;
  10.     }
  11.    
  12.     .form-container {
  13.     width: 100%;
  14.     display: flex;
  15.     padding: 50px 20px;
  16.     justify-items: center;
  17.     }
  18.    
  19.     form {
  20.     display: block;
  21.     margin: auto;
  22.     width: 400px;
  23.     background-color: rgba(255, 255, 255, 0.13);
  24.     border-radius: 10px;
  25.     backdrop-filter: blur(10px);
  26.     border: 2px solid rgba(255, 255, 255, 0.1);
  27.     box-shadow: 0 0 40px rgba(8, 7, 16, 0.6);
  28.     padding: 50px 35px;
  29.     }
  30.    
  31.     form h3 {
  32.     font-size: 25px;
  33.     text-align: center;
  34.     color: #fff;
  35.     text-shadow: 2px 2px 10px black;
  36.     }
  37.    
  38.     label {
  39.     display: block;
  40.     margin-top: 2rem;
  41.     font-size: 1rem;
  42.     }
  43.    
  44.     input {
  45.     outline: none;
  46.     border: none;
  47.     width: 100%;
  48.     background-color: #f4f3f367;
  49.     border-radius: 4px;
  50.     padding: 15px 10px;
  51.     margin-top: 8px;
  52.     font-weight: 100;
  53.     }
  54.    
  55.     input::placeholder {
  56.     color: rgb(87, 79, 79);
  57.     }
  58.    
  59.     .pass {
  60.     position: relative;
  61.     }
  62.    
  63.     .pass i {
  64.     position: absolute;
  65.     top: 50%;
  66.     transform: translateY(-50%);
  67.     cursor: pointer;
  68.     right: 20px;
  69.     }
  70.    
  71.     #show-hide {
  72.     font-size: 1rem;
  73.     }
  74.    
  75.     button {
  76.     margin-top: 30px;
  77.     width: 100%;
  78.     background-color: #ffffff;
  79.     padding: 15px 0;
  80.     font-size: 18px;
  81.     border-radius: 5px;
  82.     cursor: pointer;
  83.     border: none;
  84.     transition: all 0.1s;
  85.     }
  86.    
  87.     button:hover {
  88.     transform: translate(-0.25rem, -0.25rem);
  89.     box-shadow: 0.25rem 0.25rem #2a2a2c;
  90.     }
  91.    
  92.     hr {
  93.     margin-top: 20px;
  94.     height: 1px;
  95.     background-color: #ffffff7f;
  96.     border: none;
  97.     outline: navajowhite;
  98.     }
  99.    
  100.     .social-container h3 {
  101.     margin-top: 20px;
  102.     margin-bottom: 20px;
  103.     }
  104.    
  105.     .social {
  106.     display: flex;
  107.     justify-content: center;
  108.     width: 100%;
  109.     }
  110.    
  111.     .social div {
  112.     width: 150px;
  113.     border-radius: 5px;
  114.     padding: 10px 5px;
  115.     background-color: rgba(255, 255, 255, 0.27);
  116.     text-align: center;
  117.     margin: 0 14px;
  118.     cursor: pointer;
  119.     }
  120.    
  121.     .social div:hover {
  122.     background-color: rgba(248, 247, 247, 0.511);
  123.     }
  124.    
  125.     .social a {
  126.     text-decoration: none;
  127.     }
  128.    
  129.     .go {
  130.     color: #e60000;
  131.     }
  132.    
  133.     .fb {
  134.     color: blue;
  135.     }
  136.    
  137.     .social i {
  138.     margin-right: 4px;
  139.     }
  140.    
  141.     @media screen and (max-width: 462px) {
  142.     form {
  143.         width: 100%;
  144.     }
  145.     }

JavaScript

Lastly, here's the JS file script containing the code that makes the password field masked and unmasked. The file is known as script.js and is also loaded in the index file.

  1. const showHide = document.getElementById('show-hide');
  2. let passwordInput = document.getElementById('password');
  3.  
  4. showHide.addEventListener('click', function () {
  5.     showHide.classList.toggle('show');
  6.  
  7.     if (showHide.classList.contains('show')) {
  8.         showHide.classList.remove('fa-eye-slash');
  9.         showHide.classList.add('fa-eye');
  10.         passwordInput.setAttribute('type', 'text');
  11.     }
  12.     else {
  13.         showHide.classList.add('fa-eye-slash');
  14.         showHide.classList.remove('fa-eye');
  15.         passwordInput.setAttribute('type', 'password');
  16.     }
  17. });

In the provided JavaScript code snippet:

  • showHide gets the element with the id 'show-hide'.
  • passwordInput gets the element with the id 'password'.
  • The purpose of the click event listener for showHide is to trigger the password field to unmask or mask the entered characters.

Snapshots

Here are some images of the output of the provided source code.

Masked/Hidden Password

Show Password usinh JavaScript

Unmasked/Hidden Password

Show Password usinh JavaScript

There you have it! I hope this Show/Hide Password Source Code using HTML, CSS, and JavaScript.

Explore more on this website for more Tutorials, Free Source Codes, and Articles covering various programming languages.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment