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
How To Download Youtube Video Using jQuery
How To Download Youtube Video...

In this tutorial I will show you how to download youtube video using jquery or how to download youtube video from s...

Read More

Sep-27-2021

Laravel Datatables Example
Laravel Datatables Example

In this example, I will show you how to implement/install data tables in laravel. Datatables provides users with many fu...

Read More

May-16-2020

Laravel 9 One To Many Relationship Example
Laravel 9 One To Many Relation...

In this article, we will see laravel 9 one to many relationship example. Also, you can use one to many relationships in...

Read More

Apr-02-2022

How To Install TinyMCE Editor In Laravel
How To Install TinyMCE Editor...

In this article, I will give you an example of the TinyMCE editor, Tinymce editor is a rich-text open-source editor,&nbs...

Read More

Jun-18-2020