Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a <textarea> fill remaining height

Tags:

css

I learned from this question how to make an HTML element fill the remaining height of a container. But it doesn't seem to be working with <textarea>. This Fiddle compares the effects of display: table-row on a <textarea> and a <div>:

http://jsfiddle.net/b4Tt8/2/

Why the difference, and how can I get the appropriate effect on the textarea?

like image 730
Jack M Avatar asked Aug 27 '13 06:08

Jack M


People also ask

How can I fix the height of my textarea?

To prevent a text field from being resized, you can use the CSS resize property with its "none" value. After it you can use the height and width properties to define a fixed height and width for your <textarea> element.

How do I Autogrow textarea?

Now to make the textarea autogrow, you must hide the scrollbar and adjust the height of the textarea to the height of the contents in it. This adjustment of height should occur whenever the height of the contents change. scrollHeight of the textarea will give you the height of the entire content of the textarea.

How do I limit textarea size?

In some cases, there is a need of putting a limit on the size of characters that can be typed in a textarea. So in that case, we can use maxlength attribute to control the number of characters entered in a textarea.


2 Answers

You can use flexbox to set your textarea height according to its parent.

HTML

<div class="container">
  <div class="btn-wrapper">
    <button>X</button>
  </div>
  <textarea></textarea>
</div>

CSS

.container {
  height: 220px;
  width: 220px;
  background-color: pink;
  display: flex;
  flex-direction: column; 
 } 

.container textarea {
  box-sizing: border-box; /* fit parent width */
  height: 100%;
}

jsFiddle

like image 168
bolloga Avatar answered Oct 09 '22 11:10

bolloga


Why are you using display: table-row; declarations ? There is no need for this. Remove the display: table-row; declarations, add a box-sizing: border-box; declaration to your textarea selector and you're all set :

.container
{
    height: 220px;
    width: 220px;
    background-color: pink; 
}

.container > textarea
{
    width: 100%;
    height: 100%;
    background-color: cyan;
    box-sizing: border-box;
}

.container > div
{
    width: 100%;
    height: 100%;
    background-color: cyan;
}

Fiddle

EDIT :

The CSS above makes the text area overflowing its parent div.

Here is an updated answer :

HTML

<div class="container">
    <div class="button-wrapper">
        <button>X</button>
    </div>
    <div class="textarea-wrapper">
        <textarea></textarea>
    </div>
</div>

CSS 2

.container {
    height: 220px;
    width: 220px;
    background-color: pink;
    position: absolute;
}
.container textarea {
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 0, 0.5);
    box-sizing: border-box;
}
.container > div {
    background-color: cyan;
}
.container .button-wrapper {
    background-color: yellow;
    height: 26px;
}
.container .textarea-wrapper {
    background-color: green;
    position: absolute;
    top: 26px;
    width: 100%;
    bottom: 0;
}

CSS 3 (using calc function)

.container {
    height: 220px;
    width: 220px;
    background-color: pink;
}
.container textarea {
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 0, 0.5);
    box-sizing: border-box;
}
.container > div {
    background-color: cyan;
}
.container .button-wrapper {
    background-color: yellow;
    height: 26px;
}
.container .textarea-wrapper {
    background-color: green;
    height: calc(100% - 26px);
}

Here are fiddles that shows both solutions :

CSS 2

CSS 3

like image 37
Eric MORAND Avatar answered Oct 09 '22 11:10

Eric MORAND