Icone in input with bootstrap

Asked

Viewed 4,653 times

1

How do I add an icon from bootstrap in my input in this way:

inserir a descrição da imagem aqui

4 answers

4

You can use the class input-group-addon to do as you wish.

See the low example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />


<div class="form-group">
  <div class="input-group">
    <span class="input-group-addon">
      <i class="glyphicon glyphicon-calendar"></i>
      </span>
    <input class="form-control left-border-none" type="text" placeholder="Ida" />
  </div>
</div>


<form class="form-inline">
  <div class="form-group">
    <label class="sr-only" for="exampleInputAmount">Amount (in dollars)</label>
    <div class="input-group">
      <div class="input-group-addon">$</div>
      <input type="text" class="form-control" id="exampleInputAmount" placeholder="Amount">
      <div class="input-group-addon">.00</div>
    </div>
  </div>
</form>

View documentation.

3


You can also make the icon look inside the input positioning it on it and adding a padding-left in the input, example:

.left-inner-addon {
    position: relative;
}
.left-inner-addon i {
    position: absolute;
    padding: 10px 12px;
    pointer-events: none;
    color: #666;
}
.left-inner-addon input.form-control {
    padding-left: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
  <div class="col-xs-6">
    <div class="form-group left-inner-addon">
      <i class="glyphicon glyphicon-calendar"></i>
      <input type="text" class="form-control" placeholder="Ida">
    </div>
  </div>
</div>

1

Use the classes input-group and input-group-btn as in the example:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-xs-6">
  <div class="input-group">
    <span class="input-group-btn">
      <button type="button" class="btn btn-default">
        <i class="glyphicon glyphicon-calendar"></i>
      </button>
    </span>
    <input type="text" class="form-control" />
  </div>
</div>

Reference: bootstrap documentation (in English)

  • 2

    Just remembering that the element button has, by default, tabindex equal to 0 and enters the navigation list by keyboard. In this case, as it will be merely decorative, it is interesting to define tabindex="-1" so that the focus is passed directly to the input. Further reading: How and where tabindex works?. Use semantically input-group-addon, as specified in the other answer, makes more sense.

0

You can put a div as the example below.

<div class="input-group-addon">@</div>
  • 4

    Please collaborate with the quality of community content by better elaborating your response. Very short answers will hardly be clear enough to add anything to the discussion. If your response is based on some function or feature of the language/tool, link to the official documentation or reliable material that supports your response. Searching for similar questions in the community can also be interesting to indicate other approaches to the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.