How To Use CSS In 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 />);
Read Also: How To Install React In Laravel 9
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 />);
Read Also: How To Use Array In React JS
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 />);
Read Also: How To Install React JS Step By Step

    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:

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan