Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
From .class scope, get CSS custom property (variable) value using javascript

I know you can get the :root or html custom properties using window.getComputedStyle(document.body).getPropertyValue('--foo'), but I was wondering how you would get the value from a class scoped property?

For example:

body {
  --background: white;
}
.sidebar {
  --background: gray;
}
.module {
  background: var(--background);
}

How would I get getPropertyValue('--background') from .sidebar, which would return me gray instead of white? Am I going the wrong direction by wanting to do this (I have a library that needs the colors passed to it through JS, and they are already defined as custom properties)?

Research:

  1. I could probably query for an element with .sidebar and get it that way, but it does not seem reliable in case no such element exists.
  2. Seems like I can list all properties (https://css-tricks.com/how-to-get-all-custom-properties-on-a-page-in-javascript/), but this process seems cumbersome.
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const sideBarNodeRef = document.querySelector(".sidebar");

const sideBarBgColor = sideBarNodeRef ? sideBarNodeRef.style.backgroundColor:null

or

const sideBarBgColor = sideBarNodeRef ? sideBarNodeRef.getPropertyValue('background-color'):null
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do it like your "Research 1.", just check if the element exists to avoid errors.
Notice that if a variable is not defined, it will inherit from the parent.

function getCssVar(selector, style) {
  var element = document.querySelector(selector)
  
  // Exit if element doesn't exist
  if(!element) return false
  
  // Exit if variable is not defined
  if(!getComputedStyle(element).getPropertyValue(style)) return false
  
  console.log(`${selector} : ${getComputedStyle(element).getPropertyValue(style)}`) 
}

getCssVar('.exist-and-has-variable', '--background')
getCssVar('.exist-and-no-variable', '--background')

getCssVar('.child-with-variable', '--background')
getCssVar('.child-without-variable', '--background')

getCssVar('.dont-exist', '--background')
:root {
  --background: white;
}
.exist-and-has-variable {
  --background: gray;
}
.child-with-variable {
  --background: red;
}
<div class="exist-and-has-variable">
  <div class="child-with-variable"></div>
  <div class="child-without-variable"></div>
</div>

<div class="exist-and-no-variable">
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda