21

Given that we don't know the div's width beforehand?

My idea: Calculate the div 's width with JS, find a way to convert it to cols and the apply the css to the textarea onthe fly. But perhaps there 's no need to reinvent the wheel?

2 Answers 2

39

<textarea style="width:100%">* will work in most cases.

If it doesn't, please provide more information!

* Note: for some reason, it will need the cols and rows attributes to validate. But setting a width or a height will override them, so put any value you want.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 I never realized that you need those attributes in order to make width:100% work. I used some other work around to automatically resize the textarea if its parent container is resized but now it is so much easier :) Thank you!
@FelixKling I don't think cols and rows are required to make the width:100% work... cols and rows are required attributes of a textarea HTML element, which considered invalid without them, i.e. an HTML validator will complain. The width:100% working the way you expect, is a function of the browser. This did not work for older browsers.
5

I advise flex over width: 100%, as 100% often does not account for borders, causing horizontal scrolls to appear.

<div style="display: flex"> <textarea style="flex: 1"></textarea> </div> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.