How To Use CSS In React JS

Websolutionstuff | Aug-17-2022 | Categories : Bootstrap React JS

In this article, we will see how to use CSS in React JS. Also, we will see camel case CSS in React JS. React is a JavaScript library for building user interfaces. React makes it painless to create interactive UIs.

So, let's see how to import CSS in React JS, inline CSS in React, and how to write CSS in React JS.

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. This style same as normal CSS but in React JS you have to write CSS inside two sets of curly braces {{}}.

import React from 'react';
import ReactDOM from 'react-dom/client';

const Header = () => {
  return (
    <>
      <h1 style={{color: "blue"}}>Hello World!</h1>
      <p>Add CSS style in React JS!</p>
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Header />);

 

 

camelCased Property Names

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

import React from 'react';
import ReactDOM from 'react-dom/client';

const Header = () => {
  return (
    <>
      <h1 style={{backgroundColor: "lightblue"}}>Hello world!</h1>
      <p>Add CSS style in React JS!</p>
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Header />);

 

JavaScript Object

You can also create an object with styling information, and refer to it in the style attribute. This object writes in a style attribute like "{objectName}".

import React from 'react';
import ReactDOM from 'react-dom/client';

const Header = () => {
  return (

      <h1 style={{backgroundColor: "lightblue"}}>Hello World!</h1>
      <p>Add CSS style in React JS!</p>

  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Header />);

 

 

CSS Stylesheet

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

App.css

body {
  background-color: #111111;
  color: black;
  padding: 40px;
  font-family: Sans-Serif;
  text-align: center;
}

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import './App.css';

const Header = () => {
  return (
    <>
      <h1>Hello World!</h1>
      <p>Add CSS style in React JS!.</p>
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Header />);

 

 

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.

Create the CSS module with the .module.css extension.

style.module.css

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

Import the stylesheet in your component.

test.js

import styles from './style.module.css'; 

const Test = () => {
  return <h1 className={styles.bigblue}>Hello World!</h1>;
}

export default Test;

Import the component in your application.

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import Test from './test.js';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Test />);

 


You might also like:

Recommended Post
Featured Post
Laravel 9 Livewire CRUD Operation
Laravel 9 Livewire CRUD Operat...

In this article, we will see the laravel 9 livewire crud operation. we will learn about livewire crud operation in larav...

Read More

Nov-24-2022

How to Reset MySQL Root User Password on Ubuntu
How to Reset MySQL Root User P...

Hey there! If you've ever found yourself scratching your head over a forgotten MySQL root password on Ubuntu, fear n...

Read More

Jan-26-2024

Laravel 8 Left Join Query Example
Laravel 8 Left Join Query Exam...

In this tutorial I will give you laravel 8 left join query example. laravel left join eloquent returns all rows from the...

Read More

Nov-26-2021

Laravel 6/7 CRUD tutorial with example
Laravel 6/7 CRUD tutorial with...

In this example, I will show you how to make simple laravel CRUD(insert, update, delete, or list) operations with e...

Read More

May-08-2020