Fundamentals of Styling in React

Introduction to CSS in React
React, a popular JavaScript library for building user interfaces offers various ways to style components. Understanding these options is crucial for creating visually appealing and responsive applications. This chapter explores the fundamental techniques of styling in React, including inline styling, external CSS, CSS Modules, and styled components.
Inline Styling vs. External CSS
Inline Styling: Inline styling involves adding the style attribute directly to React elements. It uses a JavaScript object where the CSS properties are written in camelCase instead of kebab-case.
const style = {
color: 'blue',
backgroundColor: 'lightgray',
};
function MyComponent() {
return <div style={style}>Styled Component</div>;
}Advantages:
- Quick and easy for small style changes.
- Specificity; styles are applied directly to the element, overriding other styles.
Disadvantages:
- Limited scalability; not suitable for large applications.
- Clutters the JSX code.
- Lacks CSS features like pseudo-classes and media queries.
External CSS: Using external CSS files is the traditional way of styling. In React, you can import CSS files just like JavaScript modules.
/* MyComponent.css */
.myComponent {
color: blue;
background-color: lightgray;
}
import './MyComponent.css';
function MyComponent() {
return <div className="myComponent">Styled Component</div>;
}Advantages:
- Familiar approach for those with a background in web development.
- Full access to CSS features.
Disadvantages:
- Global scope by default, leading to potential style conflicts.
- Less dynamic; not tied to component logic.
CSS Modules
CSS Modules offer a middle ground between inline styles and traditional CSS. They scope the CSS locally to the component, avoiding global namespace pollution.
Usage: Rename your CSS file to MyComponent.module.css and import it using a specific syntax.
/* MyComponent.module.css */
.myComponent {
color: blue;
background-color: lightgray;
}import styles from './MyComponent.module.css';
function MyComponent() {
return <div className={styles.myComponent}>Styled Component</div>;
}Advantages:
- Local scoping of styles.
- Can use all standard CSS features.
- Better maintainability for larger projects.
Disadvantages:
- Slightly different syntax and project setup.
- Still requires external CSS files.
Styled-components
Styled-components is a library for React and React Native that allows you to use component-level styles in your application. It leverages tagged template literals in JavaScript.
Example:
import styled from 'styled-components';
const StyledDiv = styled.div`
color: blue;
background-color: lightgray;
`;
function MyComponent() {
return <StyledDiv>Styled Component</StyledDiv>;
}Advantages:
- Encapsulates styles within components, enhancing reusability.
- Dynamic styling based on props and theme.
- No class name bugs; generates unique class names.
Disadvantages:
- Requires learning a new syntax.
- Potentially small performance overhead.
Summary
Each styling method in React has its unique advantages and use cases. Inline styles are quick for small changes, external CSS is familiar and powerful, CSS Modules provide local scoping, and styled components offer a modern, component-centric approach. The choice depends on the specific needs of your project and your personal or team preferences.






