Вот 100% способ сделать это.
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; */
}
Итак, в основном здесь происходит то, что мы заполняем содержимое текстовой области и другого брата (в данном случае span) тем же контентом. Стили текстовой области обновляются, чтобы иметь тот же шрифт, отступы и т. Д., Что и у div. В зависимости от ваших конкретных потребностей вы просто обновляете их по своему усмотрению. Но это очень важно, чтобы они совпадали, так что расстояние и расположение идентичны.
Затем мы устанавливаем некоторые свойства для текстовой области, чтобы сделать ее абсолютной и принять размеры родительского элемента. Наконец, содержимое .textarea-mimic > span
установлено на visibility: hidden
. Это позволяет заполнять размеры, показывая только текстовую область. Отключите свойство видимости и display: none
текстовой области, чтобы увидеть его в действии.
Также обратите внимание, что если вы хотите, чтобы это обновлялось в режиме реального времени, некоторый простой javascript, который обновляет содержимое скрытого текста на основе текстовой области, должен сделать его динамичным.