View on GitHub

textStretch.js

A javascript function and a jQuery extension for maximizing font sizes to fill out the element's width.

textStretch.js

A javascript function and a jQuery extension for maximizing font sizes to fill out the element's width.

textStretch.js is minimal, fast and robust. It doesn't require or add any wrappers or child-elements, and it adapts to when you resize the window or flip your device.

textStretch is build with older browsers in mind. It's tested and works with Chrome, Safari, Firefox and Internet Explorer (I haven't tested any version older than 6 though).

jQuery version

The rest of this documentation will cover the jQuery-version. Click here to go to the javascript-version documentation.

Download

minified
uncompressed

How to use (jQuery version)

Load jQuery (1.7 or later) and the script

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="jquery.textStretch.min.js"></script>

...and apply textStretch to your element(s)

$(".stretch").textStretch();

It's that easy!

Arguments

There are two optional argumets: maxFontSize and minFontSize. They're both disabled (0) by default.

This is how they're used

// minimum font size
$(".stretch").textStretch({minFontSize: 12});
// min & max font-size
$(".stretch").textStretch({minFontSize: 12, maxFontSize: 60});

Changing the defaults

The default min & max font-sizes can be changed this way

// setting default max font size
$.textStretch.defaults.maxFontSize = 100;

Future calls will use the new default

// max font size will be 100 pixels, as defined above
$(".stretch").textStretch();

Unless...

// disable maxFontSize for this call
$(".stretch-no-max").textStretch({maxFontSize: 0});

Good to know