最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Placing password visibility icon inside password field - Stack Overflow

matteradmin5PV0评论

I have this following code.

<div class="login-password">
              <label for="password" class="form-label"
                >Enter your password</label
              >
              <input
                id="loginPassword"
                type="password"
                class="form-control"
                placeholder="Enter your password"
              />
              <i
                class="bi bi-eye-slash"
                id="togglePassword"
                style="margin-left: -30px; cursor: pointer"
              ></i>
</div>

When I do this the password visibility icon is placed as in the image given below.

How do I place the visibility icon within password field.

I have this following code.

<div class="login-password">
              <label for="password" class="form-label"
                >Enter your password</label
              >
              <input
                id="loginPassword"
                type="password"
                class="form-control"
                placeholder="Enter your password"
              />
              <i
                class="bi bi-eye-slash"
                id="togglePassword"
                style="margin-left: -30px; cursor: pointer"
              ></i>
</div>

When I do this the password visibility icon is placed as in the image given below.

How do I place the visibility icon within password field.

Share Improve this question asked Nov 10, 2021 at 4:29 joekevinrayan96joekevinrayan96 64411 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Using marketwatch.'s login as an example, you would have to make the css property 'position' to "absolute" for the <i> tag. This will allow you to place it inside the input box. Perhaps something like this:

.bi-eye-slash {
    width: 25px;
    height: 25px;
    position: absolute;
    z-index: 200;
    top: 28px;
    right: 10px;
    cursor: pointer;
}

I found the answer to my own question. Well, we have to group the input and the icon is the way to do it, and below is the code.

.input-group-addon i {
    margin-left: -30px;
    cursor: pointer;
    z-index: 200;
    position: absolute;
    font-size: large;
    color: #6c757d;
  }
 <link
  rel="stylesheet"
  href="https://cdn.jsdelivr/npm/[email protected]/font/bootstrap-icons.css"
/>

<div class="login-password">
                  <label for="password" class="form-label"
                    >Enter your password</label
                  >
                  <div class="input-group">
                    <input
                      id="loginPassword"
                      type="password"
                      class="form-control"
                      placeholder="Enter your password"
                    />
                    <div class="input-group-addon">
                      <i class="bi bi-eye-slash" id="togglePassword"></i>
                    </div>
                  </div>
                </div>
Post a comment

comment list (0)

  1. No comments so far