We all know how to link a stylesheet to an HTML , but styling in a React single-page application (SPA) offers more options. Let's explore the various approaches to CSS in React, highlighting their strengths and weaknesses.
Importing External Stylesheets
This method mirrors traditional CSS linking.
- Create a CSS file (e.g.,
style.css
). - Write your CSS rules.
- Import it into your React component:
import "./style.css";
Example:
import React from "react"; import "./Components/css/App.css"; function App() { return ( <div classname="main"> </div> ); } export default App;
This imports App.css
from the /Components/css
folder.
Inline Styles
While generally discouraged for large projects due to maintainability concerns, inline styles can be practical in specific contexts. React's component structure mitigates some maintainability issues.
A simple example:
<div classname="main" style="{{" color:> </div>
A more organized approach uses style objects:
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}"> </div> ); } export default App;
This applies styles defined in the styles
object. Note the use of curly braces for object referencing.
CSS Modules
CSS Modules offer locally scoped class names, preventing naming conflicts in larger applications. Each component gets its own unique class names.
- Create a
.module.css
file (e.g.,styles.module.css
). - Import it into your React component.
- Use the imported styles with
className
:
/* styles.module.css */ .font { color: #f00; font-size: 20px; }
import React from "react"; import styles from "./styles.module.css"; function App() { return ( <h1 id="Hello-World">Hello World</h1> ); } export default App;
Styled-Components
Styled-components provides a way to write actual CSS within your JavaScript, creating styled React components. Benefits include unique class names, dynamic styling, and better CSS organization.
Install: npm install styled-components
Import: import styled from 'styled-components';
Example:
import React from "react"; import styled from "styled-components"; const Wrapper = styled.div` width: 100%; height: 100px; background-color: red; display: block; `; function App() { return <wrapper></wrapper>; } export default App;
Conditional Styling
Styled-components allows conditional styling using props within the CSS, enabling dynamic style changes based on component state or props. This example uses a ternary operator for conditional styling:
(Example code demonstrating conditional styling omitted for brevity, but the concept is explained.)
Conclusion
React offers diverse CSS styling options. The best approach depends on project size, complexity, and personal preference. This overview provides a solid foundation for choosing the right method for your React projects.
The above is the detailed content of Different Ways to Write CSS in React. For more information, please follow other related articles on the PHP Chinese website!

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

Deploying like an idiot comes down to a mismatch between the tools you use to deploy and the reward in complexity reduced versus complexity added.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment