6

How can I insert a calendar web font icon into my input field?

enter image description here

HTML:

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

CSS:

.start_date:before {
  font-family: "FontAwesome";
  content: "\f073";
}
Stickers
  • 75,527
  • 23
  • 147
  • 186
CherylG
  • 1,032
  • 8
  • 23

1 Answers1

12

<input> is a self-closing tag, you cannot have any :before or :after pseudo element in it. You could wrap it into a <label> or <span> so and attach it there.

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

Here is an example of having the icon positioned inside the input field.

.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>
Stickers
  • 75,527
  • 23
  • 147
  • 186