Install Fitty
gh-pagesYou can install Fitty via npm to use it in your project.
npm install fitty --saverepository·gh-pages·Indexed 26 days ago
https://github.com/rikschennink/fittyA JavaScript library (version 2.4.2) that snugly resizes text to fit its parent container. It provides options for minSize, maxSize, multiLine wrapping, and mutation observation, along with instance methods like fit(), freeze(), unfreeze(), and unsubscribe() to control the resizing behavior.
You can install Fitty via npm to use it in your project.
npm install fitty --saveTo use Fitty, pass an element reference or a query selector to the fitty function. For optimal performance, include the script just before the closing </body> element.
If you pass a query selector, it returns an array of Fitty instances. If you pass a single element reference, it returns a single Fitty instance.
<div id="my-element">Hello World</div>
<script src="fitty.min.js"></script>
<script>
fitty('#my-element');
</script>The fitty method accepts an options object to customize behavior:
| Option | Default | Description |
|---|---|---|
minSize | 16 | The minimum font size in pixels |
maxSize | 512 | The maximum font size in pixels |
multiLine | true | Wrap lines when using minimum font size |
observeMutations | MutationObserverInit | Rescale when element contents is altered. Defaults to { subtree: true, childList: true, characterData: true } if supported. |
Example usage:
fitty('#my-element', {
minSize: 12,
maxSize: 300,
});Each Fitty instance provides methods to control resizing and properties to access the underlying element.
Methods:
fit(options): Force a redraw of the current fitty element. Use { sync: true } for a synchronous refit.freeze(): Stop updating this fitty on changes.unfreeze(): Resume updates to this fitty.unsubscribe(): Remove the fitty element from the redraw loop and restore it to its original state.Properties:
element: Reference to the related DOM element.Example:
var fitties = fitty('.fit');
// get element reference of first fitty
var myFittyElement = fitties[0].element;
// force refit
fitties[0].fit();
// force synchronous refit
fitties[0].fit({ sync: true });
// stop updating this fitty and restore to original state
fitties[0].unsubscribe();