How to include Bootstrap Glyphicon inside the "<input></input>" tag?

Asked

Viewed 22,549 times

7

With the following code:

<a href="#" class="btn btn-primary">
  <span class="glyphicon glyphicon-pencil"></span> Editar
</a>

I can create buttons like these:

inserir a descrição da imagem aqui

But I can’t seem to do input:

<input type="submit" class="btn btn-primary" value="Salvar">
  <span class="glyphicon glyphicon-floppy-disk"></span>
</input>

2 answers

15


  • Although the other answer is more complete, this was the one that best served me.

10

To get buttons with icon, keeping the input tag:

A very simple solution is to use the label and hide the submit original:

<label for="salvar" class="btn"><i class="glyphicon glyphicon-floppy-disk"></i>Salvar</label>
<input id="salvar" name="salvar" type="submit" value="Salvar" class="hidden">

Explanation: When you define a label for=, the click label is propagated to the corresponding input.


To place icons in text fields:

In this case, we "surround" the input with a div, by placing the icon superimposed on the input, and adding a padding to the left of the field:

HTML:

<div class="iconInput">
   <i class="glyphicon glyphicon-file"></i>
   <input type="text" class="form-control" placeholder="Nome do arquivo" />
</div>

CSS:

.iconInput { position: relative }
.iconInput input { padding-left: 28px }
.iconInput i { position: absolute; padding: 9px 10px }

Important the position: relative in div which encompasses the icon, for the absolute icon function properly.

See example on JS Fiddle

Browser other questions tagged

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