2

I am working on two pages in reactjs. One is a landing page and the other is an articles page. The problem I am having is the CSS rules for the landing page are affecting the appearance of the articles page. I think this has to do with reactjs since it bundles all CSS files into one. How do I separate the CSS rules for each page so that the CSS rules of one page do not affect the appearance of another?

2
  • Just a suggestions, but back when I used react, I used styled components with scoped styles. Personally I like the approach of having a single file with all the information for the component Commented Nov 20, 2018 at 7:17
  • This problem has nothing to do with React and it seems you do not yet know how to construct CSS in general for web pages. you need to wrap all the CSS rules for a certain page under a "father" rule so they will be encapsulated. this is easier with SCSS or automatic with styled-components. I would suggest giving each page a different class to its wrapper element, like div class="page--home" and so on, then use that class as prefix for all that page's rules. Commented Nov 20, 2018 at 13:43

2 Answers 2

2

give unique class/id to body or make a wrapping div for each page like

<body class="landing-page"> or <div class="landing-page"> and <body class="article-page"> or <div class="article-page">

then write css like:

.lanading-page .your-selector{
    cssrules
}

Other options:

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

Comments

0

I personally think the easiest way is to write css in js. There is no need to separate css from js. Just write them together. For example,

const newComponent=props=>{
   <div style={{ display: "flex", alignItems: "center", justifyContent: "center" }}>
     <button type="primary">Confirm</button>
     <button type="primary">Cancel</button>
   </div>
}

You can also refer to a third library, radium, which helps you write more advanced css. https://formidable.com/open-source/radium/

If you are going to learn react native in the future, you will find out that all css is written in js. I hope that this helps.

Comments

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.