简体   繁体   English

流体宽度单线textarea

[英]Fluid width single line textarea

I'm trying to create a div with a fluid-width textarea inside it. 我正在尝试在其中创建一个带有流体宽度textarea的div。 The width of the div should be at least 3em, at most 12em, and otherwise the exact same width as the textarea. div的宽度应至少为3em,最多为12em,否则与textarea的宽度完全相同。 I've got this working. 我有这个工作。 See fiddle . 看小提琴

When the textarea fills up the div, it creates a line break rather than overflowing to the left, which is the effect I'm going for. 当textarea填满div时,它会创建一个换行而不是溢出到左边,这就是我想要的效果。 Any ideas how to achieve this? 任何想法如何实现这一目标?

Edit: This code is based on A List Apart's article on Expanding Text Areas . 编辑:此代码基于A List Apart关于扩展文本区域的文章

html HTML

<div><pre><span></span><br></pre>
  <textarea autofocus placeholder='Note'></textarea>
</div>

css CSS

div {
    border: 1px solid grey;
    position: relative;
    display: inline-block;
/*     overflow: hidden; */
    height: 1.3rem;
    min-width: 3rem;
    max-width: 12rem;
}
textarea {
    border: 1px solid blue;
    resize: none;
    background: rgba(0,0,255,.5);
    padding: 0;
    width: 100%;
}
textarea, pre {
    position: absolute;
    top: 0;
    right: 0;
    height: 100%;
    margin: 0;
/*     visibility: hidden; */
}
pre {
    border: 1px solid pink;
    position: relative;
    max-width: 100%;
}
* {
    font: 1rem arial;
}

js JS

var textarea = document.querySelector('textarea');
var span = document.querySelector('span');
textarea.addEventListener('input', function() {
  span.textContent = textarea.value;
});

In your CSS use: 在你的CSS使用中:

white-space: nowrap;
overflow: hidden;    

Edit this is deprecated: Can you set wrap="off" as an attribute on the textarea ? 编辑此内容已弃用:您可以将wrap="off"textarea上的属性吗?

edit: to say overflow: hidden; 编辑:说溢出:隐藏; (per comment below) original: overflow: auto; (以下评论)原文:overflow:auto;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM