Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you you show a grey hint in an HTML text box with CSS alone?

You see these text input boxes from time to time on the web: a grey label is shown inside the box, but once you type there, the grey text disappears. This page even has one: the "Title" field behaves exactly like that.

So, questions:

  1. Is there a standard term for this? I'm really struggling to find anything on google

  2. Can it be done with just CSS?

  3. Failing that, can it be done with localised JavaScript? (ie, code just within the tag, not in the HTML header).

like image 364
Steve Bennett Avatar asked Sep 27 '10 04:09

Steve Bennett


People also ask

How do you give a TextBox a hint in HTML?

If you want to set a hint for text area or input field, then use the HTML placeholder attribute. The hint is the expected value, which gets displayed before the user enters a value, for example, name, details, etc.

How do I GREY out a text box?

In HTML, to “grey out” the text box or to disable it simply mention keyword “ disabled” in your input tag.

How do I create a hint in HTML?

HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .

How do you color a text box in CSS?

Changing Text Background Color in CSS. To change the background color of the inline text, go to the <head> section. Simply add the appropriate CSS selector and define the color and background-color property with the values you want.


2 Answers

The "placeholder" attribute is now supported across all major browsers.

<form>
<input type="text" placeholder="placeholder text">
</form>​

Styling it still seems to require vendor prefixes:

input::-webkit-input-placeholder {
    color: #59e;
}    
input:-moz-placeholder {  
    color: #59e;  
}
​

Seen here: http://jsfiddle.net/webvitaly/bGrQ4/2/

like image 181
Steve Bennett Avatar answered Sep 22 '22 00:09

Steve Bennett


I don't know of a way to do it with CSS. But looking at the page source, SO is doing it thusly-wise:

<input name="q" class="textbox" tabindex="1" 
       onfocus="if (this.value=='search') this.value = ''" 
       type="text" maxlength="80" size="28" value="search">

The middle line starting with "onfocus" is where the work is happening. When the text field gets focus, it checks to see if the default value ("search") is there. If so, it sets the value to '' (empty string). Otherwise, the text is unaffected.

One potential issue here is that if someone is trying to search for the word "search", then clicks outside of the box and clicks back in again, the text gets reset. Not a huge issue, but something to keep in mind as you're working.

like image 43
AaronM Avatar answered Sep 24 '22 00:09

AaronM