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
Carbon Add Minutes To Date In Laravel 9
Carbon Add Minutes To Date In...

In this article, we'll explore how to add minutes to a date in Laravel 8, Laravel 9 and Laravel 10 using Carbon...

Read More

Nov-23-2022

Character Count In Textarea
Character Count In Textarea

In this article, we will explain to you how to count characters from textarea. many times a client has requirements...

Read More

Jul-08-2020

Export Buttons In Datatable
Export Buttons In Datatable

In this tutorial i will show you How To Add Export Buttons In Datatable, If you want to export DataTable data...

Read More

Sep-02-2020

How to Create Multi Language Website in Laravel
How to Create Multi Language W...

In this article, we will see how to create a multi-language website in laravel. In this example, you can understand...

Read More

Nov-09-2020