bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

Styling React Using CSS

There are many ways to style React with CSS, this tutorial will take a closer look at three common ways:

  • Inline styling
  • CSS stylesheets
  • CSS Modules

Inline Styling

To style an element with the inline style attribute, the value must be a JavaScript object:

Example

Insert an object with the styling information:

const Header = () => {
 return ( <> <h1 style={{color: "red"}}>Hello Style!</h1> <p>Add a little style!</p> </> );
}

Note

In JSX, JavaScript expressions are written inside curly braces, and since JavaScript objects also use curly braces, the styling in the example above is written inside two sets of curly braces {{}} .

camelCased Property Names

Since the inline CSS is written in a JavaScript object, properties with hyphen separators, like background-color , must be written with camel case syntax:

Use backgroundColor instead of background-color

const Header = () => {
 return ( <> <h1 style={{backgroundColor: "lightblue"}}>Hello Style!</h1> <p>Add a little style!</p> </> );
}

JavaScript Object

You can also create an object with styling information, and refer to it in the style attribute:

Create a style object named myStyle

const Header = () => {
 const myStyle = {
 color: "white", backgroundColor: "DodgerBlue", padding: "10px", fontFamily: "Sans-Serif"
 };
 return ( <> <h1 style={myStyle}>Hello Style!</h1> <p>Add a little style!</p> </> );
}

CSS Stylesheet

You can write your CSS styling in a separate file, just save the file with the .css file extension, and import it in your application.

Example

Create a new file called "MyStylesheet.css" and insert some CSS code in it:

body {
 background-color: #282c34;
 color: white;
 padding: 40px;
 font-family: Sans-Serif;
 text-align: center;
}

Note

You can call the file whatever you like, just remember the correct file extension.

Example

Use the import statement to import the stylesheet:

import { createRoot } from 'react-dom/client';
import './MyStylesheet.css';
const Header = () => {
 return ( <> <h1>Hello Style!</h1> <p>Add a little style!.</p> </> );
}
createRoot(document.getElementById('root')).render( <Header /> );

React knows that files imported with the .css extension, should be treated as a CSS Stylesheet.

Note

The styles will only be available for the component that imported it.

CSS Modules

Another way of adding styles to your application is to use CSS Modules.

CSS Modules are convenient for components that are placed in separate files.

The CSS inside a module is available only for the component that imported it, and you do not have to worry about name conflicts.

Create the CSS module with the .module.css extension, example: my-style.module.css .

Example

Create a new file called "my-style.module.css" and insert some CSS code in it:

.bigred {
 color: Tomato;
 padding: 40px;
 font-family: Sans-Serif;
 text-align: center;
}

Example

Use import styles to import the stylesheet:

import { createRoot } from 'react-dom/client';
import styles from './my-style.module.css';
const Car = () => {
 return <h1 className={styles.bigred}>Hello Car!</h1>;
}
createRoot(document.getElementById('root')).render( <Car /> );

You will learn more about CSS Modules in the next chapter.

Previous

React Suspense

Next

React CSS Modules

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

Inline StylingcamelCased Property NamesJavaScript ObjectCSS StylesheetCSS Modules