Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a TextArea 100% width without overflowing when padding is present in CSS?

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"/>  </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?

like image 775
Eric Schoonover Avatar asked Nov 07 '08 02:11

Eric Schoonover


People also ask

Does width 100% include padding?

The width and height properties include the content, but does not include the padding, border, or margin.

How do I make 100 area text width?

The idea is to create a div with the class name “wrapper”. Inside that <div> element, we create a text area with a certain number of columns and rows. In this case, it is 30 and 15 respectively. After that, we set the width property to 100% to make a textarea width 100%.

How do you prevent padding from increasing width?

to fix this, you simply need to update the box-sizing parameter and set this to border-box in your css. Or you can do this for all elements by simply adding the following. This tells the browser to account for any border and padding in the values you specify for an element's width and height.


2 Answers

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.

like image 110
Piet Bijl Avatar answered Sep 19 '22 10:09

Piet Bijl


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>
like image 29
Dave Sherohman Avatar answered Sep 20 '22 10:09

Dave Sherohman