Skip to content

Instantly share code, notes, and snippets.

@yckart
Forked from 140bytes/LICENSE.txt
Last active April 4, 2019 15:02
Show Gist options
  • Save yckart/6440596 to your computer and use it in GitHub Desktop.
Save yckart/6440596 to your computer and use it in GitHub Desktop.
CSS
css(document.body, {
    fontSize: 72,
    opacity: '0.5',
    background: 'red'
});
function (
a, // a single dom-node
b, // an object containing your properties and values
c // value-placholder
) {
for (var d in b) // iterate over the css-object
a.style[d] = // add the property
(c = b[d]) + // assign the value
// if the property is none of this:
// columnCount|fillOpacity|fontWeight|lineHeight|opacity|order|orphans|widows|zIndex|zoom/
(!/columnC|fillOpac|fontW|lineH|opac|order|orph|wido|zI|zo/i.test(d)
&& // and
+c === c // it is a number
? 'px' // append `'px'` as default unit
: '' // otherwise append nothing
);
}
function(a,b,c){for(var d in b)a.style[d]=(c=b[d])+(!/columnC|fillOpac|fontW|lineH|opac|order|orph|wido|zI|zo/i.test(d)&&+c===c?'px':'');}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Yannick Albert <http://yckart.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "CSS",
"description": "Set one or more CSS properties for every matched element.",
"keywords": [
"style",
"css",
"object",
"dom",
"node"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b style="font-size: 72px; opacity: 0.5; background: red">140byt.es</b></div>
<div>Actual value: <b id="ret">140byt.es</b></div>
<script>
var css = function(a,b,c){for(var d in b)a.style[d]=(c=b[d])+(!/columnC|fillOpac|fontW|lineH|opac|order|orph|wido|zI|zo/i.test(d)&&+c===c?'px':'');};
css(document.getElementById('ret'), {
fontSize: 72,
opacity: '0.5',
background: 'red'
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment