dom-css
Small module for fast and reliable DOM styling.
- detects prefixes as necessary, cached for performance
- converts numbers to
px
strings for common properties - normalizes for camel and dash case
var css = require('dom-css')
css(element, 'position', 'absolute')
css(element, 'font-smoothing', 'none')
css(element, {
'background-color': 'blue',
left: 25,
top: 0,
marginTop: 0,
position: 'absolute'
})
css.get(element, 'position')
css.get(element, ['left', 'marginTop'])
See the special cases for a list of px
-suffixed properties (same list is used in GreenSock API).
Usage
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.
css.get(div, ['width', 'height']) => { width: "20px", height: "40px" }
Note: This does not provide the computed style!
special cases
The following properties are suffixed with 'px'
when their value is specified as a number.
top, right, bottom, left,
width, height, fontSize,
paddingLeft, paddingRight,
paddingTop, paddingBottom,
marginLeft, marginRight,
marginTop, marginBottom,
padding, margin, perspective
License
Special thanks to Paul Irish's gist for the prefix detection (now part of Modernizr).
MIT, see LICENSE.md for details.