Styling in React
Tags: css, react, reactnative, styling
Published: 2024-07-12 13:30:00

Styling is an essential aspect of building React applications. You have various options for styling, ranging from traditional CSS to modern CSS-in-JS solutions and component libraries. Let's explore some popular approaches:

# Traditional CSS

You can use plain old CSS to style your ReactJS app. Create CSS files and import them into your components. This approach provides familiarity and flexibility but may lack some of the benefits of modern styling solutions.

```css
/* styles.css */
.button {
    background-color: blue;
    color: white;
    padding: 10px;
    border: none;
    border-radius: 5px;
}
```
```jsx
// App.jsx
import React from "react";
import "./styles.css";

const App = () => <button className="button">Click Me</button>;

export default App;
```

# Inline Styles

Inline styles in React are specified as an object with camelCase properties instead of a CSS string.

```jsx
const buttonStyle = {
    backgroundColor: "blue",
    color: "white",
    padding: "10px",
    border: "none",
    borderRadius: "5px",
};

const App = () => (
    <>
        <button style={buttonStyle}>Click Me</button>
        <button style={{backgroundColor: "green", color: "white"}}>Click Me</button>
    </>
);

export default App;
```

# CSS Modules

CSS Modules allow you to write CSS that's scoped locally to the component, preventing conflicts with styles in other parts of the application.

```css
/* Button.module.css */
.button {
    background-color: blue;
    color: white;
    padding: 10px;
    border: none;
    border-radius: 5px;
}
```
```jsx
// Button.jsx
import React from "react";
import styles from "./Button.module.css";

const Button = () => <button className={styles.button}>Click Me</button>;

export default Button;
```

# CSS-in-JS Solutions

CSS-in-JS libraries like styled-components, Emotion, and Linaria allow you to write CSS directly within your JavaScript code. This approach offers scoped styles, dynamic styling, and better component encapsulation.

Styled-components is a library for React and React Native that allows you to use component-level styles in your application. It uses tagged template literals to style your components.

```jsx
// Example of using styled-components in React component
import styled from "styled-components";

const StyledButton = styled.button`
    background-color: #007bff;
    color: #fff;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
`;

const MyComponent = () => {
    return (
        <div>
            <StyledButton>Click me</StyledButton>
        </div>
    );
};
```

# Styling Libraries

Several CSS-driven styling libraries are available for React developers. These libraries offer pre-styled and utility classes to quickly build attractive interfaces. Some popular options include TailwindCSS, and react-bootstrap. More on [Styling Libraries](https://devship.tech/react/styling-libraries)

```jsx
// Example of using TailwindCSS in React component
import React from "react";

const MyComponent = () => {
    return (
        <div>
            <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
                Click me
            </button>
        </div>
    );
};
```

# Component Libraries

Several CSS-driven component libraries are available for React developers. These libraries offer pre-styled components to quickly build attractive interfaces. Some popular options include Chakra UI, MUI, Ant Design, Shadcn, MagicUI, NextUI, StyleX, TailwindUI, HeadlessUI, ArkUI, Reactstrap, Keep React and Aceternity UI. More on [Components Libraries](https://devship.tech/react/components-libraries)

```jsx
// Example of using Chakra UI components in React
import { Button, Heading } from '@chakra-ui/react';

const MyComponent = () => {
  return (
    <div>
      <Heading size="lg">Welcome to Chakra UI</Heading>
      <Button colorScheme="blue">Click me</Button>
    </div>
  );
};
```

# Choosing the Right Approach

The choice of styling approach depends on your project requirements, team preferences, and design goals. Consider factors such as developer experience, maintainability, and performance when selecting a styling solution for your React application.