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

100
Vistas
How to increase the specificity of a class of props?

I have a component with a button. It has a default class and a class passed to its from the props. But sometimes the default class overwrites the class from the props. Is it possible to increase the specificity of the class from the props or lower the specificity of the default class?

  1. Do not suggest using !important
  2. Do not suggest to prescribe default styles for the button tag, I do not want to apply them to all buttons in the project
  3. Assigning an id for each button is also not an option
  4. In the props, it is the class name that needs to be passed, not the style
import React from 'react'
import styles from './Button.module.scss'
import classNames from 'classnames'

const Button = ({ children, className, ...props }) => {
  return (
    <button {...props} className={classNames(styles.btn, className)}>
      {children}
    </button>
  )
}

I can't find a beautiful and convenient solution. Now I'm adding !imortant in a class of props. I just hope there's some css or js trick I'm not aware of.

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

0

Any additional selector will be increase priority of passed classname

component.module.scss:

button.passed-class-name {
 color: red;
}

button.module.scss:

.btn {
  color: green
}

Component.js

import styles from './component.module.scss'
import { Button } from 'components/Button'

const Component = () => {
    <Button className={styles.passedClassName} />
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

The ideal solution could not be found. I decided not to reinvent the wheel and write a UI kit for the project, like everyone else. UI kit styles are connected earlier, so styles from parents will rewrite them without !important and without increasing specificity.

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