CSS-Tricks PSD to HTML: You Design - We XHTML

Creating a Nice Textarea

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 it’s 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]


Theoretically Related Articles:

Discussion Elsewhere


Responses


  1. 1

    Gravatar

    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.


    Comment by Jermayn Parker — September 24, 2007 @ 9:03 pm

  2. 2

    Gravatar

    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.


    Comment by Chris Coyier — September 25, 2007 @ 6:06 am

  3. 3

    Gravatar

    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…


    Comment by fluminis — September 25, 2007 @ 7:02 am

  4. 4

    Gravatar

    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 =)


    Comment by Chris Coyier — September 25, 2007 @ 7:27 am

  5. 5

    Gravatar

    always wanted to know how they get the text background image …
    now that i know …
    back to editing the style sheet and stuff …
    dang …


    Comment by subcorpus — October 4, 2007 @ 9:02 pm

  6. 6

    Gravatar

    Real cool, can I used it on may web site.


    Comment by Feliks — May 9, 2008 @ 11:35 am


Leave a comment

Sick of typing in all this info everytime you comment? Register or Login and save yourself time!

LINKS: You can use <a href="">LINK</a> tags here.
CODE SAMPLES: Please wrap code samples in BOTH <pre> and <code> tags.

Thank you for visiting CSS-Tricks! I'm glad you found an article useful enough to print out! Remember to visit css-tricks.com often for more fresh content.