简体   繁体   English

在占位符中使用 Font Awesome 图标

[英]Use Font Awesome Icon in Placeholder

Is it possible to use Font Awesome Icon in a Placeholder?是否可以在占位符中使用 Font Awesome 图标? I read that HTML isn't allowed in a placeholder.我读到占位符中不允许使用 HTML。 Is there a workaround?有解决方法吗?

placeholder="<i class='icon-search'></i>"

If you're using FontAwesome 4.7 this should be enough:如果您使用的是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="&#xF002; Search" style="font-family:Arial, FontAwesome" />

A list of hex codes can be found in the Font Awesome cheatsheet .可以在Font Awesome 备忘单中找到十六进制代码列表。 However, in the lastest FontAwesome 5.0 this method does not work (even if you use the CSS approach combined with the updated font-family ).但是,在最新的 FontAwesome 5.0 中,此方法不起作用(即使您将 CSS 方法与更新的font-family结合使用)。

You can't add an icon and text because you can't apply a different font to part of a placeholder, however, if you are satisfied with just an icon then it can work.您无法添加图标和文本,因为您无法对占位符的一部分应用不同的字体,但是,如果您只对一个图标感到满意,那么它就可以工作。 The FontAwesome icons are just characters with a custom font (you can look at the FontAwesome Cheatsheet for the escaped Unicode character in the content rule. In the less source code it's found in variables.less The challenge would be to swap the fonts when the input is not empty. Combine it with jQuery like this . FontAwesome 图标只是带有自定义字体的字符(您可以查看content规则中转义 Unicode 字符的FontAwesome Cheatsheet 。在变量中找到的 less 源代码中。less挑战是在输入时交换字体不为空。像这样将它与 jQuery 结合起来。

<form role="form">
  <div class="form-group">
    <input type="text" class="form-control empty" id="iconified" placeholder="&#xF002;"/>
  </div>
</form>

With this CSS:使用这个 CSS:

input.empty {
    font-family: FontAwesome;
    font-style: normal;
    font-weight: normal;
    text-decoration: inherit;
}

And this (simple) jQuery而这个(简单的)jQuery

$('#iconified').on('keyup', function() {
    var input = $(this);
    if(input.val().length === 0) {
        input.addClass('empty');
    } else {
        input.removeClass('empty');
    }
});

The transition between fonts will not be smooth, however.但是,字体之间的过渡不会平滑。

I solved with this method:我用这个方法解决了:

In the CSS I used this code for the fontAwesome class :在 CSS 中,我将此代码用于fontAwesome 类

.fontAwesome {
  font-family: 'Helvetica', FontAwesome, sans-serif;
}

In the HTML I have added the fontawesome class and the fontawesome icon code inside the placeholder:在 HTML 中,我在占位符中添加了fontawesome 类fontawesome 图标代码

<input type="text" class="fontAwesome" name="emailAddress" placeholder="&#xf0e0;  insert email address ..." value="">

You can see in CodePen .您可以在CodePen 中看到。

@Elli's answer can work in FontAwesome 5, but it requires using the correct font name and using the specific CSS for the version you want. @Elli 的答案可以在 FontAwesome 5 中使用,但它需要使用正确的字体名称并使用您想要的版本的特定 CSS。 For example when using FA5 Free, I could not get it to work if I included the all.css, but it worked fine if I included the solid.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="&#xF002; Search" style="font-family: Arial, 'Font Awesome 5 Free'" />

For FA5 Pro the font name is 'Font Awesome 5 Pro'对于 FA5 Pro,字体名称是“Font Awesome 5 Pro”

Where supported, you can use the ::input-placeholder pseudoselector combined with ::before .如果支持,您可以将::input-placeholder伪选择器与::before结合使用。

See an example at:请参阅以下示例:

http://codepen.io/JonFabritius/pen/nHeJg http://codepen.io/JonFabritius/pen/nHeJg

I was just working on this and came across this article, from which I modified this stuff:我刚刚在做这个并遇到了这篇文章,我从中修改了这些东西:

http://davidwalsh.name/html5-placeholder-css http://davidwalsh.name/html5-placeholder-css

I am using Ember (version 1.7.1) and I needed to both bind the value of the input and have a placeholder that was a FontAwesome icon.我正在使用 Ember(版本 1.7.1),我需要绑定输入的值并有一个占位符,它是一个 FontAwesome 图标。 The only way to bind the value in Ember (that I know of) is to use the built in helper.在 Ember 中绑定值的唯一方法(我知道)是使用内置的助手。 But that causes the placeholder to be escaped, "&#xF002" just shows up just like that, text.但这会导致占位符被转义,“&#xF002”就像那样显示,文本。

If you are using Ember or not, you need to set the CSS of the input's placeholder to have a font-family of FontAwesome.无论您是否使用 Ember,您都需要将输入占位符的 CSS 设置为具有 FontAwesome 的字体系列。 This is SCSS (using Bourbon for the placeholder styling ):这是 SCSS(使用Bourbon 作为占位符样式):

    input {
      width:96%;
      margin:5px 2%;
      padding:0 8px;
      border:1px solid #444;
      border-radius: 14px;
      background: #fff;
      @include placeholder {
        font-family: 'FontAwesome', $gotham;
      }
    }

If you are just using handlebars, as has been mentioned before you can just set the html entity as the placeholder:如果您只是使用把手,如前所述,您可以将 html 实体设置为占位符:

<input id="listFilter" placeholder="&#xF002;" type="text">

If you are using Ember bind the placeholder to a controller property that has the unicode value.如果您使用 Ember,请将占位符绑定到具有 unicode 值的控制器属性。

in the template:在模板中:

{{text-field
  id="listFilter"
  placeholder=listFilterPlaceholder
  value=listFilter}}

on the controller:在控制器上:

listFilter: null,
listFilterPlaceholder: "\uf002"

And the value binding works fine!值绑定工作正常!

占位符示例

占位符gif

Use placeholder="&#xF002;"使用placeholder="&#xF002;" in your input.在您的输入中。 You can find unicode in FontAwesome page http://fontawesome.io/icons/ .您可以在 FontAwesome 页面http://fontawesome.io/icons/ 中找到 unicode。 But you have to make sure add style="font-family: FontAwesome;"但是你必须确保添加style="font-family: FontAwesome;" in your input.在您的输入中。

Anyone wondering about a Font Awesome 5 implementation:任何想知道Font Awesome 5实现的人:

Do not specify a general "Font Awesome 5" font family, you need to specifically end with the branch of icons you're working with.不要指定通用的“Font Awesome 5”字体系列,您需要特别以您正在使用的图标分支结束。 Here I am using the branch "Brands" for example.例如,我在这里使用分支“品牌”。

<input style="font-family:'Font Awesome 5 Brands' !important" 
       type="text" placeholder="&#xf167">

More detail Use Font Awesome (5) icon in input placeholder text更多细节在输入占位符文本中使用 Font Awesome (5) 图标

I know this question it is very old.我知道这个问题很老了。 But I didn't see any simple answer like I used to use.但我没有看到像以前那样简单的答案。

You just need to add the fas class to the input and put a valid hex in this case &#xf002 for Font-Awesome's glyph as here <input type="text" class="fas" placeholder="&#xf002" />您只需要将fas类添加到输入并在这种情况下为 Font-Awesome 的字形添加一个有效的十六进制&#xf002如下<input type="text" class="fas" placeholder="&#xf002" />

You can find the unicode of each glyph in the official web here .您可以在此处的官方网站中找到每个字形的 unicode。

This is a simple example you don't need css or javascript.这是一个不需要 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="&#xf002" /> </div> </form>

I do this by adding fa-placeholder class to input text:我通过添加fa-placeholder类来输入文本来做到这一点:

<input type="text" name="search" class="form-control" placeholder="&#xF002;" />

so, in css just add this:所以,在css中添加这个:

.fa-placholder { font-family: "FontAwesome"; }

It works well for me.它对我很有效。

Update:更新:

To change font while user type in your text input, just add your font after font awesome要在用户输入文本时更改字体,只需在 font awesome 后添加字体即可

.fa-placholder { font-family: "FontAwesome", "Source Sans Pro"; }

Ignoring the jQuery this can be done using ::placeholder of an input element.忽略 jQuery,这可以使用输入元素的::placeholder来完成。

<form role="form">
  <div class="form-group">
    <input type="text" class="form-control name" placeholder="&#xF002;"/>
  </div>
</form>

The css part css部分

input.name::placeholder{ font-family:fontAwesome; font-size:[size needed]; color:[placeholder color needed] }

input.name{ font-family:[font family you want to specify] }

THE BEST PART: You can have different font family for placeholder and text最好的部分:您可以为占位符和文本使用不同的字体系列

I've solved the problem a bit differently and it works with any FA icon through html code.我以不同的方式解决了这个问题,它可以通过 html 代码与任何 FA 图标一起使用。 Instead of all these difficulties with placeholder my solution is:而不是占位符的所有这些困难,我的解决方案是:

  1. To place an icon in the usual manner以通常的方式放置图标
HTML
<i class="fas fa-icon block__icon"></i>
<input type="text" name="name" class="block__input" placeholder="Some text">
CSS
.block__icon {
  position: absolute;
  margin: some-corrections;
}
.block__input {
  padding: some-corrections;
}
  1. Then adjust placeholder's text (it's personal for everyone, in my case an icon was just before the text)然后调整占位符的文本(对每个人来说都是个人的,在我的情况下,图标就在文本之前)
HTML
<!-- For example add some spaces in placeholder, to make focused cursor stay before an icon -->
...placeholder="    Some text"...
  1. Here is the problem that an icon is above the our input and blocks cursor to click so we should add one more line in our CSS这是一个图标在我们的输入上方并阻止光标点击的问题,所以我们应该在我们的 CSS 中再添加一行
CSS
.block__icon {
  position: absolute;
  margin: some-corrections;

  /* The new line */
  pointer-events: none;
}

  1. But an icon doesn't disappear together with placeholder so we need to fix it.但是图标不会与占位符一起消失,所以我们需要修复它。 And also this is the final version of my solution:这也是我的解决方案的最终版本
HTML
<i class="fas fa-icon block__icon"></i>
<input type="text" name="name" class="block__input" placeholder="    Some text">
CSS
.block__icon {
  position: absolute;
  z-index: 2; /* New line */
  margin: some-corrections;
}
.block__input {
  position: relative; /* New line */
  z-index: 2; /* New line */
  padding: some-corrections;
}
/* New */
.block__input:placeholder-shown {
    z-index: 1;
}

It's harder than I thought before, but I hope I've helped anyone with this.这比我以前想象的要难,但我希望我已经帮助过任何人。

Codepen: https://codepen.io/dzakh/pen/YzKqJvy代码笔: https ://codepen.io/dzakh/pen/YzKqJvy

There is some slight delay and jank as the font changes in the answer provided by Jason.由于 Jason 提供的答案中的字体发生变化,因此会有一些轻微的延迟和卡顿。 Using the "change" event instead of "keyup" resolves this issue.使用“change”事件而不是“keyup”可以解决这个问题。

$('#iconified').on('change', function() {
    var input = $(this);
    if(input.val().length === 0) {
        input.addClass('empty');
    } else {
        input.removeClass('empty');
    }
});

I added both text and icon together in a placeholder.我在占位符中同时添加了文本和图标。

placeholder="Edit &nbsp; &#xf040;"

CSS : CSS :

font-family: FontAwesome,'Merriweather Sans', sans-serif;

If you can / want to use Bootstrap the solution would be input-groups:如果您可以/想要使用 Bootstrap,解决方案将是输入组:

<div class="input-group">
 <div class="input-group-prepend">
  <span class="input-group-text"><i class="fa fa-search"></i></span>
  </div>
 <input type="text" class="form-control" placeholder="-">
</div>

Looks about like this: input with text-prepend and search symbol看起来像这样:输入带有文本前置和搜索符号

Teocci solution is as simple as it can be, thus, no need to add any CSS, just add class="fas" for Font Awesome 5, since it adds proper CSS font declaration to the element. Teocci解决方案尽可能简单,因此,无需添加任何 CSS,只需为 Font Awesome 5 添加 class="fas",因为它为元素添加了正确的 CSS 字体声明。

Here's an example for search box within Bootstrap navbar, with search icon added to the both input-group and placeholder (for the sake of demontration, of course, no one would use both at the same time).这是 Bootstrap 导航栏中搜索框的示例,搜索图标添加到输入组和占位符(当然,为了演示,没有人会同时使用两者)。 Image: https://i.imgur.com/v4kQJ77.png "> Code:图片:https://i.imgur.com/v4kQJ77.png > 代码:

<form class="form-inline my-2 my-lg-0">
    <div class="input-group mb-3">
        <div class="input-group-prepend">
            <span class="input-group-text"><i class="fas fa-search"></i></span>
        </div>
        <input type="text" class="form-control fas text-right" placeholder="&#xf002;" aria-label="Search string">
        <div class="input-group-append">
            <button class="btn btn-success input-group-text bg-success text-white border-0">Search</button>
        </div>
    </div>
</form>

Sometimes above all answer not woking, when you can use below trick有时最重要的是回答不工作,当您可以使用以下技巧时

 .form-group { position: relative; } input { padding-left: 1rem; } i { position: absolute; left: 0; top: 50%; transform: translateY(-50%); }
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"> <form role="form"> <div class="form-group"> <input type="text" class="form-control empty" id="iconified" placeholder="search"> <i class="fas fa-search"></i> </div> </form>

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<form role="form">
  <div class="form-group">
    <input type="text" class="form-control empty" id="iconified" placeholder="search">
    <i class="fas fa-search"></i>
  </div>
</form>

.form-group {
  position: relative;
}
i {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: 999;
}
input {
  padding-left: 1rem;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM