将Bootstrap Glyphicon添加到输入框

时间:2013-09-16 23:23:56

标签: css twitter-bootstrap glyphicons

如何将glyphicon添加到文本类型输入框?例如,我想在用户名输入中使用'icon-user',如下所示:

enter image description here

15 个答案:

答案 0 :(得分:727)

没有Bootstrap:

我们将在一秒钟内启动Bootstrap,但这里有基本的CSS概念,以便自己完成。作为beard of prey points out,您可以通过将输入元素内部的图标绝对定位来使用CSS来完成此操作。然后在任一侧添加填充,以使文本不与图标重叠。

所以对于以下HTML:

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

您可以使用以下CSS左右对齐字形:

/* 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

css screenshot

  

注意:这假设您使用的是glyphicons,但与font-awesome同样有效。
  对于FA,只需将.glyphicon替换为.fa

即可

使用Bootstrap:

作为buffer points out,可以使用Validation States with Optional Icons在Bootstrap中本地完成。这是通过为.form-group元素提供.has-feedback类,将图标设为.form-control-feedback类来完成的。

最简单的例子是这样的:

<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>

<强>赞成

  • 包括对不同表单类型(基本,水平,内联)的支持
  • 包括对不同控件尺寸(默认,小,大)的支持

缺点

  • 不包括对对齐图标
  • 的支持

为了克服缺点,我将这个pull-request与更改组合在一起,以支持左对齐的图标。由于这是一个相对较大的变化,它一直推迟到未来发布,但如果您需要这些功能今天,这里有一个简单的实施指南:

只需添加these form changes in css(也可以通过底部隐藏的堆栈代码内联)zh * LESS :或者,如果您是building via less,这是form changes in less

然后,您要做的就是在任何具有类.has-feedback-left的组中包含类.has-feedback,以便左对齐图标。

由于在不同的表单类型,不同的控件大小,不同的图标集和不同的标签可见性上有很多可能的html配置,我创建了一个测试页面,显示每个排列的正确HTML集以及现场演示

Here's a demo in Plunker

feedback screenshot

  添加pointer-events: none;

P.S。 frizi's suggestionadded to bootstrap

没找到你要找的东西?试试这些类似的问题:

为左对齐反馈图标添加CSS

.has-feedback .form-control {
  padding-right: 34px;
}
.has-feedback .form-control.input-sm,
.has-feedback.form-group-sm .form-control {
  padding-right: 30px;
}
.has-feedback .form-control.input-lg,
.has-feedback.form-group-lg .form-control {
  padding-right: 46px;
}
.has-feedback-left .form-control {
  padding-right: 12px;
  padding-left: 34px;
}
.has-feedback-left .form-control.input-sm,
.has-feedback-left.form-group-sm .form-control {
  padding-left: 30px;
}
.has-feedback-left .form-control.input-lg,
.has-feedback-left.form-group-lg .form-control {
  padding-left: 46px;
}
.has-feedback-left .form-control-feedback {
  left: 0;
}
.form-control-feedback {
  line-height: 34px !important;
}
.input-sm + .form-control-feedback,
.form-horizontal .form-group-sm .form-control-feedback {
  width: 30px;
  height: 30px;
  line-height: 30px !important;
}
.input-lg + .form-control-feedback,
.form-horizontal .form-group-lg .form-control-feedback {
  width: 46px;
  height: 46px;
  line-height: 46px !important;
}
.has-feedback label.sr-only ~ .form-control-feedback,
.has-feedback label.sr-only ~ div .form-control-feedback {
  top: 0;
}
@media (min-width: 768px) {
  .form-inline .inline-feedback {
    position: relative;
    display: inline-block;
  }
  .form-inline .has-feedback .form-control-feedback {
    top: 0;
  }
}
.form-horizontal .has-feedback-left .form-control-feedback {
  left: 15px;
}

答案 1 :(得分:61)

官方方法。无需自定义CSS:

<form class="form-inline" role="form">
  <div class="form-group has-success has-feedback">
    <label class="control-label" for="inputSuccess4"></label>
    <input type="text" class="form-control" id="inputSuccess4">
    <span class="glyphicon glyphicon-user form-control-feedback"></span>
  </div>
</form>

DEMO:http://jsfiddle.net/LS2Ek/1/

此演示基于Bootstrap文档中的示例。在此处向下滚动到“使用可选图标”http://getbootstrap.com/css/#forms-control-validation

enter image description here

答案 2 :(得分:42)

这是一个仅限CSS的替代方案。我为搜索字段设置了这个以获得类似于Firefox(和其他一百个应用程序)的效果。

Here's a fiddle

<强> HTML

<div class="col-md-4">
  <input class="form-control" type="search" />
  <span class="glyphicon glyphicon-search"></span>
</div>

<强> CSS

.form-control {
  padding-right: 30px;
}

.form-control + .glyphicon {
  position: absolute;
  right: 0;
  padding: 8px 27px;
}

答案 3 :(得分:11)

以下是我只使用默认引导程序CSS v3.3.1:

的方法
<div class="form-group">
    <label class="control-label">Start:</label>
    <div class="input-group">
        <input type="text" class="form-control" aria-describedby="start-date">
        <span class="input-group-addon" id="start-date"><span class="glyphicon glyphicon-calendar"></span></span>
    </div>
</div>

这就是它的样子:

enter image description here

答案 4 :(得分:8)

这个'作弊'将与glyphicon类将改变输入控件字体的副作用一起使用。

Fiddle

<input class="form-control glyphicon" type="search" placeholder="&#57347;"/>

如果你想摆脱副作用,你可以删除“glyphicon”类并添加以下CSS(可能有更好的方法来设置占位符伪元素的样式,我只在Chrome上测试过。) / p>

Fiddle

.form-control[type="search"]::-webkit-input-placeholder:first-letter {
    font-family:"Glyphicons Halflings";
}
.form-control[type="search"]:-moz-placeholder:first-letter {
    font-family:"Glyphicons Halflings";
}
.form-control[type="search"]::-moz-placeholder:first-letter {
    font-family:"Glyphicons Halflings";
}
.form-control[type="search"]:-ms-input-placeholder:first-letter {
    font-family:"Glyphicons Halflings";
}

可能是一个更清洁的解决方案:

Fiddle

<强> CSS

.form-control.glyphicon {
    font-family:inherit;
}
.form-control.glyphicon::-webkit-input-placeholder:first-letter {
    font-family:"Glyphicons Halflings";
}
.form-control.glyphicon:-moz-placeholder:first-letter {
    font-family:"Glyphicons Halflings";
}
.form-control.glyphicon::-moz-placeholder:first-letter {
    font-family:"Glyphicons Halflings";
}
.form-control.glyphicon:-ms-input-placeholder:first-letter {
    font-family:"Glyphicons Halflings";
}

<强> HTML

<input class="form-control glyphicon" type="search" placeholder="&#57347; search" />
<input class="form-control glyphicon" type="text"  placeholder="&#57352; username" />
<input class="form-control glyphicon" type="password"  placeholder="&#57395; password" />

答案 5 :(得分:7)

可以使用官方bootstrap 3.x版本中的类来完成,没有任何自定义css。

在输入标记之前使用input-group-addon,在input-group里面然后使用任何glyphicons,这里是代码

<form>
  <div class="form-group">
    <div class="col-xs-5">
      <div class="input-group">
          <span class="input-group-addon transparent"><span class="glyphicon glyphicon-user"></span></span>
          <input class="form-control left-border-none" placeholder="User Name" type="text" name="username">
      </div>
    </div>
  </div>
</form>

这是输出

enter image description here

要自定义它,请在您自己的custom.css文件中添加几行custuom css(如果需要,可以调整填充)

.transparent {
    background-color: transparent !important;
    box-shadow: inset 0px 1px 0 rgba(0,0,0,.075);
 }
 .left-border-none {
    border-left:none !important;
    box-shadow: inset 0px 1px 0 rgba(0,0,0,.075);
 }

通过使input-group-addon的背景透明并使输入标签的左侧渐变为零,输入将具有无缝外观。这是自定义输出

enter image description here

这是一个jsbin example

这将解决自定义css问题,在使用input-lg时重叠标签,对齐并关注制表符问题。

答案 6 :(得分:6)

这是一个非引导程序解决方案,它通过使用base64 URI编码将glyphicon的图像表示直接嵌入CSS中来保持标记简单。

input {
  border:solid 1px #ddd;
}
input.search {
	padding-left:20px;
	background-repeat: no-repeat;
	background-position-y: 1px;
	background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADbSURBVDhP5ZI9C4MwEIb7//+BEDgICA6C4OQgBJy6dRIEB6EgCNkEJ4e3iT2oHzH9wHbpAwfyJvfkJDnhYH4kHDVKlSAigSAQoCiBKjVGXvaxFXZnxBQYkSlBICII+22K4jM63rbHSthCSdsskVX9Y6KxR5XJSSpVy6GbpbBKp6aw0BzM0ShCe1iKihMXC6EuQtMQwukzPFu3fFd4+C+/cimUNxy6WQkNnmdzL3NYPfDmLVuhZf2wZYz80qDkKX1St3CXAfVMqq4cz3hTaGEpmctxDPmB0M/fCYEbAwZYyVKYcroAAAAASUVORK5CYII=);
}
<input class="search">

答案 7 :(得分:6)

如果您使用 Fontawesome ,则可以执行此操作:

attribute inspector

<强>结果

enter image description here

可以在The complete Font Awesome 4.6.3 icon reference

中找到完整的unicode列表

答案 8 :(得分:4)

这是通过在CSS中使用:before伪元素放置glyphicon的另一种方法。

Working demo in jsFiddle

对于这个HTML:

<form class="form form-horizontal col-xs-12">
    <div class="form-group">
        <div class="col-xs-7">
            <span class="usericon">
                <input class="form-control" id="name" placeholder="Username" />
            </span>
        </div>
    </div>
</form>

使用此CSS( Bootstrap 3.x和基于Webkit的浏览器兼容

.usericon input {
    padding-left:25px;
}
.usericon:before {
    height: 100%;
    width: 25px;
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    position: absolute;
    content: "\e008";
    font-family: 'Glyphicons Halflings';
    pointer-events: none;
}

正如@Frizi所说,我们必须添加pointer-events: none;,以便光标不会干扰输入焦点。所有其他CSS规则用于居中和添加适当的间距。

结果:

screenshot

答案 9 :(得分:4)

input {
  border:solid 1px #ddd;
}
input.search {
	padding-left:20px;
	background-repeat: no-repeat;
	background-position-y: 1px;
	background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADbSURBVDhP5ZI9C4MwEIb7//+BEDgICA6C4OQgBJy6dRIEB6EgCNkEJ4e3iT2oHzH9wHbpAwfyJvfkJDnhYH4kHDVKlSAigSAQoCiBKjVGXvaxFXZnxBQYkSlBICII+22K4jM63rbHSthCSdsskVX9Y6KxR5XJSSpVy6GbpbBKp6aw0BzM0ShCe1iKihMXC6EuQtMQwukzPFu3fFd4+C+/cimUNxy6WQkNnmdzL3NYPfDmLVuhZf2wZYz80qDkKX1St3CXAfVMqq4cz3hTaGEpmctxDPmB0M/fCYEbAwZYyVKYcroAAAAASUVORK5CYII=);
}
<input class="search">

答案 10 :(得分:1)

我对Bootstrap 3.3.5的这个案例也有一个决定:

<div class="col-sm-5">
    <label for="date">
       <input type="date" placeholder="Date" id="date" class="form-control">         
    </label>
    <i class="glyphicon glyphicon-calendar col-sm-pull-2"></i>
</div>

在输入时我有这样的事情:  enter image description here

答案 11 :(得分:1)

您可以使用其Unicode HTML

因此,要添加用户图标,只需将&#xe008;添加到placeholder属性或任何您想要的位置。

您可以查看enter image description here

示例:

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<input type="text" class="form-control" placeholder="&#xe008; placeholder..." style="font-family: 'Glyphicons Halflings', Arial">
<input type="text" class="form-control" value="&#xe008; value..." style="font-family: 'Glyphicons Halflings', Arial">
<input type="submit" class="btn btn-primary" value="&#xe008; submit-button" style="font-family: 'Glyphicons Halflings', Arial">
&#13;
&#13;
&#13;

  

不要忘记使用输入法将字体设置为Glyphicon字体   以下代码:font-family: 'Glyphicons Halflings', Arial,其中    Arial 是输入中常规文字的字体。

答案 12 :(得分:0)

您应该可以使用现有的引导类和一些自定义样式来执行此操作。

<form>
    <div class="input-prepend">
        <span class="add-on">
            <i class="icon-user"></i>
        </span>
        <input class="span2" id="prependedInput" type="text" placeholder="Username" style="background-color: #eeeeee;border-left: #eeeeee;">
    </div>         

修改该图标通过icon-user课程引用。这个答案是在Bootstrap第2版时编写的。您可以在以下页面上看到该引用:http://getbootstrap.com/2.3.2/base-css.html#images

答案 13 :(得分:0)

如果您有幸只需要现代浏览器:尝试使用css transform translate。这不需要包装器,并且可以自定义,以便您可以为输入[type = number]提供更多间距以容纳输入微调器,或将其移动到句柄的左侧。

    @import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css");


.is-invalid {
  height: 30px;
  box-sizing: border-box;
}

.is-invalid-x {
  font-size:27px;
  vertical-align:middle;
  color: red;
  top: initial;
  transform: translateX(-100%);
}




 <h1>Tasty Field Validation Icons using only css transform</h1>
    <label>I am just a poor boy nobody loves me</label>
    <input class="is-invalid"><span class="glyphicon glyphicon-exclamation-sign is-invalid-x"></span>

http://codepen.io/anon/pen/RPRmNq?editors=110

答案 14 :(得分:0)

经过Bootstrap 4测试。

接受一个form-control,并将is-valid添加到其类中。请注意控件如何变为绿色,但更重要的是,请注意控件右侧的对勾图标吗?这就是我们想要的!

示例:

.my-icon {
    padding-right: calc(1.5em + .75rem);
    background-image: url('https://use.fontawesome.com/releases/v5.8.2/svgs/regular/calendar-alt.svg');
    background-repeat: no-repeat;
    background-position: center right calc(.375em + .1875rem);
    background-size: calc(.75em + .375rem) calc(.75em + .375rem);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container">
  <div class="col-md-5">
	<input type="text" id="date" class="form-control my-icon" placeholder="Select...">
  </div>
</div>