0

I found many javascript plugins to make a textarea or input box elastic but what i'm wondering is:

is it possible to make something like this http://unwrongest.com/projects/elastic/ in pure css?

Charles
  • 50,943
  • 13
  • 104
  • 142
itsme
  • 48,972
  • 96
  • 224
  • 345

2 Answers2

2

Answer is no, you cannot accomplish that using pure CSS, you cannot calculate how many letters did the user typed, what's the height of the textarea and how much you should increase proportionally while the user is typing.

So that's best suited if you use jQuery or JavaScript and increase the height on onkeyup or onchange event.

The very best in least JavaScript you can get is this


Or transit the element on focus if you don't want to throw the big box on load...

textarea {
   height: 100px;
   width: 300px;
   -webkit-transition: all .5s;
   -moz-transition: all .5s;
   transition: all .5s;
   /* Used transition just to animate the resize, if not 
      required can be removed */
}

textarea:focus {
    height: 300px;
    width: 500px;
}

Demo


As you requested for minimal JavaScript to be used.. you can refer Rob's solution here

var textarea = document.getElementById("textarea");
var heightLimit = 200; /* Maximum height: 200px */

textarea.oninput = function() {
  textarea.style.height = ""; /* Reset the height*/
  textarea.style.height = Math.min(textarea.scrollHeight, heightLimit) + "px";
};

Credits for code - Rob W

Demo

Community
  • 1
  • 1
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • 1
    I recommend `oninput` over `onkeydown`, see my edited answer http://stackoverflow.com/a/7745840/1542290 (demo: http://jsfiddle.net/gjqWy/155) – Rob W Jan 21 '14 at 17:50
1

Quite simply, it is not possible using pure CSS and a textarea. However, you can use an editable div to do that. Here is an example of what you could do that creates a textarea like box that expands as the user types.

<div id="editable" contenteditable></div>
<style>
div#editable{
    min-height: 2em;
    height: auto;
    width: 200px;
    border: 1px solid #999;
}
div#editable:focus {
    border: 1px solid #000;
}
</style>
bitoffdev
  • 3,134
  • 1
  • 13
  • 16
  • man this could be awesome, do you know or have any idea on how to fake the placeholder tho? – itsme Jan 21 '14 at 16:31
  • @sbaaaang don't think that this will be submitted just like textboxes in the form – Mr. Alien Jan 21 '14 at 16:38
  • @Mr.Alien sure man just since i'm using a js form i can easly get div content :P if i can get how to fake the placeholder this solves a lot – itsme Jan 21 '14 at 16:40
  • @sbaaaang but won't make any sense, you are just increasing your work by writing more JS compared to a small plugin file, which will accomplish job easily... – Mr. Alien Jan 21 '14 at 16:41
  • Why do you need to use pure CSS? – bitoffdev Jan 21 '14 at 16:42
  • Yea, now thats my question as well – Mr. Alien Jan 21 '14 at 16:43
  • @Mr.Alien 3 lines vs 3/4 kb at least? :O , also i can't get any javascript plugin to work good actually only jquery but i'm not using frameworks and libray :( – itsme Jan 21 '14 at 16:43
  • @sbaaaang still, I would suggest you to stick with the standards, who knows what device would act with `contenteditable`? and as far as placeholder goes, you can use `onblur` and `onfocus` if you are looking forward to this solution – Mr. Alien Jan 21 '14 at 16:45
  • @Mr.Alien man the problem is i can't find any pure js plugin to achieve this, i don't use any js library like jquery and company, i just need pure js or hacks, for example this one http://lea.verou.me/scripts/elastic-textarea/ it bumps on chrome :/ it's really bad one :( – itsme Jan 21 '14 at 16:47
  • @sbaaaang hmmmm, anyways that was just my personal suggestion :) also, as far as User Interface goes, this is really uncomfy. – Mr. Alien Jan 21 '14 at 16:48
  • yeah i'm gonna accept your answer man but please put a better link for js plugin :D that is awful really :D @Mr. Alien – itsme Jan 21 '14 at 16:49
  • thanks @eliospizzaman your trick is awesome i'll try something , just the answer was about css so i can't accept your answer cause it's html5 but really really thanks – itsme Jan 21 '14 at 16:50
  • @sbaaaang lol accept this answer as it suffices your needs, but I was just suggesting you some standard way nothing else :) but anyways if I got pure JS solution, I will share for sure – Mr. Alien Jan 21 '14 at 16:50
  • @Mr.Alien lol that "if i got" you know ti means you'll never right? :D i'll accept your when plugin comes up lol , i can't accept this answer is not about css – itsme Jan 21 '14 at 16:51
  • @Mr.Alien lol and... don't forget ... "lightweight and well written one" :D ahah – itsme Jan 21 '14 at 16:52
  • 2
    @sbaaaang can you get shorter than [this](http://stackoverflow.com/a/7745840/1542290)? – Mr. Alien Jan 21 '14 at 16:59
  • @Mr.Alien not really :D accepted answer man, put that link inside it's really good resource and thanks a ton – itsme Jan 21 '14 at 17:00
  • @sbaaaang done already :) and you welcome ... – Mr. Alien Jan 21 '14 at 17:02
  • Ah, it started with CSS-only solution and almost ended with a new form plugin! :-) If @sbaaaang (it seems he is a very experienced member of the community) has entered a few more links for examples, it could have saved a few hours of chat (and a few hours of you guys) :-) – Minister Jan 21 '14 at 17:16
  • @Minister nope man :D the answer is clear i can reproduce in pure CSS so we ended up with a good js resource, but the answer is clear to me :) – itsme Jan 22 '14 at 08:24