475

I have the following CSS and HTML snippet being rendered.

textarea
{
  border:1px solid #999999;
  width:100%;
  margin:5px 0;
  padding:3px;
}
<div style="display: block;" id="rulesformitem" class="formitem">
  <label for="rules" id="ruleslabel">Rules:</label>
  <textarea cols="2" rows="10" id="rules"></textarea>
</div>

Is the problem is that the text area ends up being 8px wider (2px for border + 6px for padding) than the parent. Is there a way to continue to use border and padding but constrain the total size of the textarea to the width of the parent?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
  • Btw there is good article for this by **Jeffrey Way** on tutsplus here: http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-did-internet-explorer-get-the-box-model-right/ Maybe it will help someone ;) – Somebody Mar 04 '11 at 10:16

15 Answers15

745

Why not forget the hacks and just do it with CSS?

One I use frequently:

.boxsizingBorder {
    -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
            box-sizing: border-box;
}

See browser support here.

Prisoner ZERO
  • 13,848
  • 21
  • 92
  • 137
Zaqwsx
  • 7,723
  • 2
  • 16
  • 10
  • 2
    This has got to be one of the coolest things I have found in a while. Thank you, any chance there is a way to do this in IE7 as well? – Jeremy A. West Mar 19 '12 at 16:11
  • 58
    Note that you'll still want to use width:100% in conjunction with the border-box. – Tyler Jun 06 '12 at 20:56
  • Unfortunately, this won't work in older versions of IE. Which means you'll need to write the code anyway for the IE stylesheet. –  Mar 21 '13 at 00:41
  • 3
    Doesn't old versions of Internet Explorer use border-box behavior only? – Peter Hedberg Apr 01 '13 at 18:05
  • In my experience, this disables the `rows` attribute. You'll need to set height via CSS as well. – Nick Apr 09 '13 at 19:45
  • Works fine for the form element as well. Might work for others as well. – Gayan Dasanayake Dec 10 '13 at 17:59
  • Works with type='file' as well. – MrCleanX Jan 02 '14 at 21:54
  • 6
    can someone explain this please, where does this class go? a div surrounding the textarea? – koolaang Aug 11 '16 at 21:25
  • 2
    This is fantastic... even after all these years, still learn something new every day. @koolaang this is basically saying the object's size shouldn't factor in padding, so in theory you can apply this style right to the textarea itself. – Mike Aug 25 '16 at 17:33
  • Agreed, re fantastic, I had ignored a lot of css 3 stuff for years because of needing to support ie 7, but for the last year or so I'm ignoring their users, who are also forming a minuscule market share. I believe the best thing you can do commercially at this point is totally ignoring msie 5 6 7 8, I can tell from our demographic, which is older, that people have basically stopped using msie in general due to these support issues, and these aren't tech users. So free yourself, lol, from msie 8-7 tyranny, onward bound, css 3! – Lizardx Mar 09 '17 at 03:15
95

The answer to many CSS formatting problems seems to be "add another <div>!"

So, in that spirit, have you tried adding a wrapper div to which the border/padding are applied and then putting the 100% width textarea inside of that? Something like (untested):

textarea
{
  width:100%;
}
.textwrapper
{
  border:1px solid #999999;
  margin:5px 0;
  padding:3px;
}
<div style="display: block;" id="rulesformitem" class="formitem">
  <label for="rules" id="ruleslabel">Rules:</label>
  <div class="textwrapper"><textarea cols="2" rows="10" id="rules"/></div>
</div>
captainsac
  • 2,484
  • 3
  • 27
  • 48
Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
  • 1
    I ended up moving away from using % widths. I believe your approach would work just as well though. Thanks! – Eric Schoonover Nov 07 '08 at 22:51
  • 5
    Don't forget to add "." to the textwrapper class. – Chris Porter Mar 24 '10 at 16:46
  • 3
    @Chris: Thanks and fixed. I'm mildly surprised that it took almost a year and a half for someone to catch that... – Dave Sherohman Mar 25 '10 at 08:28
  • 2
    How will the textarea's scrollbars look in this case? – Daniel LeCheminant Apr 22 '10 at 15:26
  • 1
    In chrome, when you focus textarea element it's gets hightlighted automatically and if you create padding for div wrapper, this padding will be visible and two borders will be visible because of that. One from hightlighting and another from .textwrapper border:1px solid #999999; – Somebody Mar 04 '11 at 10:13
  • I found a problem testing this code, I had to add a instead of in order to have that correctly working. – Mc- Feb 19 '12 at 16:29
24

let's consider the final output rendered to the user of what we want to achieve: a padded textarea with both a border and a padding, which characteristics are that being clicked they pass the focus to our textarea, and the advantage of an automatic 100% width typical of block elements.

The best approach in my opinion is to use low level solutions as far as possible, to reach the maximum browsers support. In this case the only HTML could work fine, avoiding the use of Javascript (which anyhow we all love).

The LABEL tag comes in our help because has such behaviour and is allowed to contain the input elements it must address to. Its default style is the one of inline elements, so, giving to the label a block display style we can avail ourselves of the automatic 100% width including padding and borders, while the inner textarea has no border, no padding and a 100% width.

Taking a look at the W3C specifics other advantages we may notice are:

  • no "for" attribute is needed: when a LABEL tag contains the target input, it automatically focuses the child input when clicked;
  • if an external label for the textarea has already been designed, no conflicts occur, since a given input may have one or more labels.

See W3C specifics for more detailed information.

Simple example:

.container { 
  width: 400px; 
  border: 3px 
  solid #f7c; 
  }
.textareaContainer {
 display: block;
 border: 3px solid #38c;
 padding: 10px;
  }
textarea { 
  width: 100%; 
  margin: 0; 
  padding: 0; 
  border-width: 0; 
  }
<body>
<div class="container">
 I am the container
 <label class="textareaContainer">
  <textarea name="text">I am the padded textarea with a styled border...</textarea>
 </label>
</div>
</body>

The padding and border of the .textareaContainer elements are the ones we want to give to the textarea. Try editing them to style it as you want. I gave large and visible padding and borders to the .textareaContainer element to let you see their behaviour when clicked.

Bill-G
  • 305
  • 4
  • 12
15

If you're not too bothered about the width of the padding, this solution will actually keep the padding in percentages too..

textarea
{
    border:1px solid #999999;
    width:98%;
    margin:5px 0;
    padding:1%;
}

Not perfect, but you'll get some padding and the width adds up to 100% so its all good

jontyc
  • 3,445
  • 6
  • 29
  • 36
Chris James
  • 11,571
  • 11
  • 61
  • 89
13

I came across another solution here that is so simple: add padding-right to the textarea's container. This keeps the margin, border, and padding on the textarea, which avoids the problem that Beck pointed out about the focus highlight that chrome and safari put around the textarea.

The container's padding-right should be the sum of the effective margin, border, and padding on both sides of the textarea, plus any padding you may otherwise want for the container. So, for the case in the original question:

textarea{
    border:1px solid #999999;
    width:100%;
    margin:5px 0;
    padding:3px;
}
.textareacontainer{
    padding-right: 8px; /* 1 + 3 + 3 + 1 */
}

<div class="textareacontainer">
    <textarea></textarea>
</div>
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
Brian
  • 477
  • 3
  • 5
  • 1
    +1 This will work in more browsers than the CSS3 statements. The unfortunate truth is that the world still needs wrapper divs. – BenSwayne Aug 10 '12 at 21:29
  • I really like this answer. It makes the textarea snap to fit without any magic percent numbers less than 100%. You can use padding on all sides in the wrapper to force the text area to move in and remain inside the parent. The min-height and height need to be 100% on the wrapper as well. The textarea can then be set to 100% with the same padding to make everything fit perfectly. – justdan23 Dec 13 '18 at 00:45
11

This table hack code works for me in all browsers from IE8+

<td>
    <textarea style="width:100%" rows=3 name="abc">Modify width:% accordingly</textarea>
</td>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Jeff Guest
  • 119
  • 1
  • 3
8

The problem lies in the box-sizing property. By default, the initial value of the box-sizing property is content-box. so you have something like this under the hood:

textarea {
  border:1px solid #999999;
  width:100%;
  margin:5px 0;
  padding:3px;
  box-sizing: content-box;
}

box-sizing: content-box; means that the width of the actual element is equal to the width of the element's content box. so when you add padding (in this case padding-right and padding-left --> because we are talking about width) and border (in this case border-right and border-left --> because we are talking about width), these values get added to the final width. so your element will be wider than you want.

set it to box-sizing: border-box;. so the width will be calculated like so:

horizontal border + horizontal padding + width of content box = width

in this case, when you add horizontal border and horizontal padding, the final width of element does not change, in fact, the content box will shrink to satisfy the equation.

Ali Baghban
  • 517
  • 1
  • 5
  • 13
6

I was looking for an inline-styling solution instead of CSS solution, and this is the best I can go for a responsive textarea:

<div style="width: 100%; max-width: 500px;">
  <textarea style="width: 100%;"></textarea>
</div>
Jee Mok
  • 6,157
  • 8
  • 47
  • 80
  • It did not work for me in the latest chrome version. I had to use min-width:100% to get it working. – dotcoder Oct 13 '20 at 07:18
  • @MohanGundlapalli it's a simple CSS rule that should work for the latest Chrome too, is there any other styling affecting the `width: 100%` rule? maybe you can try inspecting it with the Chrome dev tool – Jee Mok Oct 14 '20 at 00:57
5

You can make use of the box-sizing property, it's supported by all the main standard-compliant browsers and IE8+. You still will need a workaround for IE7 though. Read more here.

Stacked
  • 6,892
  • 7
  • 57
  • 73
jzfgo
  • 367
  • 3
  • 8
2

No, you cannot do that with CSS. That is the reason Microsoft initially introduced another, and maybe more practical box model. The box model that eventually won, makes it inpractical to mix percentages and units.

I don't think it is OK with you to express padding and border widths in percentage of the parent too.

buti-oxa
  • 11,261
  • 5
  • 35
  • 44
1

If you pad and offset it like this:

textarea
{
    border:1px solid #999999;
    width:100%;
    padding: 7px 0 7px 7px; 
    position:relative; left:-8px; /* 1px border, too */
}

the right side of the textarea perfectly aligns with the right side of the container, and the text inside the textarea aligns perfectly with the body text in the container... and the left side of the textarea 'sticks out' a bit. it's sometimes prettier.

commonpike
  • 10,499
  • 4
  • 65
  • 58
1

For people who use Bootstrap, textarea.form-control can lead to textarea sizing issues as well. Chrome and Firefox appear to use different heights with the following Bootstrap CSS:

textarea.form-conrtol{
    height:auto;
}
Gwi7d31
  • 1,652
  • 2
  • 12
  • 10
1

I often fix that problem with calc(). You just give the textarea a width of 100% and a certain amount of padding, but you have to subtract the total left and right padding of the 100% width you have given to the textarea:

textarea {
    border: 0px;
    width: calc(100% -10px);
    padding: 5px; 
}

Or if you want to give the textarea a border:

textarea {
    border: 1px;
    width: calc(100% -12px); /* plus the total left and right border */
    padding: 5px; 
}
Jeroen Bellemans
  • 2,049
  • 2
  • 25
  • 42
0

How about negative margins?

textarea {
    border:1px solid #999999;
    width:100%;
    margin:5px -4px; /* 4px = border+padding on one side */
    padding:3px;
}
meustrus
  • 6,637
  • 5
  • 42
  • 53
0

The value of the padding has a role to play. Using the style you posted:

textarea {
  border:1px solid #999999;
  width:100%;
  margin:5px 0;
  padding:3px;
}

The width is already filled up and you have padding left, right to be 3px. So there will be an overflow.

If you change your style to this:

textarea
{
  border:1px solid #999999;
  width:98%;
  margin:5px 0;
  padding: 3px 1%;
}

What my styling is doing now is it has a width of 98% and its remaining 2% to complete a 100% and that is why I gave padding left 1% and padding right 1%. With this, the issue of overflow should be fixed

Nature
  • 149
  • 1
  • 6