如何向文本类型输入框添加象形文字?例如,我想在用户名输入中有'icon-user',就像这样:
当前回答
这里有另一种方法,通过在CSS中的伪元素之前使用:来放置象形文字。
工作演示在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。兼容基于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所说,我们必须添加指针事件:none;这样光标就不会干扰到输入焦点。所有其他CSS规则都用于居中和添加适当的间距。
结果:
其他回答
这个“欺骗”的副作用是,glyphicon类会改变输入控件的字体。
小提琴
<input class="form-control glyphicon" type="search" placeholder=""/>
如果你想摆脱副作用,你可以删除“glyphicon”类,并添加以下CSS(可能有更好的方法来设置占位符伪元素,我只在Chrome上测试过)。
小提琴
.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";
}
可能有一个更干净的解决方案:
小提琴
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=" search" />
<input class="form-control glyphicon" type="text" placeholder=" username" />
<input class="form-control glyphicon" type="password" placeholder=" password" />
这里有一个css的替代方案。我设置了一个搜索字段,以获得类似于Firefox(以及其他100个应用程序)的效果。
这是小提琴。
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中的类来完成。X版本,没有任何自定义css。
在输入标记之前使用input-group-addon,在input-group内部然后使用任何字形,下面是代码
<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>
这是输出
为了进一步自定义它,在你自己的custom.css文件中添加几行自定义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的背景透明,并使输入标签的左梯度为零,输入将有一个无缝的外观。下面是定制的输出
下面是一个jsbin的例子
这将解决自定义css与标签重叠的问题,在使用input-lg时对齐,并关注标签问题。
如果你正在使用Fontawesome,你可以这样做:
<input type="text" style="font-family:Arial, FontAwesome" placeholder="" />
结果
unicode的完整列表可以在The complete Font Awesome 4.6.3图标参考中找到
下面是我如何使用默认的引导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>
这是它的样子: