A textarea is an element on a webpage that you can type into. These are commonly used as commenting areas, contact forms or address entry areas. All browsers have defaults styles for textareas which vary. You can take control of your textareas and style them with CSS, just like any other element:

textarea#styled {
width: 600px;
height: 120px;
border: 3px solid #cccccc;
padding: 5px;
font-family: Tahoma, sans-serif;
background-image: url(bg.gif);
background-position: bottom right;
background-repeat: no-repeat;
}
In this case, the textarea with the id of “styled” has its width and height explicity set, a border and padding applied, default font set, and a background image applied.
Textareas also support some other features to help you control their aesthetic behavior. Namely, “onfocus” and “onblur” which you can apply inline and give javascript commands, like so:
<textarea name="styled-textarea" id="styled" onfocus="this.value=''; setbg('#e5fff3');" onblur="setbg('white')">Enter your comment here...</textarea>
This will change the background color of the textarea to a light green color when it’s active and back to white when it is inactive. Just include this simple script somewhere on your page*:
function setbg(color)
{
document.getElementById("styled").style.background=color
}
*Either create a .js file and link to it in your header or put it inside a <script type=”text/javascript”> tag.
See all this in action at the example page.
Could you not include the css for the textbox being active etc in the stylesheet? and what is the difference between the a:hover etc and using the javascript??
Nice tip using a background image with the words “Thank you”, maximising the space.
Well….. the only way to include it in the stylesheet is to include it as a pseudo class like this:
input:focus, textarea:focus{
background-color: lightyellow;
}
But, that’s not gonna work in IE 6 or below. Also, using the Javascript allows you to “clear” the field, which is a nice touch I think. It’s kind of annoying if you click into a field and have to manually delete what is inside it.
Be carreful with the onfocus=”this.value=””.
Each time the user leave the textarea and come back, he loose he changes…
You need to use a little more complicated javascript…
That is a good point, if you don’t want the form to clear if a user clicks away and back in after that first time, you just need to do this instead:
onfocus="this.value=''; this.onfocus=null;"
A little more complicated, but not too bad =)
Now if you do this the textarea doesn’t change back to the onfocus color.
always wanted to know how they get the text background image …
now that i know …
back to editing the style sheet and stuff …
dang …
Real cool, can I used it on may web site.
Oh I like this! Very cool. :)
How do I do it but with text instead of an image like your search box?
Thank you very much for the post.
btw, Nice blog, too!!!
I don’t suppose you know of a way to get textareas to exhibit console-like behavior a la this video? I suppose it might use flash – kind of hard to tell.
I couldn’t make it work, tried everything I could think of.
How would I do this with two text areas on one page?
Really nice tip! Thanks!
Hi,
Although you wrote jQuery, you used just javascript.
in jQuery, you can write :
$(‘styled’).focus().css(‘background’, ‘#e5fff3’);
To make textarea active when on click with css not with jacascript function?
Hey, this worked perfectly for my form! Thanks. I wondered where exactly I would add a red border like the rest of the form?
http://prismcareernetworks.com/prism_wordpress/wp-content/forms/registration_candidate/registration_form_candidate.html
I spoke too soon. It’s only working in the first textarea and not the rest. I used the id=”styled” on each textarea but nothing happens. ???
I figured it out.
@Checmark – Can you please describe how you made this work for multiple textareas ?