Custom Property Brain Twisters

I am part of that 82% that got the answer to Lea Verou’s quiz wrong. Stephen Shaw posted a similar quiz as well and it’s a fun exercise sharpen your CSS chops.


The post Custom Property Brain Twisters appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier

I am part of that 82% that got it wrong in Lea’s quiz (tweet version).

Here’s the code:

:root {
  --accent-color: skyblue;
}

div {
  --accent-color: revert; 
  background: var(--accent-color, orange);
}

So what background do I expect <div> to have?

My brain goes like this:

  1. Well, --accent-color is declared, so it’s definitely not orange (the fallback).
  2. The value for the background is revert, so it’s essentially background: revert;
  3. The background property doesn’t inherit though, and even if you force it to, it would inherit from the <body>, not the root.
  4. So… transparent.

Nope.

Lea:

[Because the value is revert it] cancels out any author styles, and resets back to whatever value the property would have from the user stylesheet and UA stylesheet. Assuming there is no --accent-color declaration in the user stylesheet, and of course UA stylesheets don’t set custom properties, then that means the property doesn’t have a value.

Since custom properties are inherited properties (unless they are registered with inherits: false, but this one is not), this means the inherited value trickles in, which is — you guessed it — skyblue.

Stephen posted a similar quiz the other day:

Again, my brain does it totally wrong. It goes:

  1. OK, well, --color is declared, so it’s not blue (the fallback).
  2. It’s not red because the second declaration will override that one.
  3. So, it’s essentially like p { color: inherit; }.
  4. The <p> will inherit yellow from the <body>, which it would have done naturally anyway, but whatever, it’s still yellow.

Nope.

Apparently inherit there is actually inheriting from the next place up the tree that sets it, which html does, so green. That actually is now normal inheriting works. It’s just a brain twister because it’s easy to conflate color the property with --color the custom property.

It also might be useful to know that when you actually declare a custom property with @property you can say whether you want it to inherit or not. So that would change the game with these brain twisters!

@property --property-name {
  syntax: '<color>';
  inherits: false;
  initial-value: #c0ffee;
}

The post Custom Property Brain Twisters appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier


Print Share Comment Cite Upload Translate Updates
APA

Chris Coyier | Sciencx (2021-06-25T18:39:46+00:00) Custom Property Brain Twisters. Retrieved from https://www.scien.cx/2021/06/25/custom-property-brain-twisters/

MLA
" » Custom Property Brain Twisters." Chris Coyier | Sciencx - Friday June 25, 2021, https://www.scien.cx/2021/06/25/custom-property-brain-twisters/
HARVARD
Chris Coyier | Sciencx Friday June 25, 2021 » Custom Property Brain Twisters., viewed ,<https://www.scien.cx/2021/06/25/custom-property-brain-twisters/>
VANCOUVER
Chris Coyier | Sciencx - » Custom Property Brain Twisters. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/25/custom-property-brain-twisters/
CHICAGO
" » Custom Property Brain Twisters." Chris Coyier | Sciencx - Accessed . https://www.scien.cx/2021/06/25/custom-property-brain-twisters/
IEEE
" » Custom Property Brain Twisters." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/06/25/custom-property-brain-twisters/. [Accessed: ]
rf:citation
» Custom Property Brain Twisters | Chris Coyier | Sciencx | https://www.scien.cx/2021/06/25/custom-property-brain-twisters/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.