Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Get the referenced value of a CSS variable

Given

<div></div>

<style>
body {
  --var1: 3;
  --var2: var(--var1);
}

div {
  --var3: var(--var2);
  width: var(--var3);
}
</style>

How can I obtain the reference list of css variables?

getReferenceList(divElement, 'width')

Would result in

['--var3', '--var2', '--var1', 3]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you already know the property name, you can use getComputedStyle. For instance,

window.getComputedStyle(document.querySelector('body')).getPropertyValue('--var1');

This will return 3.

If you would like to get a list of all stylesheets, you can use CSSStyleSheet.cssRules.

The following example will give you the stylesheet of body:

document.styleSheets[0].cssRules[0].cssText

So the result of it will be "body { --var1: 3; --var2: var(--var1); }".

In your case, cssRules[0] = body and cssRules[1] = div.

You can also use selectorText to find out what tag it is. In this case document.styleSheets[0].cssRules[0].selectorText will return the string body.

If you prefer an array of properties and values, instead of a string, you can use this regex:

const regex = /(?<={)[ -a-zA-Z0-9\S][^}]+/gm
const str = document.styleSheets[0].cssRules[0].cssText.match(regex);
console.log(str[0].split(';'));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda