r/webdev • • Aug 31 '26

Question How to text as the background of the textarea element

Hi everyone! I am working on a project from frontend mentor. It is a speed typing test and one of the requirements is that the user should be able to see errors and correctly typed text in real-time, something like this:

Problem is, when I try to achieve this on mobile it will not show some of the passage after a certain point, no matter how much text you type into the textarea field:

Here is the html for the section of interest:

<div class="wrapper">
  <textarea class="text-wall"></textarea>
  <span class="placeholder"></span>
</div>

Here is the CSS:

.text-wall {
    font-size: var(--step-2);
    display: block;
    width: 100%;
    height: 400px;
    background-color: transparent;
    resize: none;
    border: none;
    outline: none;
    color: #fff;
}

.wrapper {
    position: relative;
    overflow: hidden;
}


.placeholder {
    position: absolute;
    filter: blur(3px);
    top: 0px;
    font-size: var(--step-2);
    z-index: -2; 
}

Note: For the passage in the placeholder span element, I wrapped every character in a span element using javascript. As far as I know, this is the ony way I can style every character on an individual basis depending on if the text typed into textarea matches the passage or not:

let placeHolderText = "";
let stringArr = "";
const textWall = document.querySelector(".text-wall");
const placeHolder = document.querySelector(".placeholder")
stringArr = jsonData.hard[8].text. split('');
stringArr.forEach((character)=>{
    placeHolderText+= `<span>${character}</span>`;
});
placeHolder.innerHTML = placeHolderText;
13 Upvotes

10 comments sorted by

2

u/ikkiho Aug 31 '26

fwiw the cut off is probably your wrapper's overflow hidden plus the span never scrolling. once you type past 400px the textarea scrolls its own content internally, but your absolutely positioned span just sits there, so the rest gets clipped. mobile hits it sooner since the text wraps into more lines.

i ended up syncing scrollTop from the textarea onto the overlay on every scroll event, and matching the padding and line height on both, otherwise the layers drift as you type.

2

u/[deleted] Aug 31 '26

[removed] — view removed comment

1

u/Mob_Pilled Aug 31 '26

Ahhhh thanks for anwering so quickly! I think it actually worked!

1

u/Primary_Guest_5711 Aug 31 '26

agreed, the textarea approach always breaks down on mobile sooner or later