如何将Font Awesome图标插入文本输入框?

6
如何将日历网页字体图标插入到我的输入框中?

enter image description here

HTML:

<input class="start_date" type="text">

CSS:

.start_date:before {
  font-family: "FontAwesome";
  content: "\f073";
}
1个回答

12

<input> 是一个自闭合标签,你不能在其中使用任何:before:after伪元素。你可以将其包装在<label><span>中,并在那里进行附加。

.start_date:before {
  font-family: "FontAwesome";
  content: "\f073";
}

.start_date:before,
.start_date input {
  vertical-align: middle;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<label class="start_date">
  <input type="text" placeholder="Date">
</label>

这是一个将图标放置在输入框内部的示例。

.start_date {
  position: relative;
}

.start_date:before {
  font-family: "FontAwesome";
  font-size: 14px;
  content: "\f073";
  position: absolute;
  left: 4px;
  top: 50%;
  transform: translateY(-50%);
}

.start_date input {
  text-indent: 18px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<label class="start_date">
  <input type="text" placeholder="Date" />
</label>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接