Skip to content

Instantly share code, notes, and snippets.

@kcak11
Last active November 19, 2018 05:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcak11/4f992947bed0bed1194600f1876d9f60 to your computer and use it in GitHub Desktop.
Save kcak11/4f992947bed0bed1194600f1876d9f60 to your computer and use it in GitHub Desktop.
Elastic TextArea

Elastic TextArea

Elastic TextArea

An Elastic TextArea component which resizes automatically based on the content entered in the textarea.

A Pen by K.C.Ashish Kumar on CodePen.

License.

<textarea elastic placeholder="Add/Remove lines of text to auto-resize this textarea"></textarea>
<!--
© 2018 https://kcak11.com / https://ashishkumarkc.com
-->
/*
© 2018 https://kcak11.com / https://ashishkumarkc.com
*/
(function() {
var doElastic = function(elem) {
elem.style.height = elem.getAttribute("default-height") + "px";
elem.style.height = (elem.scrollHeight + 5) + "px";
};
[].forEach.call(document.querySelectorAll("textarea[elastic]"), function(et) {
et.setAttribute("rows", "1");
et.style.resize = "none";
et.style.overflow = "hidden";
et.style.boxSizing = "border-box";
et.setAttribute("default-height", et.offsetHeight);
doElastic(et);
et.addEventListener("input", function() {
doElastic(et);
}, false);
});
}());
/*
© 2018 https://kcak11.com / https://ashishkumarkc.com
*/
textarea {
font-family: Verdana;
border-top: none;
border-left: none;
border-right: none;
border-bottom: 2px solid #000;
background-color: #bcd9ed;
width: 50%;
outline: none;
font-size: 22px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment