如何向文本类型输入框添加象形文字?例如,我想在用户名输入中有'icon-user',就像这样:
当前回答
如果你足够幸运,只需要现代浏览器:尝试css转换翻译。这不需要包装器,并且可以自定义,以便您可以为输入[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
其他回答
它可以使用官方引导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时对齐,并关注标签问题。
您可以使用它的Unicode HTML
所以要添加用户图标,只需添加到占位符属性,或任何您想要的位置。
你可以看看这张小抄。
例子:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <input type="text" class="form-control" placeholder="" style="font-family: 'Glyphicons Halflings', Arial"> . <input type="text" class="form-control" value="value. " style="font-family: 'Glyphicons Halflings', Arial"> . <input type="submit" class="btn btn-primary" value="submit-button" style="font-family: 'Glyphicons Halflings', Arial">
方法,不要忘记将输入的字体设置为Glyphicon字体 下面的代码:font-family: 'Glyphicons Halflings', Arial,其中 Arial是输入中常规文本的字体。
对于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>
对于输入,我有这样的东西:
官方的方法。无需自定义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>
演示:http://jsfiddle.net/yajf3b7q
这个演示是基于Bootstrap文档中的一个例子。向下滚动到“可选图标”http://getbootstrap.com/css/#forms-control-validation
这里有另一种方法,通过在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规则都用于居中和添加适当的间距。
结果: