如何向文本类型输入框添加象形文字?例如,我想在用户名输入中有'icon-user',就像这样:


当前回答

用Bootstrap测试4。

获取一个表单控件,并将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>

其他回答

如果你正在使用Fontawesome,你可以这样做:

<input type="text" style="font-family:Arial, FontAwesome"  placeholder="&#xf007;" />

结果

unicode的完整列表可以在The complete Font Awesome 4.6.3图标参考中找到

您可以使用它的Unicode HTML

所以要添加用户图标,只需添加&#xe008;到占位符属性,或任何您想要的位置。

你可以看看这张小抄。

例子:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <input type="text" class="form-control" placeholder="&#xe008;" 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">

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

这里有一个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;
}

您应该能够使用现有的引导类和一些自定义样式来实现这一点。

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

编辑图标是通过图标用户类引用的。这个答案是在Bootstrap版本2的时候写的。你可以在下面的页面上看到参考:http://getbootstrap.com/2.3.2/base-css.html#images

这里有另一种方法,通过在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规则都用于居中和添加适当的间距。

结果: