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:
Is there a standard term for this? I'm really struggling to find anything on google
Can it be done with just CSS?
Failing that, can it be done with localised JavaScript? (ie, code just within the tag, not in the HTML header).
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.
In HTML, to “grey out” the text box or to disable it simply mention keyword “ disabled” in your input tag.
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" .
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.
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With