Bootstrap 4 Font-awesome input icons

2019-07-23 22:37发布

问题:

I'm trying to figure out how to add font-awesome icons to my input fields. I found a design that I really like and I need to fit icons in the input field. This is what it looks like:

But my form looks like this, And I can't really figure out how to add them in. I looked for numerous answers but none of them worked out for me as always I hope I can find the answer here.

Anyhow, This is the code for the form.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous">
<script src="https://use.fontawesome.com/f4737361cc.js"></script>

<form class="" method="POST" action="">
  <div class="row">
    <div class="col-md-12">
      <div class="form-group">
        <i class="fa fa-user-o"></i>
        <input id="email" type="email" class="form-control custom-input" name="email" value="" placeholder="Email adress" required autofocus style="border-radius: 30px;">
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-12">
      <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
    
         <i class="fa fa-lock"></i>                        
        <input id="password" type="password" class="form-control custom-input" name="password" placeholder="Wachtwoord" required style="border-radius: 30px;">
     
      </div>
    </div>
  </div>

 
</form>

Thanks in advance

回答1:

you can set the position:absulote to .fa then add position:relative to .form-control

form i.fa {
    position: absolute;
    top: 6px;
    left: 20px;
    color: blue;
    font-size: 22px;
    z-index: 9999;
}

.form-control {
    position: relative;
   padding-left: 45px !important;

}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous">
<script src="https://use.fontawesome.com/f4737361cc.js"></script>

<form class="" method="POST" action="">
  <div class="row">
    <div class="col-md-12">
      <div class="form-group">
        <i class="fa fa-user-o"></i>
        <input id="email" type="email" class="form-control custom-input" name="email" value="" placeholder="Email adress" required autofocus style="border-radius: 30px;">
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-12">
      <div class="form-group {{ $errors->has('password') ? ' has-error' : '' }}">
    
         <i class="fa fa-lock"></i>                        
        <input id="password" type="password" class="form-control custom-input" name="password" placeholder="Wachtwoord" required style="border-radius: 30px;">
     
      </div>
    </div>
  </div>

 
</form>