Creating a Nice Textarea

Published by Chris Coyier

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:

styledtextarea.png
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.

[LIVE EXAMPLE]