Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alexcambose/fit-fontsize

Small library for fitting text perfectly into it's container.
https://github.com/alexcambose/fit-fontsize

box containers css css-selector fit font font-size javascript javascript-library text

Last synced: 20 days ago
JSON representation

Small library for fitting text perfectly into it's container.

Awesome Lists containing this project

README

        

# fit-fontsize
Small library for fitting text perfectly into it's container.

[**Demo**](https://alexcambose.github.io/fit-fontsize/)

## Install
Install using [npm](https://www.npmjs.com/package/fit-fontsize)
```
npm install -S fit-fontsize
```
Install using [bower](https://bower.io/)
```
bower install fit-fontsize
```

## Usage
### Html only
```html


Lorem ipsum dolor sit amet

```
Css like properties can be added to the attribute
```html

Lorem ipsum dolor sit amet

```
### Javascript
```html

Lorem ipsum dolor sit amet

fit.apply(document.getElementById('element'));

```
Fit only with the width of the parent and also watch for changes.
```html


Lorem ipsum dolor sit amet

fit.watch(document.getElementById('element'), { type: 'width' });

```
Each time *#element* changes it will recalculate it's font-size in order to fit.
## API

## Fit
**Kind**: global class

* [Fit](#Fit)
* [new Fit(attribute)](#new_Fit_new)
* [.apply(element, options)](#Fit+apply)
* [.watch(element, options)](#Fit+watch) ⇒ number
* [.stop(index)](#Fit+stop)

### new Fit(attribute)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| attribute | string | 'fit-fontsize' | Attribute that will be used to select the element that sould be fitted |

### fit.apply(element, options)
Resize

**Kind**: instance method of [Fit](#Fit)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| element | object | | The DOM element that needs the font to be resized so that it will fit it's container |
| options | object | | Resizing options |
| [options.type] | 'both' \| 'width' \| 'height' | 'both' | Type of the resize. |
| [options.ifParentBigger] | boolean | true | Resize text bigger until it fits perfectly |
| [options.ifParentSmaller] | boolean | true | Resize text smaller until it fits perfectly |

**Example**
```js
fit.apply(document.getElementById('elem'), { type: 'width' });
```

### fit.watch(element, options) ⇒ number
Calls apply each time the element changes

**Kind**: instance method of [Fit](#Fit)
**Returns**: number - Id of the watcher
**See**: [#apply](#apply)

| Param | Type | Description |
| --- | --- | --- |
| element | object | Same as the apply method |
| options | object | Same as the apply method |

**Example**
```js
fit.watch(document.getElementById('elem'), { type: 'height' }); // 0
```

### fit.stop(index)
Stops a watcher

**Kind**: instance method of [Fit](#Fit)

| Param | Type | Description |
| --- | --- | --- |
| index | number | Id of the watcher |

**Example**
```js
fit.stop(0);
```