How to place help icon right next to text box form in Bootstrap?

Viewed 171

I have mocked up form bootstrap with help text icon shown as this:

enter image description here

However, I have hard time trying to put help icon next to my text field, my trying result as below

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

  <title>Hello, world!</title>
</head>

<body>

  <form>
    <div class="form-group row">
      <label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm text-right">First Name (English)</label>
      <div class="col-sm-5">
        <input type="email" class="form-control form-control-sm" id="colFormLabelSm">
        <i class="fa fa-question-circle" aria-hidden="true"></i>

      </div>
    </div>
  </form>



  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
</body>

</html>

Is there any way I can align my help icon as shown in my mock up properly in bootstrap form? Thanks

1 Answers

First add a custom selector to the div-container wrapping your input and your icon (class 'info-input'):

<div class="col-sm-5 info-input">
    <input type="email" class="form-control form-control-sm" id="colFormLabelSm">
    <i class="fa fa-question-circle" aria-hidden="true"></i>
</div>

Next we use flex to align input and icon in a centered row:

.info-input {
    display: flex;
    align-items: center;
}

To add some spacing between input and icon you can add some margin to the icon:

.info-input .fa {
    margin-left: 10px;
}
Related