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?
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.
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.
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.
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
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 :
<div class="container">
<div class="button-wrapper">
<button>X</button>
</div>
<div class="textarea-wrapper">
<textarea></textarea>
</div>
</div>
.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;
}
.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With