In React, how to prevent a component's CSS import from applying to the entire app?

Viewed 4758

I'm using facebook's create-react app for my application:

In my Login.js container, I am importing CSS like so:

import React from 'react';
import '../../styles/users/Login.css'
const Login = () => {
....

The problem is the Login.css styles are being applied to my entire application... for example, if Login.css has:

body {
    background:Red ;
}

The entire app would have a body of background: Red; Even outside of the Login container.

What I expected/want is for a CSS import within a container to only apply to that particular container.

Is that possible w React? How are react developers supposed to handle container specific stylings? Do I need to add an ID to all containers and include that in the entire CSS file?

2 Answers

The best and easiest solution is to give classNames to every element you have in your code. I had the same issue when trying to apply widths and heights to my images and eventually found out that it was affecting whole app.

Related