views:

80

answers:

3

Hi,

I am working on web application.

I wanted to apply auto height to textarea using CSS, dont want to use any script, jquery plugin and other stuff.

After applying class ( i.e. style property ) to textarea, it should automatically increase it's height not width as per content present it in.

In this case width should be fixed i.e. width: 98%; (In my case) only height needs to grow. So scroll bars should exist for text area.

I simply needed one CSS so that after applying to textarea, it should be auto grow like <DIV>.

Please folks do sugggest, is this possible using CSS. If this is not possible, then m okey if i get javascript statments to acheives my requirement.

Thanks,

Pravin

+1  A: 

This is not possible with pure CSS, you will need to use JavaScript

Darko Z
Thanks , i had dought for the same, I am okey if i get some javascript statments which achives my requirements.
pravin
+1  A: 

It's sort of semi-doable in html/CSS. There are, however, the usual caveats of browser support and, since it uses html5's contenteditable, it requires a fairly modern browser.

That said, the following works (in Chrome/Ubuntu 10.04):

<div id="wrap">
    <div id="editThis" contenteditable>
    </div>
</div>

With the following CSS:

div#editThis {
    min-height: 4em;
    height: auto;
    width: 50%;
    margin: 0 auto;
}
div#editThis:hover,
div#editThis:focus {
    border: 1px solid #000;
}

Demo posted at jsbin

David Thomas
@David: Thanks a lot for providing this....bt it seems that it's related to <DIV> , How do i supply this to <TEXTAREA> ?
pravin
It does apply to `div`, a `textarea` *already* has editable content. The problem, though, is that `textarea` won't use `height: auto` in the way that you want. So I used a `div` to force that behaviour.
David Thomas
+2  A: 

If you're only displaying text in a textarea and not using it to get more content input from the user then consider using a div and styling it to look like a textarea.

the other thing i have seen is an auto expanding textarea that grown in height as you type.

see here: http://james.padolsey.com/javascript/jquery-plugin-autoresize/

Moin Zaman