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

192
Visualizações
How to avoid CSS conflicts inside React components?

I have a React project which has two pages and each page has its own css file.

Page1.jsx -> Page1.css
Page2.jsx -> Page2.css

Each css file is only included in its corresponding jsx file. Both css files share similar class names (example: column, image-place, etc.). The problem is that Page1 is affected by Page2's CSS file.

I am not expert in front-end technologies. Any help will be appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Are you using create-react-app?

If so, use css-modules instead https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/

CSS Modules allows the scoping of CSS by automatically creating a unique classname

//Page1.jsx
import React from 'react';
import styles from './page1.module.css'; // Import css modules stylesheet as styles

export const Page1 = () => {
  return (
    <div className={styles.column}>page1</div>
  )
}
/* page1.module.scss */
.column {
  color: red
}

//Page2.jsx
import React from 'react';
import styles from './page2.module.css'; // Import css modules stylesheet as styles

export const Page2 = () => {
  return (
    <div className={styles.column}>page2</div>
  )
}
/* page2.module.scss */
.column {
  color: green
}

If you want to combine them with normal or external css class (bootstrap, tailwind, etc), try this:

<div className={`${styles.column} overflow-auto`}>page1</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

A possible way to avoid conflicts is to use Sass. You can set it up by typing in your react app folder terminal:

npm i sass --save-dev

Then transform all .css files to .scss. Add a unique class on the top level wrapper of each page, for example :

retrun (<div className = "pageOne">
  <img src = "/myImage" className = "image-place"/>
</div>)

And finally your styles will look like this :

.pageOne {

  .image-place {
    width:200px
  }
  
  .column {
    width:200px
  }
  
}

This way, you would have one unique class at the top level wrapper, and as many as class that have the same name in them.

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