DEV Community

Mbianou Bradon
Mbianou Bradon

Posted on

How to Build a Responsive Password Field Component using TailwindCSS

Design Inspired from [icodethis](icodethi.com) platform

In our everyday life, we turn to sign up for different programs, on different websites and different platforms depending on our needs and the purpose of our signing up.

To ensure our security and prevent not authorized individuals from accessing our accounts or details, the platform put in place security measures. One of these is the use of a Password.

A Password, sometimes called a passcode, is secret data, typically a string of characters, usually used to confirm a user's identity. Simply put, A password is a secret word or phrase that must be used to gain admission to a place.

The logic is simple, "if you don't know the password you can't come in".

As you might have guessed, the component we are going to field, is that thing in which Passwords are inputted, hence the name, Password Fields.

Let's get started!

Demo of Password Field

Understanding the Task

It’s important to divide your work or design into different parts, I always do that, it helps me break it down into smaller components that I can easily build, then join them all together to form the bigger component.

if you are used to my post, you surely know I call it the “Divide and Conquer “ approach😉. But in this situation, our Component don't really need to be divided further. So we are going to build it all at once.

Structure of Code

The root code of almost all components is almost always the same, just that their structure differs as you build it up.

This is how my base looks like

<body>
<!-- First Layer -->
 <div>
<!-- Second Layer -->
  <div>
        <!-- 
            Password Field 
              HERE
        -->

  </div>
 </div>

</body>
Enter fullscreen mode Exit fullscreen mode

From our root structure, we can see that everything goes into the second layer. Extra layers are good. But in this situation, we won't really need it.

Password Field

As earlier mentioned, we will build everything at once, but rest assured, we will understand everything together, part by part.

<body class="bg-[#f6f7f8] flex justify-center items-center min-h-screen">
<!-- First Layer -->
    <div class="mx-3 space-y-6 text-[#132B50] bg-white w-full sm:max-w-md py-7 px-12 rounded-xl shadow-lg [&_*]:transition-all [&_*]:ease-linear [&_*]:duration-100">
                <!-- Header -->
 <div class="text-2xl my-5 font-semibold">
    <h2>Enter your password</h2>
 </div>
                 <!-- User Details -->
 <div class="flex items-center gap-3">
                 <!-- User Profile -->
  <div>
    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677841863722/ui0fi1r4b.png" alt="" class="h-14 rounded-full aspect-square object-cover object-center hover:scale-110 cursor-pointer ">
  </div>
                   <!-- User Name -->
  <div>
     <p class="text-sm text-slate-500">Business Account</p>
     <h2 class="font-semibold">Mbianou Bradon</h2>
  </div>
</div>
                  <!-- Password Section -->
 <div class="my-10">
  <div class="mb-3 font-semibold">
        <h2>Password</h2>
  </div>
  <div class="flex items-center gap-2">

  <div class="w-full flex items-center gap-2 border-4 border-[#132B50] rounded-xl px-2 pt-1">
                 <!--Lock icon -->
  <div class="text-2xl cursor-pointer">
    <iconify-icon icon="heroicons-outline:lock-closed"></iconify-icon>
  </div>
                  <!-- Password Input -->
  <input type="password" name="" id="" placeholder="Enter Password" class="border-0 focus:ring-0 focus:ring-offset-0 w-full">
 </div>
                 <!-- eye icon -->
  <div class="text-2xl cursor-pointer">
     <iconify-icon icon="quill:eye-closed"></iconify-icon>
   </div>
 </div>
</div>

<div class="flex items-center justify-between flex-wrap gap-y-4">
                 <!-- Switch Component -->
  <div class="flex items-center gap-1">
     <div>
  <input type="checkbox" class="sr-only switch peer" name="check" id="check">
  <label for="check" class="peer-checked:[&>div>div]:translate-x-[1.55rem] peer-checked:[&>*]:bg-slate-700">
   <div class="w-[3.5rem] bg-slate-300 rounded-full p-0.5 cursor-pointer border border-gray-400 transition duration-300">
   <div class="w-[1.5rem] h-[1.5rem] rounded-full border-gray-800 bg-white transition duration-500"></div>
    </div>
</label>
 </div>
    <div class="font-bold">
      <h2>Stay signed in</h2>
    </div>
 </div>
                     <!-- Continue button -->
 <div class="px-4 py-2 bg-[#132B50] text-white rounded-md border border-[#132B50] hover:text-[#132B50] hover:bg-white cursor-pointer">
     <h2>Continue</h2>
   </div>
</div>
                     <!-- Reset password button -->
 <div class="font-bold text-lg underline my-5 cursor-pointer hover:no-underline w-fit">
      <h2>Reset password</h2>
   </div>
</div>


    <script src="https://code.iconify.design/iconify-icon/1.0.2/iconify-icon.min.js"></script>
</body>
Enter fullscreen mode Exit fullscreen mode

As surprisingly it can be, that's all about it. Now, let's understand the beautiful code above.

  • Header

The header is just a basic one.

         <!-- Header -->
 <div class="text-2xl my-5 font-semibold">
    <h2>Enter your password</h2>
 </div>
Enter fullscreen mode Exit fullscreen mode

All we did is to give it a font-size of text-2xl, font-weight of font-semibold and a margin-block of my-5.

  • User Details
 <!-- User Details -->
 <div class="flex items-center gap-3">
                 <!-- User Profile -->
  <div>
    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1677841863722/ui0fi1r4b.png" alt="" class="h-14 rounded-full aspect-square object-cover object-center hover:scale-110 cursor-pointer ">
  </div>
                   <!-- User Name -->
  <div>
     <p class="text-sm text-slate-500">Business Account</p>
     <h2 class="font-semibold">Mbianou Bradon</h2>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

The user details container is a flexbox that consists of 2 parts, the user profile image and the user name + type of account.

The User profile picture has some basic styles, which has height of h-14, border-radius of rounded-full, and we added aspect-square object-cover object-center to make it responsive. hover:scale-110 cursor-pointer are simply effect on hover

  • Password Section
 <!-- Password Section -->
 <div class="my-10">
  <div class="mb-3 font-semibold">
        <h2>Password</h2>
  </div>
  <div class="flex items-center gap-2">

  <div class="w-full flex items-center gap-2 border-4 border-[#132B50] rounded-xl px-2 pt-1">
                 <!--Lock icon -->
  <div class="text-2xl cursor-pointer">
    <iconify-icon icon="heroicons-outline:lock-closed"></iconify-icon>
  </div>
                  <!-- Password Input -->
  <input type="password" name="" id="" placeholder="Enter Password" class="border-0 focus:ring-0 focus:ring-offset-0 w-full">
 </div>
                 <!-- eye icon -->
  <div class="text-2xl cursor-pointer">
     <iconify-icon icon="quill:eye-closed"></iconify-icon>
   </div>
 </div>
</div>
Enter fullscreen mode Exit fullscreen mode

For the password section, we essentially have a small header, we can almost call it a label with some basic styling. Margin-block-end of mb-3 and font-weight of font-semibold

Lock icons and eye icons can be gotten on iconify. But you can as well use mine.

As for the password input, nothing extra, we just remove the default input styling, that's we removed the outline, ring and border on focus.

  • Switch Component.

For this component, I personally invite you to check on my article on HOW TO BUILD A RESPONSIVE SWITCH COMPONENT WITH TAILWINDCSS

  • Continue and Reset buttons
 <!-- Continue button -->
 <div class="px-4 py-2 bg-[#132B50] text-white rounded-md border border-[#132B50] hover:text-[#132B50] hover:bg-white cursor-pointer">
     <h2>Continue</h2>
   </div>
</div>
                     <!-- Reset password button -->
 <div class="font-bold text-lg underline my-5 cursor-pointer hover:no-underline w-fit">
      <h2>Reset password</h2>
   </div>
</div>

Enter fullscreen mode Exit fullscreen mode

For both buttons, simply styling properties were assigned.

for Continue button, a padding-inline of px-4, padding-block of py, and background-color of bg-[#132B50] which changes to bg-white on hover

while for the reset button, we added underline, which changes to no-underline on hover, and added a margin-block of my-5 for spacing vertically.

I think that's pretty much it. A quick, yet very useful tutorial

End Result

Conclusion

We just built a fully responsive Password Field component. Isn’t it amazing? We did all of this without any stress and without leaving our HTML file.

Right Now, you should be proud of yourself, trust me.

You can have a Live preview on Codepen or find the code on GitHub

Don’t hesitate to share with me if you were able to complete the tutorial on your end, I’d be happy to see any additional component and styling you added to your Password Field Component.

If you have any worries or suggestions, don’t hesitate to bring them up! 😊

See ya! 👋

Top comments (0)