Reputation: 6025
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>
:
Why the difference, and how can I get the appropriate effect on the textarea?
Upvotes: 22
Views: 33900
Reputation: 345
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%;
}
Upvotes: 22
Reputation: 6783
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;
}
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 :
Upvotes: 0
Reputation: 41852
If you set a fixed height to the button
then you can use positioning and do something like this:
HTML: (removed br tag)
<div class="container">
<button>X</button>
<textarea></textarea>
</div>
<div class="container">
<button>X</button>
<div>Text</div>
</div>
CSS
.container {
height: 220px;
width: 220px;
background-color: pink;
display: table;
}
.container > button {
display: table-row;
}
.container > textarea, .container > div {
display: table-row;
border: none; /* if you keep border then textarea will go out of the parent element */
height: 200px; /* hardcoded */
padding: 0px; /* removing padding for chrome */
width: 100%;
background-color: cyan;
}
Upvotes: 0