In addition to inline styles, there are several other methods for styling JSX in React:
-
CSS Stylesheets: You can create a separate CSS file and import it into your component. This is the traditional way of styling web applications.
import './App.css'; function App() { return <div className="app">Hello World</div>; } -
CSS Modules: This allows you to write CSS that is scoped locally to the component, preventing class name collisions.
import styles from './App.module.css'; function App() { return <div className={styles.app}>Hello World</div>; } -
Styled Components: A library that allows you to write CSS in your JavaScript files using tagged template literals.
import styled from 'styled-components'; const AppContainer = styled.div` background-color: blue; color: white; `; function App() { return <AppContainer>Hello World</AppContainer>; } -
Emotion: Similar to styled-components, Emotion allows you to style applications using CSS-in-JS.
/** @jsxImportSource @emotion/react */ import { css } from '@emotion/react'; const style = css` background-color: green; color: white; `; function App() { return <div css={style}>Hello World</div>; } -
Tailwind CSS: A utility-first CSS framework that allows you to apply styles directly in your JSX using class names.
function App() { return <div className="bg-blue-500 text-white">Hello World</div>; }
These methods provide flexibility in how you can style your React components based on your project's needs.
