Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make textarea fill the width of its parent width?

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?

Thank you.

like image 393
johnjohn Avatar asked Jul 31 '10 12:07

johnjohn


People also ask

How do you get the full width of a text area?

The idea is to create a div with the class name “wrapper”. Inside that <div> element, we create a text area with a certain number of columns and rows. In this case, it is 30 and 15 respectively. After that, we set the width property to 100% to make a textarea width 100%.

How do I increase the width of a textarea in HTML?

You need to define width of the div containing the textarea and when you declare textarea , you can then set . main > textarea to have width: inherit . Note: .

How do you set full width?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.

How to make a textarea width 100% in HTML?

Inside that <div> element, we create a text area with a certain number of columns and rows. In this case, it is 30 and 15 respectively. After that, we set the width property to 100% to make a textarea width 100%. HTML Code: The HTML code contains a <textarea> element that holds rows and columns values. padding is present?

Can CSS make a box fit within any width of its parent?

… result in it fitting nicely within any width of its parent. Can CSS actually do this? If not that would be pretty lame. It can be done with CSS3’s box-sizing property. You would set the value to border-box and then it will account for the borders and padding (everything from the outer points of the border inward).

Why is my page width so low in CSS?

Well, this is happening because you’ve set it to 100% of the width, when you want it to be less than that. Plus, the padding actually adds to the width. So you just need to make it less than 100%. I would use px, since that’s what you use.

Why won't my textarea stretch outside of the parent container?

Because if the textarea's containing element has padding, your "width:100%" textarea will likely stretch outside of the parent container -- a frustrating prospect to say the least. Luckily there's a quick CSS solution to this problem! box-sizing to the rescue!


2 Answers

<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.

like image 164
Krevan Avatar answered Sep 22 '22 06:09

Krevan


I advice 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>
like image 38
Klesun Avatar answered Sep 24 '22 06:09

Klesun