1

I am creating a multi-page application using ReactJS and I want different styles for different pages, especially the logo position.

This is my App.js file

import React from 'react';

import Login from './user/Login';
import Auth from './user/Auth';
import Home from './user/Home';
import ResetPassword from './user/ResetPassword';
import './App.css';

const App=()=>{
        //ROUTING
 }

export default App;

The last imported file always overwrites the styling properties of the entire app. I need to change the position of the logo in each page accordingly and also other things. How do I add different css properties for each page?

2
  • Add a parent class in each file and add css under that parent. Commented Jun 11, 2019 at 10:37
  • You should use either CSS Modules or styled-components Commented Jun 11, 2019 at 10:43

2 Answers 2

1

You can use or module css (for this you have to do changes in webpack). https://programmingwithmosh.com/react/css-modules-react/ Also you can use package styled components https://www.styled-components.com/ In this case, you have unique styles for all components.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't have to use styles(background color) from the file which overrides, make settings for background color just inside of your components(module css). If in some reason you need background color in that file - try to use ! important inside module css.
Aso you can add some class above and make your css more specific (.class1 .class2{background-color:red}). But better don't use this style in your App.css
0

Create a folder structure as below. And import their own css files. If you are using sass use unique class names. For the 3rd party components, you may need using "!important" in the css files.

common/
  Avatar.js
  Avatar.css
feed/
  index.js
  Feed.js
  Feed.css
profile/
  index.js
  Profile.js
  ProfileHeader.js
  ProfileHeader.css

and you can read this : https://codeburst.io/4-four-ways-to-style-react-components-ac6f323da822

2 Comments

I have a lot of files under the user folder. Moreover, I tried this, the last imprted js file's css overwrites all others.
Using CSS modules will ensure uniqueness so your CSS doesn't get overridden and stays modular. facebook.github.io/create-react-app/docs/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.