{"id":254179,"date":"2017-04-25T08:21:52","date_gmt":"2017-04-25T15:21:52","guid":{"rendered":"http:\/\/css-tricks.com\/?p=254179"},"modified":"2019-07-03T20:22:16","modified_gmt":"2019-07-04T03:22:16","slug":"now-css-custom-properties-thing-value-parts-can-changed-individually","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/now-css-custom-properties-thing-value-parts-can-changed-individually\/","title":{"rendered":"Now that CSS Custom Properties are a Thing, All Value Parts Can Be Changed Individually"},"content":{"rendered":"

In CSS, some properties have shorthand. One property that takes separated values. Syntactic sugar<\/em>, as they say, to make authoring easier. Take transition<\/code>, which might look something like:<\/p>\n

.element {\r\n  transition: border 0.2s ease-in-out;\r\n}<\/code><\/pre>\n

We could have written it like this:<\/p>\n

.element {\r\n  transition-property: border;\r\n  transition-duration: 0.2s;\r\n  transition-timing-function: ease-in-out;\r\n}<\/code><\/pre>\n

Every “part” of the shorthand value has its own property it maps to. But that’s not true for everything. Take box-shadow<\/code>:<\/p>\n

.element {\r\n  box-shadow: 0 0 10px #333;\r\n}<\/code><\/pre>\n

That’s not shorthand for other properties. There is no box-shadow-color<\/code> or box-shadow-offset<\/code>.<\/p>\n

That’s where Custom Properties come to save us!<\/p>\n

We could set it up like this:<\/p>\n

:root {\r\n  --box-shadow-offset-x: 10px;\r\n  --box-shadow-offset-y: 2px;\r\n  --box-shadow-blur: 5px;\r\n  --box-shadow-spread: 0;\r\n  --box-shadow-color: #333;\r\n}\r\n\r\n.element {\r\n  box-shadow:\r\n    var(--box-shadow-offset-x)\r\n    var(--box-shadow-offset-y)\r\n    var(--box-shadow-blur)\r\n    var(--box-shadow-spread)\r\n    var(--box-shadow-color);\r\n}<\/code><\/pre>\n

A bit verbose, perhaps, but gets the job done. <\/p>\n

Now that we’ve done that, remember we get some uniquely cool things:<\/p>\n

    \n
  1. We can change individual values with JavaScript. Like:\n
    document.documentElement.style.setProperty(\"--box-shadow-color\", \"green\");<\/code><\/pre>\n<\/li>\n
  2. Use the cascade, if we need to. If we set --box-shadow-color: blue<\/code> on any selector more specific than the :root, we’ll override that color.<\/li>\n<\/ol>\n

    Fallbacks are possible too, in case the variable isn’t set at all:<\/p>\n

    .element {\r\n  box-shadow:\r\n    var(--box-shadow-offset-x, 0)\r\n    var(--box-shadow-offset-y, 0)\r\n    var(--box-shadow-blur, 5px)\r\n    var(--box-shadow-spread, 0)\r\n    var(--box-shadow-color, black);\r\n}<\/code><\/pre>\n
    \n

    How about transforms? They are fun because they take a space-separated list of values, so each of them could be a custom property:<\/p>\n

    :root {\r\n  --transform_1: scale(2);\r\n  --transform_2: rotate(10deg);\r\n}\r\n\r\n.element{\r\n  transform: var(--transform_1) var(--transform_2);\r\n}<\/code><\/pre>\n
    \n

    What about elements that do<\/em> have individual properties for their shorthand, but also offer comma-separated multiple values? Another great use-case:<\/p>\n

    :root {\r\n  --bgImage: url(basic_map.svg);\r\n  --image_1_position: 50px 20px;\r\n  --image_2_position: bottom right;\r\n}\r\n\r\n.element {\r\n  background: \r\n    var(--bgImage) no-repeat var(--image_1_position),\r\n    var(--bgImage) no-repeat var(--image_2_position);\r\n}<\/code><\/pre>\n

    Or transitions?<\/p>\n

    :root {\r\n  --transition_1_property: border;\r\n  --transition_1_duration: 0.2s;\r\n  --transition_1_timing_function: ease;\r\n  \r\n  --transition_2_property: background;\r\n  --transition_2_duration: 1s;\r\n  --transition_2_timing_function: ease-in-out;\r\n}\r\n\r\n.element {\r\n  transition: \r\n    var(--transition_1_property) \r\n    var(--transition_1_duration) \r\n    var(--transition_1_timing_function),\r\n    var(--transition_2_property) \r\n    var(--transition_2_duration) \r\n    var(--transition_2_timing_function),\r\n}<\/code><\/pre>\n

    Dan Wilson recently used this kind of thing<\/a> with animations to show how it’s possible to pause individual animations! <\/p>\n


    \n

    Here’s browser support:<\/p>\n

    This browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>

    Desktop<\/h4>
    Chrome<\/span><\/th>Firefox<\/span><\/th>IE<\/span><\/th>Edge<\/span><\/th>Safari<\/span><\/th><\/tr><\/thead>
    49<\/span><\/td>31<\/span><\/td>No<\/span><\/td>16<\/span><\/td>10<\/span><\/td><\/tr><\/table><\/div>

    Mobile \/ Tablet<\/h4>
    Android Chrome<\/span><\/th>Android Firefox<\/span><\/th>Android<\/span><\/th>iOS Safari<\/span><\/th><\/tr><\/thead>
    142<\/span><\/td>144<\/span><\/td>142<\/span><\/td>10.0-10.2<\/span><\/td><\/tr><\/table><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"

    CSS Custom Properties give us the ability to reach into a property value and change certain parts of it. That’s useful in a bunch of places, but in particular, it’s useful in properties which don’t allow us to do it any other way.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0","_share_on_mastodon_status":"%title% %permalink%"},"categories":[4],"tags":[1036],"class_list":["post-254179","post","type-post","status-publish","format-standard","hentry","category-articles","tag-custom-properties"],"acf":{"show_toc":"No"},"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":16302,"url":"https:\/\/css-tricks.com\/things-it-might-be-funuseful-to-try-the-universal-selector-on\/","url_meta":{"origin":254179,"position":0},"title":"Things It Might Be Fun\/Useful to Try the Universal (*) Selector On","author":"Chris Coyier","date":"February 15, 2012","format":false,"excerpt":"What CSS properties might it make sense to apply to every single element on the page? We'll take a look at some that may (or may not) be a good idea.","rel":"","context":"In "Articles"","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2012\/02\/transition.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":294043,"url":"https:\/\/css-tricks.com\/contextual-utility-classes-for-color-with-custom-properties\/","url_meta":{"origin":254179,"position":1},"title":"Contextual Utility Classes for Color with Custom Properties","author":"Christopher Kirk-Nielsen","date":"August 14, 2019","format":false,"excerpt":"In CSS, we have the ability to access currentColor which is tremendously useful. Sadly, we do not have access to anything like currentBackgroundColor, and the color-mod() function is still a ways away. With that said, I am sure I am not alone when I say I'd like to style some\u2026","rel":"","context":"In "Articles"","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/08\/currentColor.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/08\/currentColor.png?fit=1200%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/08\/currentColor.png?fit=1200%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/08\/currentColor.png?fit=1200%2C600&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/08\/currentColor.png?fit=1200%2C600&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":383506,"url":"https:\/\/css-tricks.com\/positioning-text-around-elements-with-css-offset\/","url_meta":{"origin":254179,"position":2},"title":"Positioning Text Around Elements With CSS Offset","author":"Preethi","date":"January 24, 2025","format":false,"excerpt":"When it comes to positioning elements on a page, including text, there are many ways to go about it in CSS \u2014 the literal position property with corresponding inset-* properties, translate, margin, anchor() (limited browser support at the moment), and so forth. The offset property is another one that belongs\u2026","rel":"","context":"In "Articles"","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"The number four in large print with a label that says number four' along it's top-left edge.","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2025\/01\/css-offset-number-4.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2025\/01\/css-offset-number-4.jpg?fit=1200%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2025\/01\/css-offset-number-4.jpg?fit=1200%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2025\/01\/css-offset-number-4.jpg?fit=1200%2C600&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2025\/01\/css-offset-number-4.jpg?fit=1200%2C600&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":21913,"url":"https:\/\/css-tricks.com\/pizza-time-hovers\/","url_meta":{"origin":254179,"position":3},"title":"Pizza Time Hovers","author":"Chris Coyier","date":"June 10, 2013","format":false,"excerpt":"Hover Maester Jenn Lukas sent me a link to PizzaTime.com. We agreed that 1) those are some pretty neat hovers! and 2) it's pretty cool that there is a quality website at all at a domain like PizzaTime.com - in which that she typed in randomly hoping there would be.\u2026","rel":"","context":"In "Articles"","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":209210,"url":"https:\/\/css-tricks.com\/using-multi-step-animations-transitions\/","url_meta":{"origin":254179,"position":4},"title":"Using Multi-Step Animations and Transitions","author":"Geoff Graham","date":"December 10, 2015","format":false,"excerpt":"CSS animations are rad and the concept is fairly simple. Name the animation, define the movement in @keyframes and then call that animation on an element. If you haven't worked with them, you can level up on the syntax right here in the Almanac. While the concept is simple, there\u2026","rel":"","context":"In "Articles"","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":375412,"url":"https:\/\/css-tricks.com\/adding-box-shadows-to-wordpress-blocks-and-elements\/","url_meta":{"origin":254179,"position":5},"title":"Adding Box Shadows to WordPress Blocks and Elements","author":"Ganesh Dahal","date":"December 7, 2022","format":false,"excerpt":"The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.","rel":"","context":"In "Articles"","block_context":{"text":"Articles","link":"https:\/\/css-tricks.com\/category\/articles\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/11\/wordpress-box-shadow.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/11\/wordpress-box-shadow.jpg?fit=1200%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/11\/wordpress-box-shadow.jpg?fit=1200%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/11\/wordpress-box-shadow.jpg?fit=1200%2C600&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/11\/wordpress-box-shadow.jpg?fit=1200%2C600&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/254179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=254179"}],"version-history":[{"count":6,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/254179\/revisions"}],"predecessor-version":[{"id":292430,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/254179\/revisions\/292430"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=254179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=254179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=254179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}