1

Keep in mind that this is not a duplicated question, to probe it go and read the http://www.w3.org/TR/css-variables/ if you read it says:

This version: http://www.w3.org/TR/2015/CR-css-variables-1-20151203/ If you read the date it says that is from 2015/12/03 so it's very recent, probably there is the confussion. The question that it says that was asked first does not work in the same way and was asked two years ago, e.g.(it uses a prefix -webkit- so it means that it wont work in all browsers that support the Native CSS Variables) The kind of variables i'm talking is something new, to understand why am i saying this, you need to read the articles of the links below, also my question cover another things that aren't answered in the question that was asked two years ago.

Also the second question below is updated now.


Maybe some people already know that there is a new implementation of variables for CSS, they bring a whole new way to write CSS code, at this moment the browser support is very low, but this have a great future.

I had read some information that explains more about CSS variables, so if you need info about it:

http://philipwalton.com/articles/why-im-excited-about-native-css-variables/

https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables

http://www.w3.org/TR/css-variables/

There are a few things that i still don't have clear:

Is there another method to change the values of a variable, that not be the media queries? e.g. using :hover or javascript?

Can i use variables to define Selectors?

Rodrigo Calix
  • 627
  • 1
  • 7
  • 16
  • Not sure what you are asking but I'm pretty sure the answer is *NO*. Perhaps you would clarify what it is you are asking. – Paulie_D Dec 19 '15 at 20:18
  • 1
    Native Variables have [horrible support anyway](http://caniuse.com/#feat=css-variables). I would recommend using SCSS or the like. – Jacob G Dec 19 '15 at 20:22
  • The Question has been updated now, Yeah the support is very low at this moment, Anyway SCSS variables are static when you compile them, Native CSS variables can change using media queries. – Rodrigo Calix Dec 19 '15 at 20:27
  • 1
    @RodrigoCalix yep, that's why the native CSS variables are awesome :D You just shouldn't let the awesomeness make you throw your browser support off a cliff. – Jacob G Dec 19 '15 at 20:28
  • @JacobGray Ha ha ha i know, this is a question for future use, it will help a lot of people and also will clear out my doubts about this topic. :D I'm not planning to use them right now but yes in the future. – Rodrigo Calix Dec 19 '15 at 20:33
  • I vote IE should be purged from the earth, for the sake of the progression of the web. – Jacob G Dec 19 '15 at 20:39
  • @JacobGray Interesting fact: Edge is considering implement CSS Variables, http://caniuse.com/#feat=css-variables , Coding Would be more awesome if the people catch at day. – Rodrigo Calix Dec 19 '15 at 20:47

1 Answers1

3
  • Is there another method to change the values of a variable, that not be the media queries and selectors?

    I assume you mean changing the value of a CSS variable outside of a rule set.

    Yes, you can. You can assign a CSS variable through the style property and the setProperty or setPropertyValue methods.

    document.body.style.setProperty('--bg', 'red');
    body {
      background: var(--bg);
    }
  • Can i use variables to define Selectors?

    No. It wouldn't make sense to do so: from which element should it retrieve the value of the variable? Using variable values in properties could make more sense, but you can't neither.

    To retrieve the value of a CSS variable you can only use var(), which can only be used in values.

    The value of a custom property can be substituted into the value of another property with the var() function.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • Thanks! it was very helpfull!! Obviously you know what i'm talking about, could you please mark the question as unduplicated? – Rodrigo Calix Dec 21 '15 at 00:29
  • @RodrigoCalix The question was closed by a moderator, so even my dupehammer wouldn't be enough to reopen it. I would say it's a partial duplicate, though. You can try removing the common part and clarifying the other one. Once the question becomes different enough, you can ping BoltClock in a comment and request him to undo the close vote. – Oriol Dec 21 '15 at 01:01
  • I se, Thanks anyway! :D – Rodrigo Calix Dec 21 '15 at 01:40