Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React: Using CSS Styles

React: Using CSS Styles

There are several ways to use CSS in a React application.

The following are some common approaches and detailed instructions on how to implement them in React.

Inline Styles

Using CSS styles directly inside an element in React is achieved through inline styles.

Inline styles pass CSS styles directly as an object to an element's style attribute. Each style property is expressed in camelCase rather than the traditional CSS property name.

Using directly inside an element:

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom/client'

;

const

Header

=

(

)

=>

{

 

return

(

   

<>

     

<

h1 style

=

{

{

color

:

"red"

}

}

Hello Style

!

h1

     

<

p

Add a little style

!

p

   

 

)

;

}

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<
Header
/>
)

;

You can also create an object with style information and reference it in the style attribute:

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom'

;

const

MyComponent

=

(

)

=>

{

 

// Define an inline style object

 

const

containerStyle

=

{

    padding

:

'20px'

,

    backgroundColor

:

'#f0f0f0'

 

}

;

 

const

titleStyle

=

{

    fontSize

:

'24px'

,

    color

:

'#333'

 

}

;

 

return

(

   

<

div style

=

{

containerStyle

}

     

<

h1 style

=

{

titleStyle

}

Hello

,

world

!

h1

   

div

 

)

;

}

;

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<
MyComponent
/>
)

;

In the example above, containerStyle and titleStyle are two inline style objects applied to the div and h1 elements respectively. The key points are:

Inline styles can also change dynamically based on the component's state:

Example

import

React

,

{

useState

}

from

'react'

;

import

ReactDOM from

'react-dom'

;

const

MyComponent

=

(

)

=>

{

 

const

[

isHighlighted

,

setIsHighlighted

]

=

useState

(

false

)

;

 

const

containerStyle

=

{

    padding

:

'20px'

,

    backgroundColor

:

isHighlighted

?

'#ffff99'

:

'#f0f0f0'

 

}

;

 

const

titleStyle

=

{

    fontSize

:

'24px'

,

    color

:

'#333'

 

}

;

 

return

(

   

<

div style

=

{

containerStyle

}

     

<

h1 style

=

{

titleStyle

}

Hello

,

world

!

h1

     

<

button onClick

=

{

(

)

=>

setIsHighlighted

(

!

isHighlighted

)

}

        Toggle Highlight

     

button

   

div

 

)

;

}

;

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<
MyComponent
/>
)

;

Via stylesheet files

You can write CSS styles in a separate file, simply save it with a .css extension, then import it into your application.

Create a stylesheet named App.css and add the following CSS code:

Example

body

{

 

background-color

:

#282c34

;

 

color

:

white

;

 

padding

:

40px

;

 

font-family

:

Sans-Serif

;

 

text-align

:

center

;

}

Import the App.css stylesheet in the application:

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom/client'

;

import

'./App.css'

;

const

Header

=

(

)

=>

{

 

return

(

   

<>

     

<

h1

Hello Style

!

h1

     

<

p

Add a little style

!

.

p

   

 

)

;

}

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<
Header
/>
)

;

CSS Modules

Another way to add styles to an application is to use CSS Modules.

CSS Modules are a naming convention for locally scoped CSS files, where CSS class names and animation names are locally scoped by default.

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

Create a CSS module with the .module.css extension, for example: MyComponent.module.css.

Example

/* MyComponent.module.css */

.container

{

 

padding

:

20px

;

 

background-color

:

#f0f0f0

;

}

.title

{

 

font-size

:

24px

;

 

color

:

#333

;

}

Import the stylesheet in the app:

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom'

;

import

styles from

'./MyComponent.module.css'

;

const

MyComponent

=

(

)

=>

{

 

return

(

   

<

div className

=

{

styles.

container

}

     

<

h1 className

=

{

styles.

title

}

Hello

,

world

!

h1

   

div

 

)

;

}

;

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<
MyComponent
/>
)

;