How to Make React CSS Import Component-Scoped

How to make React CSS import component-scoped?

It sounds like CSS Modules, or many of the other CSS-in-JS packages, does what you want. Others include Emotion (my current favorite), Styled Components, or many of the packages here.

A CSS Module is a CSS file in which all class names and animation names are scoped locally by default. All URLs (url(...)) and @imports are in module request format (./xxx and ../xxx means relative, xxx and xxx/yyy means in modules folder, i. e. in node_modules).

Here's a quick example:

Let's say we have a React component like:

import React from 'react';
import styles from './styles/button.css';

class Button extends React.Component {
render() {
return (
<button className={styles.button}>
Click Me
</button>
);
}
}
export default Button;

and some CSS in ./styles/button.css of:

.button {
border-radius: 3px;
background-color: green;
color: white;
}

After CSS Modules performs it's magic the generated CSS will be something like:

.button_3GjDE {
border-radius: 3px;
background-color: green;
color: white;
}

where the _3DjDE is a randomly generated hash - giving the CSS class a unique name.

An Alternative

A simpler alternative would be to avoid using generic selectors (like p, code, etc) and adopt a class-based naming convention for components and elements. Even a convention like BEM would help in preventing the conflicts you're encountering.

Applying this to your example, you might go with:

.aboutContainer {
# Some style
}

.aboutContainer__code {
# Some style
}

Essentially all elements you need to style would receive a unique classname.

How to import css file as local scoped in ReactJS?

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
}
}

Changing the CSS for a React component has effect on all other pages

If you want to localize CSS rules, then you would have to switch to modular stylesheets (works the same for sass stylesheets).

In your current structure, the component imports non-modular stylesheet and doesn't localize the changes with a unique identifier. Therfore added rules live in a global scope without a unique identifier that would localize them so that only selected components could understand them. That means that they are capable of easily overwriting the same-named rules which were previously established (import order matters here, because it would dictate how the bundler appends the output stylesheet).

So instead of holding component-related rules within ./style.scss file, rename it to ./index.module.scss and then you would utilize it within the component like so:

import React from 'react';
import styles from './index.module.scss';

const HomePage = () => {
return (
<div className={style.homepage}>
<h1 className={style.heading}>Landing page</h1>
</div>
);
};

export default HomePage;

and your stylesheet would look like:

.heading {
color: #f3f3f3;
font-family: "Cambria";
font-weight: normal;
font-size: 2rem;
}

disclaimer:

I've changed the styling convention from selecting elements by their tag, to selecting them by class, because targetting elements by tag is widely considered a bad practice [ref] , but if you want to maintain it, then you would have to provide a parent scope for such a rule (it already exists since the parent <div/> element has an assigned class.
In this case the implementation would look like:

import React from 'react';
import styles from './index.module.scss';

const HomePage = () => {
return (
<div className={style.homepage}>
<h1>Landing page</h1>
</div>
);
};

export default HomePage;

and styles:

.homepage {
h1 {
color: #f3f3f3;
font-family: "Cambria";
font-weight: normal;
font-size: 2rem;
}
}

Evaluate component-scoped scss styles after global min.css in React

You should change the order of import

In, index.js

import ReactDOM from "react-dom";

import "library/library.min.css";

import App from "src/App";

ReactDOM.render(<App />, document.getElementById("root"));

How to import css for only any component in Reactjs

Source - https://codeburst.io/4-four-ways-to-style-react-components-ac6f323da822

import React from 'react';
import registerCSS './register_page.css'; //stylesheet
import loginCSS './login_page.css'; //stylesheet

const DottedBox = () => (
<div className={registerCSS.container}>
<p className={loginCSS.content}>Get started with CSS styling</p>
</div>
);

your CSS should be like this

:local(.container) {
margin: 40px;
border: 5px dashed pink;
}
:local(.content) {
font-size: 15px;
text-align: center;
}

Example

A CSS Module is a CSS file in which all class names and animation names are scoped locally by default. Great article about css modules here.

import React from 'react';
import styles from './DashedBox.css';

const DashedBox = () => (
<div className={styles.container}>
<p className={styles.content}>Get started with CSS Modules style</p>
</div>
);

export default DashedBox;

Similar to css we import css file import styles './DashedBox.css'
then we access to className as we access to object

:local(.container) {
margin: 40px;
border: 5px dashed pink;
}
:local(.content) {
font-size: 15px;
text-align: center;
}

:local(.className)-this when you use create-react-app because of webpack configurations
.className-this if you use your own react boilerplate.
To make CSS modules work with Webpack you only have to include the modules mentioned above and add the following loader to your webpack.config.js file:

. . .
{
test: /\.css$/,
loader: 'style!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
}
. . .


Related Topics



Leave a reply



Submit