How to recover variable value (custom Property) CSS?

Asked

Viewed 40 times

1

For example, I have a variable defined in my style sheet:

:root {
  --limite: 90vw;
}

To retrieve the value of this CSS variable in jQuery in such a way that each time this resolution changes (yes, it will change when changing the browser resolution), jQuery captures this change together?

  • "css variable?" variable o property? want to read the value of --limite that’s it?

  • That’s right, CSS variable, not property!

1 answer

1

There is no reason to use jQuery for this! : -) You can use the method getPropertyValue, available in prototype type objects CSSStyleDeclaration.

A simple example:

// Equivalente ao pseudo-seletor `:root` no CSS.
const root = document.documentElement;

// Define uma "variável" na raiz:
root.style.setProperty('--my-var', 'rgb(255, 0, 0)');

// Obtém a "variável":
const result = root.style.getPropertyValue('--my-var');
console.log(result); //=> rgb(255, 0, 0)

The problem is that using getPropertyValue in CSSStyleDeclaration returned by property style of any element will not work to get the "variavies" defined by the CSS, since the property style recovers only defined styles inline in the element itself.

To get CSS "variables" defined in the stylesheet, you must use the CSSStyleDeclaration returned by the method getComputedStyle by application with the element itself.

An example:

// Obtém a raiz:
const root = document.documentElement;

// Obtém os estilos computados para a raiz:
const computedStyles = window.getComputedStyle(root);

// Obtém a "variável":
const result = computedStyles.getPropertyValue('--my-var');
console.log(result); //=> rgb(255, 0, 0)
:root {
  --my-var: rgb(255, 0, 0);
}

Since computed styles inherit CSS rules defined on any parent element, you can use this artifice on any descending element of the root element.

However, to facilitate the computation of styles, always search for the closest element to the one that defined the rule. In this case, the :root.


Regarding "observing changes" automatically, there is no defined way to do this. One option is to encapsulate the logic I showed above in a function and call it whenever the user changes the resolution.

You can do this type of invocation by issuing the event resize, for example.


This response was based in this question (and answers) soen.

  • I think I get it. But I don’t think this is going to work. Not your code, but my idea of a solution. See: https://codepen.io/carcleo/pen/yLVOxMv, I’m creating a Slider. Everything works fine. However, when I reduce and enlarge the window size, the javascript does not fit the size of the div.containner and then gives a larger count than the images.

  • I added a remark on this at the end of the reply (I edited it just now), did you come to read? There is no way to "observe" directly the value of custom Property defined in the style sheet. What you can do is recompute it (and therefore read it again) by issuing some event, such as the resize.

  • Got it. I’ll try it here, thanks. But , if it’s not too much to ask, can you please read my comment?

  • Ué, young man, I read your comment, so much so that I answered it! : P You are talking about which comment? :)

  • https://codepen.io/carcleo/pen/yLVOxMv

Browser other questions tagged

You are not signed in. Login or sign up in order to post.