i am schulz
A code snippet reading '<textarea syntax=highlighting></textarea>', marked up in bright colors.

A Colorful Textarea

Adding syntax highlighting to an input field can be a hard task. <textarea> supports neither styling of individual characters or words, nor HTML tags within itself, there is no fully supported native solution for that. Most editors work with contenteditable to actually render a fully marked up code snippet and let the user edit its content. This requires a lot of work to get it accessible (as in restore all the native functions of a textarea) and still adds a lot of complexity. If you don’t want that and are just looking for a quick, dead-simple solution: Here’s how to colorize a textarea.

Solution

The trick is to separate the input element from the displayed one. We can’t color the content of a textarea, but we can make it invisible and replace it with marked up content. This works with monospaced fonts and fonts with a uniform width across normal, bold and italic characters. I’m using this for code and markdown, so that’s perfectly acceptable for me. We also need to be careful to match the dimensions of the textarea exactly while only using font-relative units like em, to ensure that the highlight element scales well with the invisible textarea. The cursor is still in the textarea’s context, while the text itself is rendered in the highlight element. We want to match every character of the textarea to match the highlighted one on a pixel-perfect basis.

A 3D explosion schema of the layout. In the background is a greyed out textarea with a colored cursor after the last character. It's content is a code snippet of an empty html5 page. The foreground is the same text, but syntax-highlighted in bright colors. The cursor of the textarea reaches into the foreground.

I also need to auto-resize my textarea. Since textareas usually scroll vertically, that would mess up the position matching with the highlight element. Auto-resizing seems like a graceful workaround to me.

The highlghting itself would work with every code parser. I’m using highlight.js to convert markdown to syntax-highlighted HTML. I listen for content changes in the textarea and parse new rendered code on every input. To counter the worst performance hits, I’ll just use requestAnimationFrame. Debouncing isn’t an option here, because the user would only see what they’ve written after they’ve finished typing. That’d be very poor UX.

Demo

CodePen (ID: ZEXyqPr) Activating this Feature allows CodePen to place cookies and communicate with CodePen's servers.
(more info)

Note that this example also displays the rendered Markdown in a separate element. I’ll use the change listener that I already have to splice in a Markdown renderer: Showdown.

Pros

  • as accessible as a textarea
  • is a progressively enhanced feature
  • can be styled exactly to your needs
  • dead simple solution compared to a rich text editor

Cons

  • has performance issues with large texts (as do textareas in general)
  • works only with monospaced fonts
  • works only with auto-sizing textareas

This article was written in a textarea :)