# update-input-width

> A function that given input element, updates its width to fit its content.

Latest version **2.0.0** (published 2025-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install update-input-width
pnpm add update-input-width
yarn add update-input-width
bun add update-input-width
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2025-03-20 |
| First published | 2019-05-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 15 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 4 |
| Author | Wojciech Maj |
| Maintainers | wojtekmaj |
| Keywords | input width |

## Links

- npm: https://www.npmjs.com/package/update-input-width
- Repository: https://github.com/wojtekmaj/update-input-width
- Homepage: https://github.com/wojtekmaj/update-input-width#readme
- Issues: https://github.com/wojtekmaj/update-input-width/issues
- Funding: https://github.com/wojtekmaj/update-input-width?sponsor=1
- npm.io page: https://npm.io/package/update-input-width

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2025-03-20
- 1.4.2 — 2023-10-18
- 1.4.1 — 2023-05-29
- 1.4.0 — 2023-05-29
- 1.3.1 — 2023-02-02
- 1.3.0 — 2023-01-09
- 1.2.2 — 2021-08-13
- 1.2.1 — 2020-02-02
- 1.2.0 — 2019-12-29
- 1.1.1 — 2019-08-12
- 1.1.0 — 2019-08-01
- 1.0.1 — 2019-05-03
- 1.0.0 — 2019-05-03

## README

[![npm](https://img.shields.io/npm/v/update-input-width.svg)](https://www.npmjs.com/package/update-input-width) ![downloads](https://img.shields.io/npm/dt/update-input-width.svg) [![CI](https://github.com/wojtekmaj/update-input-width/actions/workflows/ci.yml/badge.svg)](https://github.com/wojtekmaj/update-input-width/actions)

# Update-Input-Width

A function that given an input element, updates its width to fit its content.

## tl;dr

- Install by executing `npm install update-input-width` or `yarn add update-input-width`.
- Import by adding `import updateInputWidth from 'update-input-width'`.
- Use it by calling it with input element as an argument.

## User guide

### `updateInputWidth(element: HTMLInputElement)`

A function that given an input element, updates its width to fit its content by setting inline `width` CSS property.

#### Sample usage

```ts
import updateInputWidth from 'update-input-width';

updateInputWidth(myInput); // 42
```

or

```ts
import { updateInputWidth } from 'update-input-width';

updateInputWidth(myInput); // 42
```

### `getFontShorthand(element: HTMLElement)`

A function that given HTML element returns font CSS shorthand property. Equal to Chrome-only code:

```ts
window.getComputedStyle(element).font;
```

#### Sample usage

```ts
import { getFontShorthand } from 'update-input-width';

getFontShorthand(myInput); // 'normal normal 600 normal 20px / 25px Arial, sans-serif'
```

### `measureText(text: string, font: string)`

A function that given text and font CSS shorthand property returns text width in pixels.

#### Sample usage

```ts
import { measureText } from 'update-input-width';

measureText('hello', 'normal normal 600 normal 20px / 25px Arial, sans-serif'); // 42
```

## License

The MIT License.

## Author

<table>
  <tr>
    <td >
      <img src="https://avatars.githubusercontent.com/u/5426427?v=4&s=128" width="64" height="64" alt="Wojciech Maj">
    </td>
    <td>
      <a href="https://github.com/wojtekmaj">Wojciech Maj</a>
    </td>
  </tr>
</table>

---
_Source: https://npm.io/package/update-input-width · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
