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 11 Socialite Login with Slack Account
Laravel 11 Socialite Login wit...

Hello, laravel web developers! In this article, we'll see how to Socialite login with Slack in laravel 11. In l...

Read More

Aug-16-2024

How To Send Email With Attachment In Laravel 9
How To Send Email With Attachm...

In this article, we will see how to send email with attachments in laravel 9. As we all know mail functionalities are co...

Read More

Mar-16-2022

Laravel 9 Create Middleware For XSS Protection
Laravel 9 Create Middleware Fo...

In this article, we will see laravel 9 create middleware for XSS protection. Cross-site scripting is a type of...

Read More

Apr-30-2022

Laravel 10 Form Validation Example
Laravel 10 Form Validation Exa...

In this article, we will see the laravel 10 form validation example. Here, we will learn about basic form input with lar...

Read More

Mar-22-2023