First of all, we need to measure the width of element with its current font size and
text content. For more information, you can take a look at this
post.
const measureWidth = function(text, font) {
...
};
const ele = document.getElementById('headline');
const styles = window.getComputedStyle(ele);
const font = styles.font;
const fontSize = parseInt(styles.fontSize);
const measured = measureWidth(ele.textContent, font);
const scaleFontSize = Math.floor(scale * fontSize);
ele.style.fontSize = `${scaleFontSize}px`;