Skip to content

AndersDJohnson/css-safe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

css-safe

Make strings safe for CSS. Useful for data-driven CSS class names.

Transform to lowercase, replace spaces with dashes, and replace all other characters (but a-z) with their double-underscore-prefixed zero-padded-to-4-characters hexadecimal Unicode index.

For example, I use this on websites to add a CSS class to each page's <body> based on its title or URI, to allow targeting it uniquely.

Install

via npm:

$ npm install --save css-safe

Supports Node, AMD, browser global via UMD.

Use

var cssSafe = require('css-safe');

var safe;

safe = cssSafe('To do');
// 'to-do'

safe = cssSafe('Red, and blue.');
// 'red__002c-and-blue__0021'

About

Make strings safe for CSS.

Resources

License

Stars

Watchers

Forks

Packages

No packages published