62

I have a textfield is there a way to hide the blinking text cursor? I say this because I am doing a horror/mystery website and one of the clues is to start typing anywhere.

Maybe I can do it with javascript?

Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34
test
  • 17,706
  • 64
  • 171
  • 244
  • 2
    Have you tried setting `style='cursor: default'`? I'm confused what you're talking about with a 'blinking' text cursor, none of my cursors blink. – animuson Sep 08 '10 at 19:18
  • 2
    @animuson: that is for the mouse cursor that displays when the mouse is over the text input, whereas OP is asking about the blinking text cursor, aka the caret. – BoltClock Sep 08 '10 at 19:18
  • 2
    @BoltClock's a Unicorn: Ah, the text position. I believe that's called a caret though, not a cursor. – animuson Sep 08 '10 at 19:22
  • 1
    @animuson It'd be nice if everybody called it a caret to avoid confusion. But the distinction you made is not how it's used. I hear cursor maybe even more often than caret. From dictionary.com: `Computers. a movable, sometimes blinking, symbol that indicates the position on a CRT or other type of display where the next character entered from the keyboard will appear, or where user action is needed, as in the correction of an erroneous character already displayed.` – Ruan Mendes Mar 13 '14 at 18:39
  • 3
    X11 has a mouse pointer and text cursor. MS-Windows has a mouse cursor and a text caret. That's what's confusing. I prefer the X11 naming convention. It was also used by Apple and the Amiga. – Alexis Wilke Mar 26 '14 at 20:24
  • Have a look at this, you will get your solution. https://stackoverflow.com/questions/45738397/hide-text-field-blinking-cursor-in-ie-or-even-change-blinking-cursor-color-to-wh/51328027#51328027 – Shrirang Kadale Jul 13 '18 at 15:14

12 Answers12

63

The basic idea is, that the cursor's color is the same as the text's color. So the first thing you do is make the text transparent, thus taking the cursor away with it. Then you can make the text visible again with a text shadow.

Use this link to see it live in jsfiddle.

input[type="text"]{
    color : transparent;
    text-shadow : 0 0 0 #000;
}
input[type="text"]:focus{
    outline : none;
}

Update:

Known to not work in iOS 8 and IE 11


Another idea of my is a bit more hacky and requires javascript.

HTML and CSS part:

You make 2 input fields and position one exactly on top of the another with z-index, etc. Then you make the top input field completely transparent, no focus, no color, and alike. You need to set the visible, lower input to disabled, so that it only shows the content of the above input, but not actually works.

Javascript part:

After all the above you sync the two inputs. On keypress or on change you copy the contents of the higher input to the lower.

Summing all the above: you type in an invisible input, and that will be sent to the backend when the form submitted, but every update of the text in it will be echoed into the lower visible, but disabled input field.

Lajos Mészáros
  • 3,756
  • 2
  • 20
  • 26
  • Thanks for the info, nice to know, that it's not a general solution. There is no good solution for hiding a cursor in an input field, because it is a rear thing to do. No native CSS, or JS code is implemented so far, that intentionally does that. All we can do is make hacks that seems like it hides the cursor, but don't expect it to work everywhere. – Lajos Mészáros Jun 11 '14 at 12:26
44

caret-color: transparent !important; works in newer browsers

cdeutsch
  • 3,847
  • 27
  • 25
21

I was looking for a way to hide the blinking cursor on iOS devices for date inputs that trigger a calendar, because you could see the cursor blinking on top of the calendar picker.

input:focus { text-indent: -9999em; }

So in this case my CSS works nicely, obviously the downside is that if you need to see what you are typing then it is not good

Rémi Becheras
  • 14,902
  • 14
  • 51
  • 81
Slavo
  • 341
  • 2
  • 5
  • 2
    Can you expand on your answer at all? – bwegs Mar 19 '15 at 14:15
  • 1
    Sorry, i forgot to add: i was looking for a way to hide the blinking cursor on iOS devices for date inputs that trigger a calendar, because you could see the cursor blinking on top of the calendar picker. so i this case my CSS i works nicely, obviously the downside is that if you need to see what you are typing then it is not good. – Slavo Mar 20 '15 at 00:50
  • 3
    You should include the explanation in the answer, not in a comment, @Slavo. Please, [edit] your answer. – Palec Mar 20 '15 at 10:48
  • Just added it for him. – Rémi Becheras Aug 05 '15 at 12:31
  • I like this answer and it lead me to a better solution: `input:focus { line-height: 0; }` – gdibble Jul 26 '16 at 23:21
  • Another oddity: this solution works in IE, but not in Edge - the caret remains at the start position of the input. Change the text-indent value in Edge in the developer tools, and it disappears. – ccprog Oct 03 '18 at 17:43
21

Try this:

$(document).ready(
  function() {
    $("textarea").addClass("-real-textarea");
    $(".textarea-wrapper").append("<textarea class=\"hidden\"></textarea>");
    $(".textarea-wrapper textarea.hidden").keyup(
      function() {
        $(".textarea-wrapper textarea.-real-textarea").val($(this).val());
      }
    );
    $(".textarea-wrapper textarea.-real-textarea").focus(
      function() {
        $(this).parent().find("textarea.hidden").focus();
      }
    );
  }
);
.textarea-wrapper {
  position: relative;
}

.textarea-wrapper textarea {
  background-color: white;
}

.textarea-wrapper,
.textarea-wrapper textarea {
  width: 500px;
  height: 500px;
}

.textarea-wrapper textarea.hidden {
  color: white;
  opacity: 0.00;
  filter: alpha(opacity=00);
  position: absolute;
  top: 0px;
  left: 0px;
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<div class="textarea-wrapper">
  <textarea></textarea>
</div>

The idea is to create a second, invisible <textarea> over/on-top-of the real one. The user is typing in the invisible one but the text doesn't appear (nor the caret/cursor) as it is invisible! You then use JavaScript to assign its value to the visible one.

But it doesn't seem to work in IE8 :'( the caret is still visible even though the opacity is cranked up to 11.

But it works in Firefox... ?

j08691
  • 204,283
  • 31
  • 260
  • 272
Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
  • 1
    If you play with background colors and opacity you can get it to kind of work in IE... but then the background has to be gray as opposed to white :( – Richard JP Le Guen Sep 11 '10 at 23:36
  • I had similar problem, but I needed only the `focus`/`blur` events, so I used a simple checkbox :) – pesho hristov Mar 27 '17 at 10:02
  • Why don't you just use the onKeyPress event to assign the value to the textarea? – Nonny Moose Jul 05 '17 at 00:03
  • Instead of this, you should write `pointer-events: none;` CSS property. – Shrirang Kadale Jul 13 '18 at 14:44
  • How would that work for users who navigate using the keyboard, @ShrirangKadale? Then someone could use `tab` to give focus to the wrong text area. Try it here: https://jsfiddle.net/xpo3fqLn/1/ - type into the text box, then use `shift+tab` to shift focus to the real text box, the cursor comes back, and the two text areas get out-of-sync. – Richard JP Le Guen Jul 13 '18 at 22:45
  • 1
    You are right @RichardJPLeGuen I did not think about that. Thank you!!! – Shrirang Kadale Jul 14 '18 at 06:27
6

I think this is a perfect solution: make the input wide enough, align right to screen right, thus make cursor and content locate at the outside of the screen, while it's still clickable perfect solution

Community
  • 1
  • 1
sinfere
  • 927
  • 8
  • 9
4

Unfortunately you can not style the text cursor with CSS. You can only do some very bad JavaScript tricks but depending on the layout and requirements of your website, it might not be possible at all. So I would recommend to forget the whole idea.

2ndkauboy
  • 9,302
  • 3
  • 31
  • 65
3

<input style="position: fixed; top: -1000px">

Works in iOS8.

Daiwei
  • 40,666
  • 3
  • 38
  • 48
1

you can "Hide textfield blinking cursor" by calling blur function on focus event

<input type="text" onfocus="this.blur()"/>
coder
  • 1,874
  • 2
  • 22
  • 31
  • 34
    Who up-voted this? If you blur it on focus, you'll never be able to type in it... – corescan Dec 03 '13 at 22:14
  • 5
    Unrelated but super useful for a different purpose. if you set an input field as "disabled" then change events won't trigger. doing this is a great workaround for keeping an input field disabled but still having the ability to attach change events to it. – But those new buttons though.. Aug 21 '14 at 01:53
  • 3
    If you only want an onclick event than this is useful. – Wolfgang Fahl Dec 21 '14 at 21:06
  • 2
    There is an element, which is called button, to fill this purpose. – Lajos Mészáros Jul 17 '15 at 12:22
  • 1
    This is useful when input is used for third party controls , like input is part of a drop down from infragistics : This fix removes the blinking text in IE for readonly input – katesky8 Nov 19 '19 at 16:33
1

function noCursor(a){
  var a = document.getElementById(a),
      b = document.createElement('input');
  b.setAttribute("style","position: absolute; right: 101%;");
  a.parentNode.insertBefore(b, a);

  if(a.addEventListener){
    b.addEventListener("input",function(){a.value = b.value});
    a.addEventListener("focus",function(){b.focus()});
  }else{
    a.attachEvent("onfocus",function(){b.focus()});
    b.attachEvent("onpropertychange",function(){a.value = b.value});
  };
 
}

noCursor('inp');
<input id="inp">

You can use the function for each element jou want no cursor for.

1

Setting the input to readonly also does this since it prevents focus but it may not be applicable to many use cases that still need it.

<input type="text" readonly />
Chase
  • 2,206
  • 1
  • 13
  • 17
1

List of recommended css solutions to hide the caret


  1. caret-color: transparent; - For my case this approach wasn't good enough since you're still able to manipulate the input field in order to show the caret on ios. You can reproduce it on an ipad by focusing on an input then press the keyboard button that brings the keyboard down. After that you can simply just click on the input field again and suddenly the caret appears. I have also been able to see the cursor on iphones but i'm not exactly sure how to reproduce it since it seems kind of random.

  1. opacity: 0 - This approach does not work on android devices since you won't be able to focus on the input. Another thing I don't like is the fact that the site wouldn't automatically scroll up/down to the input after focusing.

  1. text-indent: -9999em; - This isn't really a solution in itself since the caret always would be in the left upper corner of the input, atleast on ios devices. Though if you set the width of the input to overflow the website's width then you wouldn't be able to see the caret.

  1. visibility: hidden; display: none; - These solutions do remove the caret but you'll not be able to focus on the input, not even if you've implemented a click event to do it.

  1. font-size: 0; - I do not recommend this approach since it doesn't work on adroid devices and apparently some windows computers. The browser will also zoom in on the input if the font-size is less than 16px therefore you would have to add maximum-scale=1 to the meta viewport tag. You would also have to display the input somewhere else than the input field.

What I did

I ended up not using any of these methods since they didn't work well for my project. I instead used a lightweight code editor like Lajos Mészáros also recommended and made the height of the input 0. That also means you'll need to make a click event on another element that sets the focus for the input. You can look at Monkeytype for reference (I'm not affiliated to that website).

Nicolas
  • 97
  • 7
0

just made a proof of concept for a friend of mine, using @sinfere 's idea:

demo: http://jsfiddle.net/jkrielaars/y64wjuhj/4/

The start of the input is offset so it falls outside of the container (which has overflow hidden) The actual caracters (and blinking cursor) wil never enter into view. The fake div is placed below the input field so a tap on the fake div will set focus on the invisible input.

<div class="container">
    <div id="fake" class="fake">
        <span class='star empty'></span>
        <span class='star empty'></span>
        <span class='star empty'></span>
        <span class='star empty'></span>
    </div>
    <input type="text" id="password" class="invisible" maxlength="4">
</div>
JasperZelf
  • 2,731
  • 1
  • 22
  • 34