4

How can I import stylesheet locally for single view.

I am having two views /home and /blog and the following directory of files:

src
│  
└───components       
      │
      └───Home
      │     Home.js
      │     Home.css
      └───Blog
            Blog.js
            Blog.css

I have a seperate css file home.css for home view only but that style is getting applied to blog view also.

I want the home.css file to work only with the home.js view.

Home.css

.text1 {
   font-size: 1em !important;
   font-family: "Noto Sans", sans-serif !important;
   color: red;
 }

Home.js

import React, { Component } from "react";
import "./home.css";

class Home extends Component {
  render() {
    return (
      <div className="text1" style={{ overflow: "Hidden" }}>
        <h1>This must be red.</h1>
        <a href="/blog">Go to Blog</a>
      </div>
    );
  }
}

export default Home;

Blog.js

import React, { Component } from "react";
import "./blog.css";   //I didn't Import Home.css here and blog.css is and empty css file.

class Blog extends Component {
  render() {
    return (
      <div className="text1" style={{ overflow: "Hidden" }}>
        <h1>This must be black.</h1>
        <a href="/">Go to Home</a>
      </div>
    );
  }
}

export default Blog;

Here is the React App:

https://codesandbox.io/s/github/AzizStark/ReactStyleIssue/tree/master/?fontsize=14&hidenavigation=1&theme=dark

1
  • 1
    this is happening because of the same className being used in both blog and home. Whichever stylesheet get appends last will have the most effect. Either use different className or use id instead. Commented Dec 26, 2019 at 16:25

2 Answers 2

10

for locally scope don't use import, use require :

import React, { Component } from "react";
//import "./home.css"; // comment

class Home extends Component {
  render() {
    require("./home.css"); // here
    return (
      <div className="text1" style={{ overflow: "Hidden" }}>
        <h1 class="red">This must be red.</h1>
        <a href="/blog">Go to Blog</a>
      </div>
    );
  }
}

export default Home;
Sign up to request clarification or add additional context in comments.

1 Comment

Surprising this isn't said often, even though this is the easiest and best fix.
4

You cannot locally scope CSS in React with pure/vanilla CSS.

In order to achieve locally scoped CSS,

you will have to make use of

1) CSS modules,

import React, { Component } from "react";
import styles from "./blog.css";  

class Blog extends Component {
  render() {
    return (
      <div className="text1" style={styles.blackDiv}>
        <h1>This must be black.</h1>
        <a href="/">Go to Home</a>
      </div>
    );
  }
}

export default Blog;

2) CSS-in-JS/JSS (using libraries such as styled-components).

const Button = styled.button`
  background: transparent;
  border-radius: 3px;
  border: 2px solid palevioletred;
  color: palevioletred;
  margin: 0 1em;
  padding: 0.25em 1em;
`



render(
   <Button /> 
);

3) Using SASS/SCSS (by nesting the selectors/classes within a parent class)

// blog.scss

.blackDiv {
  >h1 {
    // css properties
  }
  > a {
     // css properties
  }
}

4 Comments

Why I cannot locally scope css?
@AzizStark that is because of how CSS works. Once CSS is imported, it will be "combined" and applied to the entire DOM.
You are right!, The CSS is combined. During the final build the css is bundled together even though I use "require()"function which only works is development environment.
@wentjun I thought webpack/css plugin adds some random string after the css class to keep it unique. Isn't it? I remember having same classes in different files and it still used to work in an isolated fashion.

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.