Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the caret with css [duplicate]

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

like image 727
Randomblue Avatar asked Sep 07 '11 19:09

Randomblue


People also ask

How do you change caret size in CSS?

If caps is ok in your context, one easy hack is to use the css attribute font-variant: small-caps; to get the caret larger than the text.

How do you style a caret in CSS?

The caret-shape property in CSS changes the shape of the text cursor inside editable elements that indicates a user is typing. It's part of the CSS Basic User Interfaces Module Level 4, which is currently in Working Draft status. As I write, the caret is the little blinking bar that follows each character I type.

Can I change cursor color CSS?

And the default color of this cursor for most of the browsers is black. But now you can change the cursor color to any color that you want. To change the cursor color in CSS we use the caret-color property and this property can be applied to the inputs and text areas both.


2 Answers

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/

like image 95
Nestor Britez Avatar answered Sep 21 '22 17:09

Nestor Britez


'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"/>
like image 43
Michael Jasper Avatar answered Sep 21 '22 17:09

Michael Jasper