R

React Handbook

Clean • Professional

CSS Styling in React

2 minute

React CSS Styling

Styling in React is crucial for building attractive, user-friendly, and responsive interfaces. React supports multiple CSS styling approaches, allowing developers to pick the method that best suits their project.

Whether you’re a beginner or an experienced developer, understanding CSS in React ensures your apps look polished and maintainable.

Why CSS Styling Matters in React

Proper styling helps:

  • Maintain a consistent look and feel across components
  • Make reusable components that don’t conflict with each other
  • Improve user experience and accessibility
  • Handle dynamic changes like themes, state changes, or user interactions

React doesn’t restrict you to a single method — you can choose from inline styles, CSS files, CSS modules, or CSS-in-JS libraries.

Methods for Styling React Components

External CSS Stylesheets

The traditional approach: write CSS in separate .css files and import them into components.

Example:

App.css

.title {
  color: #4a90e2;
  font-size: 24px;
  text-align: center;
  margin-top: 20px;
}

App.js

import "./App.css";

function App() {
  return <h1 className="title">Hello, CSS Styling in React!</h1>;
}

export default App;

Inline Styles

Inline styling is defined directly within JSX using the style attribute.

Example:

function InlineStyled() {
  const style = {
    backgroundColor: "lightblue",
    color: "white",
    padding: "10px 20px",
    borderRadius: "5px"
  };

  return <button style={style}>Click Me</button>;
}

CSS Modules (Scoped Styling)

CSS Modules allow component-level scoped styles, preventing class name conflicts.

Example:

Button.module.css

.btn {
  background-color: #ff6f61;
  color: white;
  padding: 10px 15px;
  border-radius: 5px;
  cursor: pointer;
}
.btn:hover {
  background-color: #e65b50;
}

Button.js

import styles from "./Button.module.css";

function Button({ text }) {
  return <button className={styles.btn}>{text}</button>;
}

export default Button;

Styled Components (CSS-in-JS)

Styled Components allow you to write CSS directly in JavaScript using template literals. This method is widely used in modern React projects.

Installation:

npm install styled-components

Example:

import styled from "styled-components";

const StyledButton = styled.button`
  background-color: ${(props) => (props.primary ? "#007bff" : "#555")};
  color: white;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;

  &:hover {
    background-color: ${(props) => (props.primary ? "#0056b3" : "#333")};
  }
`;

function App() {
  return <StyledButton primary>Primary Button</StyledButton>;
}

export default App;

Using CSS Frameworks

You can also use pre-built CSS frameworks for rapid UI development:

  • Tailwind CSS: Utility-first, fast prototyping
  • Bootstrap: Classic responsive design system
  • Material-UI (MUI): Component-based design with Material guidelines
  • Chakra UI: Accessible, modular components

Example – Tailwind CSS:

function TailwindButton() {
  return (
    <button className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-700">
      Click Me
    </button>
  );
}

Conditional Styling

React makes it easy to style elements dynamically using state or props.

Example:

function Alert({ type }) {
  const alertStyle = {
    color: type === "success" ? "green" : "red",
    fontWeight: "bold",
  };

  return <p style={alertStyle}>This is a {type} alert!</p>;
}

 

Article 0 of 0