如何将字体真棒图标插入文本输入?

时间:2015-06-16 02:48:13

标签: html css css3 font-awesome webfonts

如何将日历网络字体图标插入输入字段?

enter image description here

HTML:

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

CSS:

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

1 个答案:

答案 0 :(得分:12)

<input>是一个自动结束标记,您cannot中包含任何: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>