Automatic text resizing in JavaScript / HTML

How to implement auto-resizing for a rich-text WYSIWYG editor

By Alex Exarhos, September 4, 2021

Automatic text resizing on Ellacard

All sorts of websites are set up for you to enter text, whether it’s making a Facebook post, editing a Wikipedia article, or writing a review for your favorite website. The vast majority of websites use a simple text box to accept input. You can’t see rich formatting in that text box, like bold / italic / underline. Even websites that are designed to take your text input and produce a visual output, like a photo website that allows you to enter captions (or some of our competitors), don’t let you edit formatted text directly, you have to enter it into a box, then preview it separately.

There’s a term for this — WYSIWYG (what you see is what you get). It’s a much better user experience to be able to edit formatted text directly, rather then entering plain text in one place and having to preview it in another with formatting / line-wrapping applied. Word processing applications have been doing this since the 90s, but it’s a lot harder to do on a website.

Wikipedia’s default text editor is not WYSIWYG — you enter the raw text and special formatting characters as plain text then preview your changes as an additional step

At Ellacard, we even introduce another compounding factor. Many of our inputs are constrained: you only have a certain amount of room in which to type your message on a templated card or in a video title for example. Try to enter a message in this card. You’ll see that not only can you edit your message directly on the card in a fancy cursive font, but it also automatically shrinks your text if you type too much (to a pre-defined minimum visible size).

There’s an aside here that I wanted to call out — you can see how challenging this is as there are still bugs even in well-established, popular, user-friendly programs like iMovie. Enter a title and apply a zoom effect, and you’ll see the line-wrapping change and shuffle the text around as it zooms, making it much harder to read and I’m sure not as intended! Our video titles don’t have this problem, FYI.

iMovie unexpected text wrapping behavior

This was not easy to get right! For the non-software developers out there, hopefully this post will give you better appreciation of tools on and off the web that have gone the extra mile to avoid the text preview step. For those interested in seeing some code, please read on!

How to implement auto-resizing for a rich-text WYSIWYG editor

First let’s set up a text input in our HTML. We use a content-editable div instead of a text area so we can apply the rich text styles to it. Note that uniform text rendering across browsers is actually an extremely difficult problem. Ellacard actually strips native bold text and applied a custom pseudo-bold class that simply outlines the text in order to be consistent, but we won’t get into that here.

<style type="text/css">
#container {
background-color: lightgrey;
position: relative;
display: table-cell;
vertical-align: middle;
}
#input {
text-align: center;
word-wrap: normal;
}
</style>...<div id="container">
<div id="input" contenteditable="true">This text will automatically resize as it is edited</div>
</div>

The HTML is simple. We can apply our own styles to that div to adjust things like font, shadow, and color. But the important part is our JavaScript change handler whose sole purpose is to update the fontSize property by measuring the input.

There are a couple of magic numbers here. We need to set the size of the container (width and height) and the maximum (default) and minimum font sizes. The general approach is this: any time there is input to that text box, check to see if it is now taller or wider than its parent. If so, reduce the font size and check again, all the way down until we reach our minimum font size.

<script type="text/javascript">
const MAX_WIDTH = 400;
const MAX_HEIGHT = 300;
const MAX_FONT_SIZE = 120;
const MIN_FONT_SIZE = 12;function onChange(e) {
const el = e.target;
let fontSize = MAX_FONT_SIZE;
el.style.fontSize = fontSize + 'px';
while (fontSize > MIN_FONT_SIZE && (el.scrollHeight > MAX_HEIGHT || el.scrollWidth > MAX_WIDTH)) {
fontSize--;
el.style.fontSize = fontSize + 'px';
}
}function onLoad() {
const container = document.getElementById('container');
container.style.width = MAX_WIDTH + 'px';
container.style.height = MAX_HEIGHT + 'px';const input = document.getElementById('input');
input.style.fontSize = MAX_FONT_SIZE + 'px';
input.addEventListener('input', onChange);
input.focus();
onChange({ target: input });
}window.addEventListener('load', onLoad);
</script>

Here’s a fiddle, try it for yourself: https://jsfiddle.net/r6fxu2w8/