2.1.0 • Published 8 years ago

dom-css v2.1.0

Weekly downloads
167,587
License
MIT
Repository
github
Last release
8 years ago

dom-css

stable

Small module for fast and reliable DOM styling.

  • normalizes for camel and dash case (see to-camel-case)
  • detects vendor prefixes as necessary, cached for performance (see prefix-style)
  • converts numbers to px strings for common properties (see add-px-to-style)
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

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' }

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 (sourced from React).

Changelog

  • 2.x
    • formatted to 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 for details.

cognitiv-ui@scenify/react-custom-scrollbar@infinitebrahmanuniverse/nolb-dom-@everything-registry/sub-chunk-1499npm-recommendermiya-chatpenplotpreact-custom-scrollbarsmorphdeckbinder-web@cinic/react-custom-scrollbarsbrowser-lessonsbudo-ball-examplebookie-scrollbarcardo-paris@xuzhanhh/react-scroll@wt-ui/coreawesome-react-scrollbars@atomic-reactor/react-custom-scrollbarsapply-selector-and-cssacsg@whalecloud/ecommerce@whalecloud/common-login-pages@whalecloud/crm-5gv-bz-components@whalecloud/crm-common-components@whalecloud/crm-custc-components@whalecloud/crm-sic-components@whalecloud/crmbusiness@whalecloud/page-hub@webtechart/react-custom-scrollbarsyl-coreantler-one@aliyun-sls/react-custom-scrollbars@amazing-hiring/react-custom-scrollbarstruv-react-custom-scrollbars-2@spotware/react-custom-scrollbars@troglotit/react-custom-scrollbars@wizteam/react-custom-scrollbars-2@wteco/core@manychat/react-custom-scrollbars@meitner/react-custom-scrollbarskv-editorlevitech-spiritmnm-react-custom-scrollbarsreact-streamed-pixireact-streamed-textreact-touch-scrollbarsreact-rcsreact-scroll-containerreact-searcherreact-scrollbar-ie8react-sb3react-better-scrollbarsrc-custom-scrollbarsreact-custom-scrollbarsreact-custom-scrollbars-2react-custom-scrollbars-axsignaloceanreact-custom-scrollbars-betterreact-custom-scrollbars-cbxreact-custom-scrollbars-extendedreact-custom-scrollbars-fixreact-custom-scrollbars-for-styled-componentsreact-custom-scrollbars-forkreact-custom-scrollbars-mkellyreact-custom-scrollbars-patchedreact-custom-scrollbars-rcutreact-custom-scrollbars-rtlreact-custom-scrollbars-scrollbar-widthreact-custom-scrollbars-touchreact-custom-scrollbars-with-mac-supportreact-custom-scrollbars-with-mobilereact-custom-scrollbars-with-mobile-fixreact-customized-scrollbarsreactjs-custom-scrollbarreactive-pixireplace-custom-scrollbarsrahul-components-afrozrc-scrollbarstippsettings-panelsolid-custom-scrollbarssnaphy-custom-scrollbarsso-custom-scrollbarsspooky-elementspsvn-custom-scrollbarsseasun-designscrollbars-q210-patchedreact-ping-scrollbarstt-react-custom-scrollbars@tesq0/react-custom-scrollbars@kartikag01/react-custom-scrollbarsdom-freelinedom-css-mixindom-css-transform@lingyi/react-custom-scrollbarsf1-domdom-image-filecontrol-panelcooldom@influxdata/react-custom-scrollbars
2.1.0

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago