ThisisaJavaScriptlibraryfor[escapingtextforuseinCSSstringsoridentifiers](https://mathiasbynens.be/notes/css-escapes) while generating the shortest possible valid ASCII-only output. [Here’s an online demo.](https://mothereff.in/css-escapes)
[ApolyfillfortheCSSOM`CSS.escape()`methodisavailableinaseparaterepository.](https://mths.be/cssescape) (In comparison, _cssesc_ is much more powerful.)
Bydefault,`cssesc` returns a string that can be used as part of a CSS string. If the target is a CSS identifier rather than a CSS string, use the `isIdentifier: true`setting(seebelow).
Thedefaultvalueforthe`isIdentifier` option is `false`. This means that the input text will be escaped for use in a CSS string literal. If you want to use the result as a CSS identifier instead (in a selector, for example), set this option to `true`.
```js
cssesc('123a2b');
// → '123a2b'
cssesc('123a2b',{
'isIdentifier':true
});
// → '\\31 23a2b'
```
####`quotes`
Thedefaultvalueforthe`quotes` option is `'single'`. This means that any occurences of `'` in the input text will be escaped as `\'`,sothattheoutputcanbeusedinaCSSstringliteralwrappedinsinglequotes.
```js
cssesc('Loremipsum"dolor"sit\'amet\'etc.');
// → 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.'
// → "Lorem ipsum \"dolor\" sit \\'amet\\' etc."
cssesc('Loremipsum"dolor"sit\'amet\'etc.',{
'quotes':'single'
});
// → 'Lorem ipsum "dolor" sit \\\'amet\\\' etc.'
// → "Lorem ipsum \"dolor\" sit \\'amet\\' etc."
```
IfyouwanttousetheoutputaspartofaCSSstringliteralwrappedindoublequotes,setthe`quotes` option to `'double'`.
```js
cssesc('Loremipsum"dolor"sit\'amet\'etc.',{
'quotes':'double'
});
// → 'Lorem ipsum \\"dolor\\" sit \'amet\' etc.'
// → "Lorem ipsum \\\"dolor\\\" sit 'amet' etc."
```
####`wrap`
The`wrap` option takes a boolean value (`true` or `false`), and defaults to `false` (disabled). When enabled, the output will be a valid CSS string literal wrapped in quotes. The type of quotes can be specified through the `quotes`setting.
```js
cssesc('Loremipsum"dolor"sit\'amet\'etc.',{
'quotes':'single',
'wrap':true
});
// → '\'Lorem ipsum "dolor" sit \\\'amet\\\' etc.\''
// → "\'Lorem ipsum \"dolor\" sit \\\'amet\\\' etc.\'"
cssesc('Loremipsum"dolor"sit\'amet\'etc.',{
'quotes':'double',
'wrap':true
});
// → '"Lorem ipsum \\"dolor\\" sit \'amet\' etc."'
// → "\"Lorem ipsum \\\"dolor\\\" sit \'amet\' etc.\""
```
####`escapeEverything`
The`escapeEverything` option takes a boolean value (`true` or `false`), and defaults to `false`(disabled).Whenenabled,allthesymbolsintheoutputwillbeescaped,evenprintableASCIIsymbols.
Theglobaldefaultsettingscanbeoverriddenbymodifyingthe`css.options` object. This saves you from passing in an `options` object for every call to `encode`ifyouwanttousethenon-defaultsetting.
```js
// Read the global default setting for `escapeEverything`:
cssesc.options.escapeEverything;
// → `false` by default
// Override the global default setting for `escapeEverything`:
cssesc.options.escapeEverything=true;
// Using the global default setting for `escapeEverything`, which is now `true`:
ThislibrarysupportstheNode.jsandbrowserversionsmentionedin[`.babelrc`](https://github.com/mathiasbynens/cssesc/blob/master/.babelrc). For a version that supports a wider variety of legacy browsers and environments out-of-the-box, [see v0.1.0](https://github.com/mathiasbynens/cssesc/releases/tag/v0.1.0).
##Author
|[![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |