Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!