The fitty function returns a single or multiple Fitty instances depending on how it's called. If you pass a query selector it will return an array of Fitty instances, if you pass a single element reference you'll receive back a single Fitty instance.
For optimal performance add a CSS selector to your stylesheet that sets the elements that will be resized to have white-space:nowrap and display:inline-block. If not, Fitty will detect this and will have to restyle the elements automatically, resulting in a slight performance penalty.
Suppose all elements that you apply fitty to are assigned the fit class name, add the following CSS selector to your stylesheet:
```css
.fit {
display: inline-block;
white-space: nowrap;
}
```
Should you only want to do this when JavaScript is available, add the following to the `
` of your web page.
``` html
<script>
document.documentElement.classList.add('js');
</script>
```
And change the CSS selector to:
```css
.js .fit {
display: inline-block;
white-space: nowrap;
}
```
Do Not Upscale Text
Fitty calculates the difference in width between the text container and its parent container. If you use CSS to set the width of the text container to be equal to the parent container it won't scale the text.
This could be achieved by forcing the text container to be a block level element with display: block !important or setting its width to 100% with width: 100%.
Custom Fonts
Fitty does not concern itself with custom fonts. But it will be important to redraw Fitty text after a custom font has loaded (as previous measurements are probably incorrect at that point).
If you need to use fitty on browsers that don't have CSS Font Loading support (Edge and Internet Explorer) you can use the fantastic FontFaceObserver by Bram Stein to detect when your custom fonts have loaded.
See an example custom font implementation below. This assumes fitty has already been called on all elements with class name fit.
``` html
<style>
/* Only set the custom font if it's loaded and ready */
new FontFaceObserver('Oswald').load().then(redrawFitty);
};
document.body.appendChild(s);
}
// Does the current browser support the CSS Font Loading API?
if ('fonts' in document) {
native();
} else {
fallback();
}
})();
</script>
```
Notes
- Will not work if the fitty element is not part of the DOM.
- If the parent element of the fitty element has horizontal padding the width calculation will be incorrect. You can fix this by wrapping the fitty element in another element.
``` html
<div style="padding-left:100px">
<h1 class="fit">I'm a wonderful heading</h1>
</div>
```
``` html
<div style="padding-left:100px">
<div><h1 class="fit">I'm a wonderful heading</h1></div>