This document explains different ways to apply styles in React, including inline styles, CSS modules, and styled-components, with examples and best practices for component styling.
In React, styles can be applied to components in various ways, including inline styles, CSS stylesheets, CSS modules, and styled-components. Each method has its own use cases and advantages.
Inline styles are defined directly within the component using a JavaScript object. This method is useful for dynamic styles that depend on component state or props.
1function MyComponent() {
2 const style = {
3 color: 'blue',
4 backgroundColor: 'lightgray',
5 padding: '10px',
6 borderRadius: '5px'
7 }
8
9 return <div style={style}>Hello, World!</div>
10}
11// Or
12function MyComponent() {
13 return (
14 <div
15 style={{
16 color: 'blue',
17 backgroundColor: 'lightgray',
18 padding: '10px',
19 borderRadius: '5px'
20 }}
21 >
22 <p style={{ margin: 0 }}>Hello, World!</p>
23 </div>
24 )
25}
backgroundColor).CSS Modules allow you to write CSS that is scoped locally to the component, preventing style conflicts. They are implemented as a file with a .module.css extension.
1/* MyComponent.module.css */
2.container {
3 color: blue;
4 background-color: lightgray;
5 padding: 10px;
6 border-radius: 5px;
7}
8
9.text {
10 margin: 0;
11}
Import the CSS module in your component:
1import styles from './MyComponent.module.css'
2
3function MyComponent() {
4 return (
5 <div className={styles.container}>
6 <p className={styles.text}>Hello, World!</p>
7 </div>
8 )
9}
Styled-components is a library that allows you to write CSS in JavaScript, creating styled components with encapsulated styles.
1import styled from 'styled-components'
2
3const Container = styled.div`
4 color: blue;
5 background-color: lightgray;
6 padding: 10px;
7 border-radius: 5px;
8`
9
10const Text = styled.p`
11 margin: 0;
12`
13
14function MyComponent() {
15 return (
16 <Container>
17 <Text>Hello, World!</Text>
18 </Container>
19 )
20}
React offers multiple approaches to styling components, each with unique benefits. Inline styles are best for dynamic, component-scoped changes. CSS modules provide local scoping and prevent conflicts. Styled-components enable encapsulated, reusable styles using JavaScript. Choose the method that best fits your project’s needs.
(1) Inline styles in React are defined as JavaScript objects and allow dynamic styling within components.
(1) Using the same class name in a global CSS file can cause style conflicts and unintended overrides.
(4) CSS modules do not apply styles globally; they scope styles locally to prevent conflicts.
(1) Styled-components enable writing CSS in JavaScript, supporting dynamic and encapsulated styles.
(1) The class name must be referenced from the imported styles object for CSS modules to work.
| Method | Characteristic |
|---|---|
| A. Inline Styles | 1. Styles are scoped locally using unique class names |
| B. CSS Modules | 2. Styles are defined as JavaScript objects |
| C. Styled-Components | 3. CSS is written in JavaScript and encapsulated |
| D. Global CSS | 4. Styles can cause conflicts across components |
A-2, B-1, C-3, D-4.
Styled-components allow you to create reusable, encapsulated styles for React components using JavaScript.
True. Styled-components enable reusable and encapsulated styles using JavaScript.
(3) Inline styles are applied using the style attribute, not className.
(1) CSS modules prevent style conflicts by scoping styles to individual components.
(1) Dynamic styles in React are often applied using JavaScript objects that change based on state or props.