Styling a React app: the available approaches
A regular HTML document typically pulls in CSS through a stylesheet linked in the <head>, but a single-page application like React has more than one route to the same result. A few of them resemble the traditional method; others take a different direction entirely. Here are the main options.
External stylesheets
React can import a CSS file directly, much like linking one in the HTML <head>. Create a CSS file in the project, write the rules, then import it into the React file. The import normally sits at the top of the file next to the other imports.
import "./style.css";
import { React } from "react";
import "./Components/css/App.css";
function App() {
return (
<div className="main">
</div>
);
}
export default App;
In this example, an App.js pulls in a CSS file from the /Components/css folder.
Inline styles
Inline styling has a reputation for hurting maintainability, but some situations call for it. In React the concern is softened anyway, because the CSS frequently lives in the same file as the component. A basic inline style looks like his:
<div className="main" style={{color:"red"}}>
A cleaner pattern is to define a style object first and then reference it from an element's style attribute, selecting the property to style.
import { React } from "react";
function App() {
const styles = {
main: {
backgroundColor: "#f1f1f1",
width: "100%",
},
inputText: {
padding: "10px",
color: "red",
},
};
return (
<div className="main" style={styles.main}>
<input type="text" style={styles.inputText}></input>
</div>
);
}
export default App;
Here the styles object holds two further objects — one for the .main class, one for a text input — with rules similar to an external stylesheet. Those objects are applied to the style attribute of elements in the returned markup. Styles are referenced inside curly brackets rather than the quotation marks used in plain HTML.
CSS Modules
CSS Modules give you locally scoped class names and work directly alongside React. Per the project's repository documentation:
CSS Modules works by compiling individual CSS files into both CSS and data. The CSS output is normal, global CSS, which can be injected directly into the browser or concatenated together and written to a file for production use. The data is used to map the human-readable names you've used in the files to the globally-safe output CSS.
Put simply, the same class name can be reused across files without collisions, since every name is compiled to a unique programmatic one. That matters most in larger applications, where each class name is scoped to the component that imports it.
A module stylesheet resembles a regular one, with a different extension (e.g. styles.module.css). The setup: create the .module.css file, import the module into the React app as before, then add a className to an element or component and reference the desired style from the import.
/* styles.module.css */
.font {
color: #f00;
font-size: 20px;
}
import { React } from "react";
import styles from "./styles.module.css";
function App() {
return (
<h1 className={styles.heading}>Hello World</h1>
);
}
export default App;
styled-components
The styled-components library builds custom components from actual CSS written in JavaScript — in effect, a React component bundled with styles. Its features include unique class names, dynamic styling and better CSS management, since every component carries its own separate styles.
Install the npm package from the command line:
npm install styled-components
Then import it into the React app:
import styled from 'styled-components'
Create a component and assign a styled property to it, noting the backtick template literals used in the Wrapper object:
import { React } from "react";
import styled from "styled-components";
function App() {
const Wrapper = styled.div`
width: 100%;
height: 100px;
background-color: red;
display: block;
`;
return <Wrapper />;
}
export default App;
That Wrapper renders as a div carrying those styles.
Conditional styling with props
Because styled-components are functional, props are available inside the CSS, which makes it possible to change styles based on a state or prop. In the demo, the div's display property is driven by the display state, and a button toggles that state on click, switching the div between the styles of two states.
For conditional expressions, use ? in place of the usual if/else syntax, with the else portion placed after the semicolon. Also make sure the state is called or used only after it has been initialized — in that demo, the state belongs above the Wrapper component's styles.
No single approach outranks the others; which one fits depends on the situation at hand.



