能否在占位符中使用Font Awesome图标?我读到HTML在占位符中不被允许,有没有解决办法?
placeholder="<i class='icon-search'></i>"
能否在占位符中使用Font Awesome图标?我读到HTML在占位符中不被允许,有没有解决办法?
placeholder="<i class='icon-search'></i>"
如果你正在使用 FontAwesome 4.7
,那么这应该就足够了:
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<input type="text" placeholder=" Search" style="font-family:Arial, FontAwesome" />
您可以在 Font Awesome cheatsheet 中找到十六进制代码列表。然而,在最新的 FontAwesome 5.0 中,这种方法不起作用(即使您使用与更新的 font-family
结合使用的 CSS 方法)。
content
规则中转义的Unicode字符。在less源代码中,它可以在variables.less中找到。挑战将是在输入不为空时交换字体。像这样结合jQuery使用。<form role="form">
<div class="form-group">
<input type="text" class="form-control empty" id="iconified" placeholder=""/>
</div>
</form>
使用这段 CSS:
input.empty {
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
}
这是(简单的)jQuery:
$('#iconified').on('keyup', function() {
var input = $(this);
if(input.val().length === 0) {
input.addClass('empty');
} else {
input.removeClass('empty');
}
});
font-family: Font Awesome\ 5 Free;
无法正常工作。我不得不使用font-family: FontAwesome;
。 - kanlukaszfont-family: 'Font Awesome 5 Pro', 'Montserrat';
应用于搜索框,效果非常好。 - Jeff W我用以下方法解决了这个问题:
在CSS中,我使用了以下代码来处理fontAwesome类:
.fontAwesome {
font-family: 'Helvetica', FontAwesome, sans-serif;
}
在HTML中,我已添加了< strong>fontawesome类和< strong>fontawesome图标代码在占位符内部:<input type="text" class="fontAwesome" name="emailAddress" placeholder=" insert email address ..." value="">
你可以在CodePen上查看。
input[type="text"]::placeholder { text-align: right; font-family: Roboto, 'Font Awesome\ 5 Pro', sans-serif; font-weight: 600; }
- ChrisB@Elli的回答可以在FontAwesome 5中使用,但需要使用正确的字体名称和该版本特定的CSS。例如,在使用FA5 Free时,如果我包含了all.css,则无法让它正常工作,但是如果我包含solid.css,则可以正常工作:
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/solid.css">
<input type="text" placeholder=" Search" style="font-family: Arial, 'Font Awesome 5 Free'" />
对于FA5 Pro,字体名称为“Font Awesome 5 Pro”
如果支持的话,您可以使用 ::input-placeholder
伪选择器与 ::before
结合使用。
请参见以下示例:
http://codepen.io/JonFabritius/pen/nHeJg
我之前在这方面做了一些工作,然后看到了这篇文章,从中修改了这些内容:
placeholder=""
。你可以在FontAwesome页面找到Unicode http://fontawesome.io/icons/。
但是,你必须确保在输入框中添加style="font-family: FontAwesome;"
。 input {
width:96%;
margin:5px 2%;
padding:0 8px;
border:1px solid #444;
border-radius: 14px;
background: #fff;
@include placeholder {
font-family: 'FontAwesome', $gotham;
}
}
如果你只是使用handlebars,就像之前提到的那样,你只需将html实体设置为占位符:
<input id="listFilter" placeholder="" type="text">
{{text-field
id="listFilter"
placeholder=listFilterPlaceholder
value=listFilter}}
在控制器上:
listFilter: null,
listFilterPlaceholder: "\uf002"
而且值绑定很好用!
我知道这个问题很老。但我没有看到像我以前使用的那样简单的答案。
你只需要将fas
类添加到输入框中,并在这种情况下放入一个有效的十六进制值,例如 Font-Awesome 的符号 
,如下所示:<input type="text" class="fas" placeholder="" />
您可以在官方网站上找到每个图标的 Unicode。
这是一个简单的例子,您不需要 CSS 或 JavaScript。
input {
padding: 5px;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<form role="form">
<div class="form-group">
<input type="text" class="fas" placeholder="" />
</div>
</form>
如果你在考虑使用Font Awesome 5
,以下是一些相关内容:
不要指定通用的“Font Awesome 5”字体族,需要具体指定你所使用的图标分支。例如,在这里我使用了“Brands”分支。
<input style="font-family:'Font Awesome 5 Brands' !important"
type="text" placeholder="">
fa-placeholder
类添加到输入文本中来实现此目的:
<input type="text" name="search" class="form-control" placeholder="" />
因此,在 CSS 中只需添加以下内容:
.fa-placholder {
font-family: "FontAwesome"; }
这对我很有效。.fa-placholder {
font-family: "FontAwesome", "Source Sans Pro"; }

这个值?我的意思是,你是怎么从icon-search
变成了
的? - Ігар Цімошкаid="iconified"
,只需添加class="fa"
,Font Awesome 将为你处理 CSS 样式。 - Pedro Hoehl Carvalho<input type="text" placeholder=" 搜索博客" style="font-family: FontAwesome, Arial; font-style: normal">
,否则会显示一个包含“fl”的符号。 - Guillaume Renoult