Custom Text Caret Styles

Bar, block, and underscore carets for text fields, including a caret you position yourself when caret-color is not enough.


Custom Text Caret Styles


Native inputs already accept caret-color. A block or underscore caret needs the native caret hidden and a span parked at the end of the value. This is the caret inside a field, not a mouse cursor. For the pointer, see label cursor and cursor effects.

Bar caret

.caret-bar {
  caret-color: #d4552a;
  font: 16px/1.4 ui-monospace, monospace;
  padding: 0.7rem 0.8rem;
}

Block and underscore

Hide the native caret and draw your own after the text. This version tracks the end of the value, which is the usual case for a single-line composer.

<label class="caret-field">
  <input class="caret-input" id="composer" type="text" value="Draft the hover" />
  <span class="caret-block" id="block" aria-hidden="true"></span>
</label>
.caret-field { position: relative; display: inline-block; }
.caret-input { caret-color: transparent; font: 16px/1.4 ui-monospace, monospace; padding: 0.7rem 0.8rem; }
.caret-block {
  position: absolute;
  top: 0.7rem;
  width: 0.6em;
  height: 1.15em;
  background: #d4552a;
  animation: caret-blink 1s steps(1) infinite;
  pointer-events: none;
}
.caret-under {
  height: 2px;
  top: auto;
  bottom: 0.65rem;
  width: 0.7em;
}
@keyframes caret-blink { 50% { opacity: 0; } }
const input = document.getElementById("composer");
const caret = document.getElementById("block");
const mirror = document.createElement("span");
mirror.style.cssText = "position:absolute;visibility:hidden;white-space:pre;font:16px/1.4 ui-monospace,monospace;";
document.body.append(mirror);

function place() {
  mirror.textContent = input.value;
  caret.style.left = `${input.offsetLeft + 13 + mirror.offsetWidth}px`;
}
input.addEventListener("input", place);
place();

Keep the native caret on any field where the user can click into the middle of the string, unless you also measure the selection with a mirror. caret-color is the right tool for that case.