# dom-css

> fast dom CSS styling

Latest version **2.1.0** (published 2016-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install dom-css
pnpm add dom-css
yarn add dom-css
bun add dom-css
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2016-08-09 |
| First published | 2014-12-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/dom-css) |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 152 |
| Author | Matt DesLauriers |
| Maintainers | mattdesl |
| Keywords | dom, css, style, sheet, animate, element, px, pixels |

## Links

- npm: https://www.npmjs.com/package/dom-css
- Repository: https://github.com/mattdesl/dom-css
- Issues: https://github.com/mattdesl/dom-css/issues
- npm.io page: https://npm.io/package/dom-css

## Dependencies (3)

- [prefix-style](https://npm.io/package/prefix-style.md) 2.0.1
- [to-camel-case](https://npm.io/package/to-camel-case.md) 1.0.0
- [add-px-to-style](https://npm.io/package/add-px-to-style.md) 1.0.0

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2016-08-09
- 2.0.1 — 2016-07-14
- 2.0.0 — 2015-11-30
- 1.1.2 — 2015-07-07
- 1.1.1 — 2015-02-09
- 1.1.0 — 2015-02-06
- 1.0.6 — 2015-01-16
- 1.0.5 — 2015-01-16
- 1.0.4 — 2014-12-07
- 1.0.3 — 2014-12-07
- 1.0.2 — 2014-12-07
- 1.0.1 — 2014-12-06
- 1.0.0 — 2014-12-06

## README

# dom-css

[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)

Small module for fast and reliable DOM styling. 

- normalizes for camel and dash case (see [to-camel-case](https://www.npmjs.com/package/to-camel-case))
- detects vendor prefixes as necessary, cached for performance (see [prefix-style](https://github.com/mattdesl/prefix-style))
- converts numbers to `px` strings for common properties (see [add-px-to-style](https://www.npmjs.com/package/add-px-to-style))

```js
var css = require('dom-css')

//set a style
css(element, 'position', 'absolute')

//will be set as "WebkitFontSmoothing" on Chrome
css(element, 'font-smoothing', 'none')

//set multiple styles
css(element, {
  // can be camel or dash case
  'background-color': 'blue',

  // you can use numbers to auto-"px"
  left: 25, 
  top: 0,
  marginTop: 0,
  position: 'absolute',
  
  // certain props will not have "px" added
  opacity: 0.5
})

//get the current style
css.get(element, 'position') 
// -> 'absolute'

css.get(element, ['left', 'marginTop']) 
// -> { left: '25px', marginTop: '0px' }
``` 

**Note:** The `get()` method does not *compute* an element's style, it only fetches the currently set inline style.

## Usage

[![NPM](https://nodei.co/npm/dom-css.png)](https://nodei.co/npm/dom-css/)

#### `css(element, property, value)`
#### `css.set(element, property, value)`

Styles an element with the css `property` (dash or camel case) and a given value. `value` is a string, or a number to be suffixed with `'px'` (special cases, see below). 

#### `css(element, styles)`
#### `css.set(element, styles)`

A shorthand for setting multiple styles, where `styles` is an object containing `property:value` pairs. 

#### `css.get(element, prop)`

Gets the inline style of element, where `prop` is a string (like `"borderRadius"`) or an array of strings. If an array of strings is given, an object is returned with key-value pairs representing the specified properties.

```js
css.get(div, ['width', 'height'])
//=> { width: '20px', height: '40px' }
```

This does not provide the *computed* style, only the current inline style.

#### auto px

If a number is specified, the value will have `"px"` added to it, *unless* it is a special unitless property like `'opacity'` and `'zIndex'`. See the full list in [add-px-to-style](https://www.npmjs.com/package/add-px-to-style) (sourced from React).

## Changelog

- `2.x`
  - formatted to [standard](https://npmjs.com/package/standard) code style
  - updates to latest `prefix-style`, since `'Khtml'` prefix has long been obsolete
  - now all properties are suffixed with "px" except a few like `opacity`, `zIndex`, etc. The list is sourced from React and maintained in another module.
- `1.x` - initial version which had a list of properties to be suffixed with "px"

## License

Special thanks to Paul Irish's gist for the prefix detection (now part of Modernizr). 

MIT, see [LICENSE.md](http://github.com/mattdesl/dom-css/blob/master/LICENSE.md) for details.

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