Alghany Jagad
Alghany Jagad

Reputation: 259

How to insert icon inside input tag

I want to put icon inside the input tag but its always in the bottom of input. i used bootstrap actually but for this i used custom style

here's the html:

             <div class="form-group">
                  <label for="password" class="text-secondary">Password</label>
                  <div class="input-group">
                    <input id="password" type="password" class="form-login @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">
                        <div class="input-group-append">
                        <span class="input-group-text" onclick="password_show_hide();">
                          <i class="fas fa-eye" id="show_eye"></i>
                          <i class="fas fa-eye-slash d-none" id="hide_eye"></i>
                        </span>
                      </div>
                    @error('password')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                  </div>
                </div>

here's the css:

.form-login{
    display: block;
    width:100%;
    font-size: 1rem;
    font-weight: 400;
    line-height:1.5;
    border-color: #009DA9 !important;
    border-style: solid !important;
    border-width: 0 0 1px 0 !important;
    padding: 0px !important;
    color:black;
    height: auto;
    border-radius: 0;
    background-color: #fff;
    background-clip: padding-box;
}


.form-login:focus{

    color: #495057;
    background-color: #fff;
    border-color: #fff;
    outline: 0;
    box-shadow: none;
}

what it's look like right now:

what i want to look like:before

enter image description here

Upvotes: 0

Views: 5583

Answers (2)

user13898463
user13898463

Reputation:

You can use position absolute to position the icon over the input field. Check to code I posted below:

  .form-group {
    width: 50%;
    position: relative;
  }

  .form-group i {
    position: absolute;
    right: 0px;
  }

  .icon {
    padding: 10px 0px;
    color: grey;
    min-width: 5px;
    text-align: center;
  }

  .input-field {
    width: 100%;
    padding: 10px;
    border-radius: 10px;
    border-color: solid lightgray;
  }
<div class="form-group">
  <label for="passowrd">Password</label>
  <div class="input-set">
    <i class="fas fa-lock icon">icon</i>
    <input id="password" name="password" class="input-field" type="text" placeholder="password" />
  </div>
</div>

Upvotes: 1

Mihai T
Mihai T

Reputation: 17687

Well you can use position absolute to position the icon over the input.

You might need to change the top value a little bit and the padding-right value. Check the comment inside the snippet below

.input-group {
  position:relative;
  display:inline-block;
}
.input-group-text {
  position: absolute;
  right:0;
  top: 0;
}

input {
  padding-right: 20px; /* = icon width if you don't want the password to go under the icon */ 
  }
<div class="input-group">
   <input id="password" type="password" class="form-login" />
   <div class="input-group-append">
     <span class="input-group-text">
       <i class="fas fa-eye" id="show_eye">icon</i>

     </span>
   </div>

 </div>

Upvotes: 1

Related Questions