gengns ES

An entry from the gengns log, written by Génesis García Morilla · · CSS3, HTML5, JavaScript

Text Fill

You can easily resize text to make it fit into a container. The font size gets as big as possible. Open the example in JSFiddle, resize the result window and/or change the text into the div tag and rerun.

JavaScript

const textFill = $e => $e.style.fontSize = `${innerWidth / $e.textContent.length}px`

const $div = document.querySelector('div')
onresize = () => textFill($div)

textFill($div)

HTML

<div>The quick brown fox jumps over the lazy dog</div>

CSS

body {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

div {
  background-color: darkseagreen;
  padding: 10px;
  width: 45vw;
  text-align: center;
}
Open in JSFiddle

Tags: fill, resize, text, textFill