JSFiddle - React, Tailwind, and code Playground

HTML

<div id="container">
  <div id="input" contenteditable="true">This text will automatically resize as it is edited</div>
</div>

CSS

#container {
  background-color: lightgrey;
  position: relative;
  display: table-cell;
  vertical-align: middle;
}
#input {
  text-align: center;
  /* Words that are too wide should shrink the font size instead of wrapping */
  word-wrap: normal;
}

JavaScript

const MAX_WIDTH = 400;
const MAX_HEIGHT = 300;
const MAX_FONT_SIZE = 120;
const MIN_FONT_SIZE = 12;

function onChange(e) {
  // For simplicity, we always start with the max font size and work our way down. This ensures that we also accomodate
  // the case where the font size should increase, like if we deleted some text. Note that we could make this more
  // efficient if needed, such as by starting with the initial font size and incrementing or decrementing from there,
  // or doing a binary search.
  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);