[转]Add Bootstrap Glyphicon to Input Box

本文转自:http://stackoverflow.com/questions/18838964/add-bootstrap-glyphicon-to-input-box

How can I add a glyphicon to a text type input box? For example I want to have ‘icon-user‘ in a username input, something like this:


576down voteaccepted

Without Bootstrap:

We‘ll get to Bootstrap in a second, but here‘s the fundamental CSS concepts in play in order to do this yourself.  As beard of prey points out, you can do this with CSS by absolutely positioning the icon inside of the input element.  Then add padding to either side so the text doesn‘t overlap with the icon.

So for the following HTML:

<div class="inner-addon left-addon">
    <i class="glyphicon glyphicon-user"></i>
    <input type="text" class="form-control" />
</div>

You can use the following CSS to left and right align glyphs:

/* enable absolute positioning */
.inner-addon {
    position: relative;
}

/* style icon */
.inner-addon .glyphicon {
  position: absolute;
  padding: 10px;
  pointer-events: none;
}

/* align icon */
.left-addon .glyphicon  { left:  0px;}
.right-addon .glyphicon { right: 0px;}

/* add padding  */
.left-addon input  { padding-left:  30px; }
.right-addon input { padding-right: 30px; }

Demo in Plunker

Note: This presumes you‘re using glyphicons, but works equally well with font-awesome.   For FA, just replace .glyphicon with .fa


With Bootstrap:

As buffer points out, this can be accomplished natively within Bootstrap by using Validation States with Optional Icons.  This is done by giving the .form-group element the class of .has-feedback and the icon the class of .form-control-feedback.

The simplest example would be something like this:

<div class="form-group has-feedback">
    <label class="control-label">Username</label>
    <input type="text" class="form-control" placeholder="Username" />
    <i class="glyphicon glyphicon-user form-control-feedback"></i>
</div>

Pros:

  • Includes support for different form types (Basic, Horizontal, Inline)
  • Includes support for different control sizes (Default, Small, Large)

Cons:

  • Doesn‘t include support for left aligning icons

To overcome the cons, I put together this pull-request with changes to support left aligned icons.  As it is a relatively large change, it has been put off until a future release, but if you need these features today, here‘s a simple implementation guide:

If you‘re using less, just include these changes in your project. If you‘re not using less, grab the file and convert it to CSS with any online LESS Compiler.

Then, all you have to do is include the class .has-feedback-left on any group that has the class .has-feedback in order to left align the icon.

Since there are a lot of possible html configurations over different form types, different control sizes, different icon sets, and different label visibilities, I created a test page that shows the correct set of HTML for each permutation along with a live demo.

Here‘s a demo in Plunker

P.S. frizi‘s suggestion of adding pointer-events: none; has been added to bootstrap

Didn‘t find what you were looking for? Try these similar questions:

时间: 2024-12-21 17:32:00

[转]Add Bootstrap Glyphicon to Input Box的相关文章

实现bootstrap布局的input输入框中的图标点击功能

使用bootstrap布局可以在input的输入框中添加譬如登录名输入框中的一键清除图标和密码输入框中显示密码的小眼睛图标.如下图: 但是在将图标放入input输入框中,这些小图标是无法获得点击事件的: 那么问题来了,怎样让这些小图标能够获得点击事件呢? 我也不知道,但是可以用一种迂回的方式,来间接的实现该功能. 重点来了: 在小图标的位置上,添加一个看不见的元素,元素的大小正好将小图标覆盖,但是没有背景颜色和边框:相当于给小图标盖了一个透明的被子: <div class="input-g

bootstrap Glyphicon字体在firefox不显示【CORS问题】

最近使用bootstrap来试水一个手机版网站的项目,在用到里面的Glyphicon字体的时候,那些glyphicon-search 类套用上去之后无法在firefox上显示,显示为乱码,IE,Safari,chrome等等显示正常. 奇怪,一直刷新跟清缓存都无法解决这个问题.后来在firefox的控制面板上看到以下的信息 网上能看到有类似的同行也有这个经历 http://www.yanxiaoyu.com/show-23-19-1.html 在网上搜到上面的解决方法 http://enable

bootstrap glyphicon图标无法显示

如果不注意bootstrap引入css和fonts的规范,则可能会导致bootstrap 在显示glyphicon图标时无法正常显示,显示为方框. 此时可搜索bootstrap.css中的.glyphicon,可发现其大约上下10行左右有引入字体的URL,如下所示: 确保该目录存在即可. 通常出错是因为路径不正确所致.

bootstrap 表单 input group 输入框内带字符 加文字 符号

<div class="form-group" id='rule4' style='display:none;'> <div class="col-sm-12"> <div class="input-group"> <span class="input-group-addon">请设置正确答案:</span> <input name='key' type=&qu

Bootstrap系列 -- 14. 表单控件输入框input

每一个表单都是由表单控件组成.离开了控件,表单就失去了意义.接下来的我们简单的来了解Bootstrap框架中表单控件的相关知识. 单行输入框,常见的文本输入框,也就是input的type属性值为text.在Bootstrap中使用input时也必须添加type类型,如果没有指定type类型,将无法得到正确的样式,因为Bootstrap框架都是通过input[type=“?”](其中?号代表type类型,比如说text类型,对应的是input[type=“text”])的形式来定义样式的. <fo

Bootstrap 表单控件一(单行输入框input,下拉选择框select ,文本域textarea)

单行输入框,常见的文本输入框,也就是input的type属性值为text.在Bootstrap中使用input时也必须添加type类型,如果没有指定type类型,将无法得到正确的样式,因为Bootstrap框架都是通过input[type=“?”](其中?号代表type类型,比如说text类型,对应的是input[type=“text”])的形式来定义样式的. 为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”,如: <form role="form"

Bootstrap学习笔记(四)表单input

单行输入框,常见的文本输入框,也就是input的type属性值为text.在Bootstrap中使用input时也必须添加type类型,如果没有指定type类型,将无法得到正确的样式,因为Bootstrap框架都是通过input[type="?"](其中?号代表type类型,比如说text类型,对应的是input[type="text"])的形式来定义样式的. <form role="form"> <div class=&quo

BootStrap DataTables Spring MVC简单增删改查实例

1 <!DOCTYPE html> 2 <%@ page contentType="text/html;charset=gbk" language="java" %> 3 <%@page isELIgnored="false" %> 4 <meta name="viewport" content="width=device-width, initial-scale=1&quo

MVC中用jQuery加BootStrap实现动态增加删除文本输入框!

http://www.freejs.net/article_biaodan_278.html 这是在网上找到方法,我修改了一下实合我的项目,发博只为收藏记录并加深记忆. 修改后效果如下 @model PwtsWeb.Models.PhoneNum @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-w