129

I want to style the caret of a focused <input type='text'/>. Specifically, the color and thickness.

Velda
  • 577
  • 2
  • 5
  • 20
Randomblue
  • 105,985
  • 138
  • 338
  • 537

7 Answers7

106

'Caret' is the word you are looking for. I do believe though, that it is part of the browsers design, and not within the grasp of css.

However, here is an interesting write up on simulating a caret change using Javascript and CSS http://www.dynamicdrive.com/forums/showthread.php?t=17450 It seems a bit hacky to me, but probably the only way to accomplish the task. The main point of the article is:

We will have a plain textarea somewhere in the screen out of the view of the viewer and when the user clicks on our "fake terminal" we will focus into the textarea and when the user starts typing we will simply append the data typed into the textarea to our "terminal" and that's that.

HERE is a demo in action


2018 update

There is a new css property caret-color which applies to the caret of an input or contenteditable area. The support is growing but not 100%, and this only affects color, not width or other types of appearance.

input{
  caret-color: rgb(0, 200, 0);
}
<input type="text"/>
Michael Jasper
  • 7,862
  • 4
  • 40
  • 58
  • 1
    This should be the accepted answer, works like a charm - Thanks :) – Sarah Sep 08 '20 at 06:30
  • is there any way to set a custom cursor on `input` elements or is the only solution a hackish workaround? – oldboy Oct 14 '20 at 08:47
  • 1
    There is also [`caret-shape`](https://www.w3.org/TR/2021/WD-css-ui-4-20210316/#caret-shape) (where we can set `auto | bar | block | underscore`) and the shorthand [`caret`](https://www.w3.org/TR/2021/WD-css-ui-4-20210316/#caret). Too bad that they are currently [supported by no one](https://css-tricks.com/almanac/properties/c/caret-shape/#browser-support) (no even on [Can I use](https://caniuse.com/css-caret-shape) at the time this is written) – BarryCap Jul 16 '21 at 15:46
76

If you are using a webkit browser you can change the color of the caret by following the next CSS snippet. I'm not sure if It's possible to change the format with CSS.

input,
textarea {
    font-size: 24px;
    padding: 10px;
    
    color: red;
    text-shadow: 0px 0px 0px #000;
    -webkit-text-fill-color: transparent;
}

input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder {
    color:
    text-shadow: none;
    -webkit-text-fill-color: initial;
}

Here is an example: http://jsfiddle.net/8k1k0awb/

Nestor Britez
  • 1,218
  • 10
  • 14
  • 4
    A clever hack, but emoji don't work as they just become filled in by the shadow – simbolo Jun 30 '16 at 13:28
  • Hmm, shades isn't needed for effect in my case (desktop Chrome and FF) – just edit text-fill-color of then input. – Velda Sep 18 '17 at 13:50
39

In CSS3, there is now a native way to do this, without any of the hacks suggested in the existing answers: the caret-color property.

There are a lot of things you can do to with the caret, as seen below. It can even be animated.

/* Keyword value */
caret-color: auto;
color: transparent;
color: currentColor;

/* <color> values */
caret-color: red;
caret-color: #5729e9;
caret-color: rgb(0, 200, 0);
caret-color: hsla(228, 4%, 24%, 0.8);

The caret-color property is supported from Firefox 55, and Chrome 60. Support is also available in the Safari Technical Preview and in Opera (but not yet in Edge). You can view the current support tables here.

Toastrackenigma
  • 6,303
  • 4
  • 39
  • 52
Sacha Nacar
  • 2,258
  • 17
  • 14
  • 1
    Beat me to the punch. Of course, this property will only work in Firefox, at least for now, and doesn't release until April 2017. See [this](https://developer.mozilla.org/en-US/Firefox/Releases/53#Changes_for_Web_developers) page. – SpyderScript Feb 25 '17 at 22:23
6

Here are some vendors you might me looking for

::-webkit-input-placeholder {color: tomato}
::-moz-placeholder          {color: tomato;} /* Firefox 19+ */
:-moz-placeholder           {color: tomato;} /* Firefox 18- */
:-ms-input-placeholder      {color: tomato;}

You can also style different states, such as focus

:focus::-webkit-input-placeholder {color: transparent}
:focus::-moz-placeholder          {color: transparent}
:focus:-moz-placeholder           {color: transparent}
:focus:-ms-input-placeholder      {color: transparent}

You can also do certain transitions on it, like

::-VENDOR-input-placeholder       {text-indent: 0px;   transition: text-indent 0.3s ease;}
:focus::-VENDOR-input-placeholder  {text-indent: 500px; transition: text-indent 0.3s ease;}
amn
  • 7,742
  • 8
  • 54
  • 82
Sergey Khmelevskoy
  • 2,312
  • 3
  • 18
  • 40
4

It is enough to use color property alongside with -webkit-text-fill-color this way:

    input {
        color: red; /* color of caret */
        -webkit-text-fill-color: black; /* color of text */
    }
<input type="text"/>

Works in WebKit browsers (but not in iOS Safari, where is still used system color for caret) and also in Firefox.

The -webkit-text-fill-color CSS property specifies the fill color of characters of text. If this property is not set, the value of the color property is used. MDN

So this means we set text color with text-fill-color and caret color with standard color property. In unsupported browser, caret and text will have same color – color of the caret.

Velda
  • 577
  • 2
  • 5
  • 20
1

input{
  caret-color: rgb(0, 200, 0);
}
<input type="text"/>
GtiClicker
  • 78
  • 8
0

it is simple. Just add the caret-color attribute like this:

* {cursor: url(http://labs.semplice.com/wp-content/uploads/2020/02/custom-cursor-arrow.png) 20 20, auto;}
input, div {padding: 20px;}
<input style="caret-color: rgb(0, 200, 0);">

<br>
<textarea style="caret-color: blue;"></textarea>

<br>
<div contenteditable style="padding: 5px; border: 1px solid black;border-color: black;caret-color: red"></div>