I have a textarea inside a div, and I wish for the text area height to match the height of the div container.
Currently, the width fits perfectly, however the textbox (which begins empty) only fills about 20% of the div height.
And when an ajax call grabs some text and populates the textarea, the textarea height remains at about 20% of the div and provides a scroll bar.
How can I alter my CSS to ensure that the textarea height always matches the height of its div container?
#answerBox {
width: 100%;
height: 100%;
box-sizing: border-box;
margin: 5px auto;
padding: 5px;
font-size: 10px;
text-align: left;
position: relative;
}
#answerBoxDiv {
width: 90%;
min-height: 50%;
margin: 5px auto;
border: 4px solid #00bfb6;
padding: 5px;
font-size: 10px;
text-align: left;
position: relative;
}
<div id="answerBoxDiv">
<textarea id="answerBox"></textarea>
</div>
5
Answers
try keeping the min-height of the textarea as 100% or nearby or try using
object-fit: contain(or cover)
on your text areaYou can try this
Give specific
height
for #answerBoxDiv parent div. So you will get appropriate height for this div.You need to explicitly set a height of the parent container (whether that is in px or rem etc) – that way the height: 100% of the textarea will fill to the container.
The expand on text content cannot be done with simple css – you need js to determine the heaight of the content and adjust the parent container accordingly.
The only way tyou can keep height: 100% on the parent container is its ancestor has a height set (eg 100vh) – that way the browser can determine the height of each DOM element and size the text area accrodingly.
UPDATE – I have added a js function to automatiucally increae the height of the parent container on the input. (the textarea autoincreases in height since it is 100% of the parentThis will need massaging – but when you type into the textarea the height will auto expand.
Percentage height does not work with a min-height parent, you would either need to give your parent a height or you could use flex: