Skip to content

Instantly share code, notes, and snippets.

@maxhoffmann
Created April 8, 2014 19:40
Show Gist options
  • Select an option

  • Save maxhoffmann/10178540 to your computer and use it in GitHub Desktop.

Select an option

Save maxhoffmann/10178540 to your computer and use it in GitHub Desktop.
fullText.js
function fullText() {
var elements = [].slice.call(document.querySelectorAll('[data-full-text]'));
elements.forEach(function(element) {
var REGEX_NUMBER = /\d+/;
var REGEX_NO_NUMBER = /[^\d]+/;
element.style.display = "inline";
element.style.fontSize = '';
var width = element.getBoundingClientRect().width;
var containerWidth = element.parentNode.getBoundingClientRect().width;
var fontSize = getComputedStyle(element).fontSize;
var fontSizeValue = fontSize.match(REGEX_NUMBER)[0];
var fontSizeUnit = fontSize.match(REGEX_NO_NUMBER)[0];
var newFontSize = (fontSizeValue/width)*containerWidth;
element.style.display = '';
element.style.fontSize = newFontSize + fontSizeUnit;
});
}
window.addEventListener('DOMContentLoaded', fullText);
window.addEventListener('resize', fullText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment