【问题标题】:Textarea with initial auto height by content with pure CSS具有纯 CSS 内容的初始自动高度的文本区域
【发布时间】:2018-04-27 05:19:18
【问题描述】:

我一直在寻找一种简单的 CSS 解决方案来使其内容的 texarea 匹配高度。

我不希望在您键入时自动调整大小的文本区域。我有一个文本区域,其中已经包含文本,我希望它与内容匹配。

有没有办法使用 CSS 做到这一点?

【问题讨论】:

  • 不,因为根据定义,textarea 的大小不根据其内容。
  • 如果文本已经在其中,为什么不根据该文本设置一个静态高度?
  • 我可以用代码做到这一点,但我在问是否可以使用 css。所有的证据都反对这一点,唉。
  • 您可以使用 CSS(即代码)设置高度。

标签: javascript jquery css


【解决方案1】:

不,因为根据定义,textarea 的大小不根据其内容。

但是,您可以使用 <div contenteditable="true"></div> 并将其样式设置为看起来和行为类似于 textarea

.textarea {border:1px solid #e0e0e0; max-height:100px; overflow-y:scroll;}
<div contenteditable="true" class="textarea">jdsklf ;askf; fs;dlfkj sad;flkasdj f;laskfj as;lfkajsd f;lasdkfj asl;dkfj sad;lfkasjd f;laskdjf a;sldfkj asdf;lkasdjf ;lasdkfj asd;lfkjsad f;laksdjf ;alsdkfjs ad;lfkjsad f;lksadjf ;lasdkfjasdl;fk jasdl;fkj asdf;lksadj f;lsadkfj sad;lfkjsd f;lksadjf; lsadkfjsda;lfk jsd;lfk jsdf</div>

【讨论】:

  • 这是一个很酷且有创意的解决方案。但是我宁愿写一些 JS 而不是设置一个 div 的样式,以使其看起来和行为与我已经设置了样式的 textarea 完全一样。不过谢谢,看来我得到了答案:)。
  • @BernardGritsmiller 您的问题看起来好像您不想要 JavaScript 解决方案 - 只有 CSS 解决方案。
  • @BernardGritsmiller JavaScript 解决方案会很困难,因为您无法获取文本的高度,然后将 textarea 设置为该高度。
  • 我真正想要的是一个 CSS 属性。哦,好吧。
  • @Geuis 要完全按照我的回答用 2 行代码所做的工作要做很多工作,并且需要复制内容(不是很干)。
【解决方案2】:

这是一个 100% 的 css 方法。

https://codepen.io/anon/pen/BxLMKK

请注意,对于这个演示,我做了一个简单的 2 列布局,只是为了提供上下文,但实际重要的部分只是在 .textbox-mimic div 中。

html:

<div class="container">
  <div>
    <div class="textarea-mimic">
      <span>
        Here's some text that's already here. Here's some text that's already here. Here's some text that's already here. Here's some text that's already here. Here's some text that's already here. Here's some text that's already here.  Here's some text that's already here. Here's some text that's already here. Here's some text that's already here. Here's some text that's already here.
      </span>
    <textarea>Here's some text that's already here. Here's some text that's already here. Here's some text that's already here. Here's some text that's already here. Here's some text that's already here. Here's some text that's already here.  Here's some text that's already here. Here's some text that's already here. Here's some text that's already here. Here's some text that's already here.
        </textarea>
    </div>
  </div>
  <div>
    Some things over here
  </div>
</div>

CSS:

.container {
  outline: 2px solid #000;
  display: flex;
  width: 500px;
  height: 100%;
  margin: 0 auto;
}

.container > div {
  flex: 1;
  outline: 1px solid #cc00cc;
  padding: 8px;
}

.container > div > .textarea-mimic {
  position: relative;
}
.container > div > .textarea-mimic > span {
  visibility: hidden;
}
.container > div > .textarea-mimic > textarea {
  width: 100%;
  height: 100%;
  overflow: hidden;
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  font: inherit;
  overflow: hidden;
  resize: none;
/*   display: none; */
}

所以基本上这里发生的事情是我们正在填充 textarea 的内容和具有相同内容的另一个兄弟(在本例中为 span)。文本区域的样式更新为与 div 具有相同的字体、填充等。根据您的特定需求,您只需根据需要更新这些内容。但关键是它们要匹配,以便间距和布局相同。

然后我们为 textarea 设置一些属性,使其位置绝对,并采用父级的尺寸。最后将.textarea-mimic &gt; span的内容设置为visibility: hidden。这允许在仅显示文本区域的同时填充尺寸。关闭可见性属性和 textarea 的 display: none 以查看它的实际效果。

另外请注意,如果您希望它实时更新,一些基于 textarea 更新隐藏文本内容的简单 javascript 应该使其动态化。

【讨论】:

  • 我尝试添加一些文本。 Textarea 没有自动调整大小。
猜你喜欢
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
相关资源
最近更新 更多