7

I need to hide the text box blinking cursor in CSS / Javascript.

Is it possible?

8
  • Why? Is the user going to be entering data into that field? If so, this sounds like a usability nightmare.
    – Buggabill
    Jan 29, 2010 at 13:28
  • Do you want them to enter into this textbox? What is the usage? You can always redirect the focus away on a click or focus event on the box...but what is the use case here?
    – curtisk
    Jan 29, 2010 at 13:29
  • 1
    I certainly hope not. That would be quite annoying if I couldn't see where I was typing. Jan 29, 2010 at 13:29
  • Do you want to hide it or change it for other type?
    – Amra
    Jan 29, 2010 at 13:32
  • "I want to customize it where it should not look like text box" --> ok, you can change the looks, but it still needs to behave like a textbox
    – Natrium
    Jan 29, 2010 at 13:47

5 Answers 5

14

You could set a maxlength on the textbox and then use text-indent to move the cursor back more characters than the maxlength.

For example, you could set maxlength=20 and then for the text box set text-indent: -20em that way the text starts out of the box's boundaries and can't ever come into view.

1
  • but that also hides all text in the textbox.... i guess the author want something like allowing you to scroll the text but not show the blinking cursor... but that's just a guess as his question was 2 lines.
    – gcb
    Jan 16, 2015 at 0:53
4

Here is my solution from another question, that I answered already:

https://stackoverflow.com/a/23472096/1806628


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.

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

Warning:

It does not seem to work under iOS 8. (Thanks @Altaveron for the info)


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.

2

This is pretty old, but I was just dealing with a similar issue. For browsers that support it (meaning, not IE8), you can set the color of the input element to be the same as its background (probably white), and then the cursor will be invisible.

1
<input type=text disabled="disabled"/>

because i can't see any other reason you might want to do that.

edit:

i guess you want to allow the user to scroll the text that is larger than the area, but not show the blinking?

if that is so, you still want to disable it. not just hide the blinking cursor. if the user can't type there, it should be disabled. period.

now, if you still want to allow the user to see all the content, you have to make the input as big as the content. there is no escaping that.

and then limit the size with a parent div with CSS overflow: hidden or scroll.

<div style="overflow: scroll-x;"><input size=255 value="some string 255 char long" /></div>
12
  • 4
    The correct attribute value is disabled and not true: disabled="disabled".
    – Gumbo
    Jan 29, 2010 at 13:51
  • 2
    @Gumbo: that's correct for XHTML, but there's not often a need to use XHTML. Easiest is to write pages in HTML and use just disabled, with no value.
    – Tim Down
    Jan 29, 2010 at 14:06
  • 2
    @Tim Down: The correct value for HTML is disabled too. There it’s just that the attribute value can be omitted since the only valid attribute value disabled is implied (see w3.org/TR/html4/interact/forms.html#adef-disabled).
    – Gumbo
    Jan 29, 2010 at 14:23
  • 1
    @Gumbo: well yes. There are two correct ways of using a boolean attribute in HTML 4: on its own with no value or with a value that is the same as its name. Neither is more correct than the other, although the following line at the bottom of w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2 is worth noting: "Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form.".
    – Tim Down
    Jan 29, 2010 at 15:07
  • 2
    How is this the right answer? This doesn't do what is asked for... What we're looking for is to hide the caret but still enable input
    – ditoslav
    Jun 13, 2014 at 21:06
0

If it's an input with the readonly="readonly" attribute on it, Safari on iOS still shows a blinking cursor when focusing on it.

So to remove the blinking, the following worked for me:

-webkit-user-select: none; 
user-select: none;

Not the answer you're looking for? Browse other questions tagged or ask your own question.