Skip to content

Instantly share code, notes, and snippets.

@conofor
Created October 29, 2022 01:11
Show Gist options
  • Save conofor/f82922bfa7aec4b923f42b62c54398f2 to your computer and use it in GitHub Desktop.
Save conofor/f82922bfa7aec4b923f42b62c54398f2 to your computer and use it in GitHub Desktop.
textarea auto height
<textarea rows="1">Here is a very long value for testing auto height</textarea>
<pre><code class="language-js">document.querySelectorAll('textarea').forEach(el => {
el.style.height = el.setAttribute('style', 'height: ' + el.scrollHeight + 'px');
el.classList.add('auto');
el.addEventListener('input', e => {
el.style.height = 'auto';
el.style.height = (el.scrollHeight) + 'px';
});
});</code></pre>
document.querySelectorAll('textarea').forEach(el => {
el.style.height = el.setAttribute('style', 'height: ' + el.scrollHeight + 'px');
el.classList.add('auto');
el.addEventListener('input', e => {
el.style.height = 'auto';
el.style.height = (el.scrollHeight) + 'px';
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.17.1/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.17.1/plugins/autoloader/prism-autoloader.min.js"></script>
textarea {
resize: none;
&.auto {
overflow-y: hidden;
}
}
// Some styles
textarea {
-webkit-appearance: none;
box-sizing: border-box;
outline: none;
width: 100%;
max-width: 240px;
font-size: 14px;
font-family: 'Inter';
line-height: 22px;
padding: 16px 20px;
border-radius: 15px;
color: #404660;
border: none;
background: #fff;
transition: box-shadow .25s;
box-shadow: inset 0 0 0 1px var(--border-color, #E1E6F9), 0 0 0 3px var(--focus-color, transparent);
&:focus {
--focus-color: #ECEFFC;
}
&:focus,
&:hover {
--border-color: #BBC1E1;
}
}
body {
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: #F6F8FF;
}
pre {
margin: 60px 0 0 0;
padding: 20px 24px;
border-radius: 15px;
background: #171827;
code {
font-family: 'Fira Mono';
color: #BBC1E1;
line-height: 1.4;
font-size: 12px;
.token {
&.function {
color: #5C86FF;
}
&.string {
color: #F0B849;
}
&.parameter {
color: #CB7DD0;
}
&.punctuation {
color: #8A91B4;
}
}
}
}
<link href="https://fonts.googleapis.com/css?family=Inter:400,500,600,700&amp;display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Fira+Mono&amp;display=swap" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment