In React.js, there are different ways to use CSS. Here are some common methods:
1. Inline Styles:
You can add styles right to your React components with the style
attribute. This is helpful for changing styles based on component states or props.
import React from 'react';
const MyComponent = () => {
const myStyle = {
color: 'blue',
fontSize: '16px',
// add other styles as needed
};
return (
<div style={myStyle}>
Hello, React!
</div>
);
};
export default MyComponent;
2. CSS Modules:
CSS modules let you write organized CSS with class names that apply only to specific parts. Make a CSS file with the .module.css
extension, and then import and use the styles in your React components.
styles.module.css:
.myClass {
color: red;
font-size: 18px;
}
MyComponent.jsx:
import React from 'react';
import styles from './styles.module.css';
const MyComponent = () => {
return (
<div className={styles.myClass}>
Hello, React!
</div>
);
};
export default MyComponent;
3. CSS-in-JS Libraries:
There are many libraries, such as styled-components, emotion, and more, that let you write CSS right in your JavaScript files using template literals.
Using styled-components:
import React from 'react';
import styled from 'styled-components';
const StyledDiv = styled.div`
color: green;
font-size: 20px;
`;
const MyComponent = () => {
return (
<StyledDiv>
Hello, React!
</StyledDiv>
);
};
export default MyComponent;
4. Global Styles:
For global styles, just import your CSS file into your React app (usually in your index.js
or App.js
file).
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css'; // Import your global styles
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
These are some of the usual ways to add styles in React. Pick the one that works best for your project and what you like.
In React, there are no strict rules for naming CSS classes, but following some common practices can make your code simpler to read and maintain. Here are a few ideas:
CamelCase:
- Use simple words for class names in JavaScript or React. For example:
myClass
orheaderContainer
.
- Use simple words for class names in JavaScript or React. For example:
BEM (Block Element Modifier):
BEM is a popular naming method that makes CSS neater and simpler to maintain. It has three parts: Block, Element, and Modifier.
Example:
header
,header__title
,header__title--large
.
Prefixing:
- You can use prefixes to show the purpose or context of a part. For example, if you have a part called
Button
, you might name its styles likebtnPrimary
orbtnSecondary
.
- You can use prefixes to show the purpose or context of a part. For example, if you have a part called
Component-Based Naming:
- Name your styles according to the component they're part of. For example, if you have a
LoginForm
component, you might use styles likeloginFormContainer
orloginFormInput
.
- Name your styles according to the component they're part of. For example, if you have a
Avoid generic names:
- Stay away from generic names that could conflict with other styles. Be clear and detailed.
Example using BEM:
// MyComponent.jsx
import React from 'react';
import './MyComponent.css';
const MyComponent = () => {
return (
<div className="myComponent">
<p className="myComponent__text">Hello, React!</p>
</div>
);
};
export default MyComponent;
/* MyComponent.css */
.myComponent {
/* styles for the component */
}
.myComponent__text {
/* styles for the text element inside the component */
}
Keep in mind that these conventions aren't strict rules. Pick a naming method that works best for your project and team. Being consistent in your codebase is important for easy reading and upkeep.