JSFiddle - React, Tailwind, and code Playground

by Bhaumik Mehta

HTML

<div style="position: relative;">
    <label id="fixedLabel" for="textarea">Bhaumik Writes: </label>
    <textarea id="textarea" autofocus></textarea>
</div>

CSS

#fixedLabel {
    position: absolute;
    top: 3px;
    left: 3px;
    color: gray;
    z-index: 2;
    background-color: #fff;
}

#textarea {
  position: relative;
  z-index: 1;
}

#textarea, #fixedLabel {
    font-family: "Arial";
    font-size: 0.8em;
}

JavaScript

var textarea = document.getElementById("textarea");
var label = document.getElementById("fixedLabel");

textarea.style.textIndent = (label.clientWidth + 3) + "px";

var lastScrollTop = 0;
textarea.addEventListener("scroll", function(){
    var st = this.scrollTop;
    if (st > lastScrollTop){
        //if (label.style.top > 3) {
            label.style.top = ((label.style.top + 1) - 1) + 'px';
//        }
    } else {
      //if (label.style.top > 3) {
            label.style.top = (label.style.top + 1) + 'px';
        //}
    }
    lastScrollTop = st;
});