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

150
Vistas
Set CSS variable with quotes and comma from JavaScript

I have the following CSS:

:root{
  --font-family: "Times", serif;
}

body{
  font-family: var(--font-family);
}

I also have a dropdown with the following values:

serif
sans-serif
'Montserrat', sans-serif;
'Source Code Pro', monospace;

I use this dropdown to set a CSS variable on the body:

let root = document.documentElement;

const fontSelect = document.querySelector("#font")

fontSelect.addEventListener("change", e => {
  console.log(fontSelect.value);
  root.style.setProperty('--font-family', fontSelect.value);
});

Each time, the correct console log happens, however only serif and sans-serif are being applied in Google Chrome. How can I make 'Montserrat', sans-serif; work?

https://codepen.io/EightArmsHQ/pen/3405e40daa7fcc7b17ff8a0e6a205b66

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to remove the ; from your values to make it valid.

let root = document.documentElement;

const fontSelect = document.querySelector("#font")

fontSelect.addEventListener("change", e => {
  console.log(fontSelect.value);
  root.style.setProperty('--font-family', fontSelect.value);
});
:root{
  --font-family: "Times", serif;
}

body{
  font-family: var(--font-family);
}
<select id="font">
  <option value="serif">Serif</option>
  <option value="sans-serif">Sans serif</option>
  <option value="'Montserrat', sans-serif">Montserrat</option>
  <option value="'Source Code Pro', monospace">Source Code Pro</option>
</select>

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