Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textarea to fill a parent container exactly, with padding

Tags:

html

css

I want a <textarea> to completely fill an absolutely-sized parent container, with padding in the textarea. I have this working for Chrome using the following code:

<div id="wrap"><textarea>hello stack overflow world</textarea></div> 
/* Color and border CSS omitted for simplicity */ #wrap    { position:absolute; top:10%; left:20%; right:30%; bottom:60%      } textarea { position:absolute; top:0; left:0; right:0; bottom:0; padding:1em } 

enter image description here

Unfortunately, Firefox (v5) does not properly honor the right/bottom position, producing these results:

Firefox's rendering of the markup; the textarea does not fill the container.

Here's a fiddle showing the problem in action: http://jsfiddle.net/ZEhwR/

How can I achieve the result stated in the first sentence across Chrome and FF (and ideally IE9 as well)?

like image 602
Phrogz Avatar asked Jul 22 '11 20:07

Phrogz


People also ask

How do I make a text area 100?

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 I Auto Resize textarea?

height = 'auto'; to this. style. height = '0px'; and add a min-height to prevent the initial state from actually going to 0, scrollHeight will correctly return one row of height when appropriate.

Is it valid to have a textarea element inside a button?

You shouldn't try to place a button inside a textarea, that's doesn't make sense semantically and shouldn't be done. You could do what you're currently doing: positioning the button on top of the textarea in the right place.


2 Answers

Use width: 100%; height: 100%; to make the <textarea> fill up the wrapping <div>. Unfortunately, you won't be able to put on margins/padding, as they get ADDED to the 100%, so the right/bottom edges will overflow.

Edit: To use width: 100%; along with padding, you can change the box sizing model:

    width: 100%;     height: 100%;      box-sizing: border-box; 

With this change, 100% now refers to the distance between the outsides of the borders, instead of the distance between the outside of the content.

like image 66
Marc B Avatar answered Oct 21 '22 16:10

Marc B


I have a solution where you can have 100% width and height with padding in the text area. I use negative margins to achieve the desired effect.

HTML:

<div class="container">     <textarea class="text"/></textarea> </div> 

CSS:

.container{   padding: 10px   border: 1px solid silver }  .container .text{   resize: none;   outline: none;   width: 100%;   padding: 10px;   border: none;   height: 100%;   margin: -10px; } 

Tested this working on Google Chrome and Firefox

like image 43
ShadyKiller Avatar answered Oct 21 '22 17:10

ShadyKiller