Full Width Textareas

By  on  

Working with textarea widths can be painful if you want the textarea to span 100% width.  Why painful?  Because if the textarea's containing element has padding, your "width:100%" textarea will likely stretch outside of the parent container -- a frustrating prospect to say the least.  Luckily there's a quick CSS solution to this problem!

The CSS

box-sizing to the rescue!

textarea {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;

	width: 100%;
}

Setting the box-sizing to border-box allows the textarea to respect its parent container's padding and border, recalculating what 100% actually means.  If the box-sizing were content-box, the textarea would continue to stretch outside the parent container as it would have before.

Recent Features

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

Incredible Demos

  • By
    Multiple Backgrounds with CSS

    Anyone that's been in the web development industry for 5+ years knows that there are certain features that we should have had several years ago. One of those features is the HTML5 placeholder; we used JavaScript shims for a decade before placeholder came...

  • By
    Create a Simple Dojo Accordion

    Let's be honest:  even though we all giggle about how cheap of a thrill JavaScript accordions have become on the web, they remain an effective, useful widget.  Lots of content, small amount of space.  Dojo's Dijit library provides an incredibly simply method by which you can...

Discussion

  1. Very nice. I’ve struggled with this in the past. Top tip

  2. Great tip. Thanks.

  3. Great tip.
    Is it support in all browser?

  4. brett

    last I checked, ie9 did not support this, but i think ie10 had plans to

  5. Actually it is quite well supported :
    Firefox, Opera, Safari, Chrome, IE8+

    See : http://caniuse.com/#feat=css3-boxsizing

  6. See this Paul Irish link…

    http://paulirish.com/2012/box-sizing-border-box-ftw/

    Helps tremendously with mobile development as well and calculation of % width when utilizing this box-sizing.

  7. Tom

    Does not work for me (Win7, Firefox 15.0.1)

  8. I use Win7 64bits and tested with Firefox 15.0.1, IE9, Safari and Chrome. All working fine
    Be sure to use all 3 declarations for maximum compatibility:

    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    
  9. Does not work for me too (Mac OS X, Chrome 26)

  10. Hmm… nice, I got saved! thanks for sharing :D

  11. Nice, it works…

  12. Hi David! First i should say thanks about what you have written, I have question, i have done whatever you said but i wanna get some space from around of its parent tag, and i gave it margin:10px . But it doesn’t work ! Why?

  13. Alf

    Doesn’t work for me (Windows 7 + Chrome 46.0.2490.80 m)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!