×

React CSS

What is React CSS

React CSS is used to provide the style to the React applications. The style attribute adds dynamically-computed styles at render time, and it is one of the most used attributes to provide style in React applications. Instead of a CSS string style attribute, it accepts the JavaScript object in CamelCase properties.

There are four ways by which we can provide style to react components; these are listed as follows:

  • Inline Styling.
  • CSS Stylesheet.
  • CSS module.
  • Styled components.

1. Inline Styling

To provide style to an element by inline style attribute, the value must be an object of JavaScript.

Let’s try to understand by using the following basic example of Inline styling in CSS.

Example:

App.js

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

class App extends React.Component {
  render() {
    return (
      <div>
        <h2>Hello World :)</h2>
        <p>This is an example of Inline Styling in CSS :)</p>
      </div>
    );
  }
}

export default App;

Note: We are using two curly braces in the above example in the following lines:

<h1 style={{color: "Magenta"}}>

<h2 style={{color: "Blue"}}>This happens because, In JSX, the JavaScript expressions should be written in curly braces, and also the objects of JavaScript use curly braces, that's why the above styling is written within the sets of curly braces {{}}.

Output:

Inline Styling

camelCase Property Name

As the inline CSS is written inside the JavaScript object, but the properties having two names, like background-color, must be written within the camelCase syntax.

Example:

App.js

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

class App extends React.Component {
  render() {
    return (
      <>
        <h2>Hello World :)</h2>
        <p>Welcome to the tutorialandexample.com :)</p>
      </>
    );
  }
}

export default App;

Output:

Welcome to TutorialandExample

Using JavaScript Object

In inline styling, you can also create an object with styling information and can refer to it in the style attribute.

Example:

App.js

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

class App extends React.Component {
  render() {
    const styleObject = {
      color: "Green",
      backgroundColor: "Yellow",
      padding: "10px",
      fontFamily: "Arial",
    };

    return (
      <div style={styleObject}>
        <h2>Hello World :)</h2>
        <p>Welcome to the tutorialandexample.com :)</p>
      </div>
    );
  }
}

export default App;

Output:

Hello world TutorialandExample

2. CSS Stylesheet

You can make a separate file to write styling for your react application, and you have to save that file with .css extension. Now, you need to import this file in your application.

Appnew.css

body {
background-color: cyan;
color: blue;
padding: 40px;
font-family: Arial;
text-align: center;
}

App.js

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

class App extends React.Component {
  render() {
    return (
      <div>
        <h2>Hello World :) :)</h2>
        <p>Welcome to the tutorialandexample.com :) :)</p>
      </div>
    );
  }
}

export default App;

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title></title>
</head>
<body>
    <div id="root">&nbsp;</div>
</body>
</html>

Output:

CSS Stylesheet

3. CSS Module

It is a different way to add the styles in your react application. It is also a CSS file in which all class names and animation names are scoped locally by default. These modules are convenient for components that are in separate files. It is only used by the component which imports it.

You can create the CSS module by .module.css extension like myModule.module.css name.

Example

App.js

import React from 'react';
import ReactDOM from 'react-dom';
import styles from './myModule.module.css';

class App extends React.Component {
  render() {
    return (
      <div>
        <h2>Hello World :) :)</h2>
        <p>Welcome to the tutorialandexample.com :) :)</p>
      </div>
    );
  }
}

export default App;

myModule.module.css

.myfirststyle { 
  background-color: #cde1b5; 
  color: green; 
  padding: 10px; 
  font-family: Arial; 
  text-align: center; 
 } 
 .mysecondstyle{  
  background-color: #ede1b5; 
  color: blue; 
  font-family: Arial; 
  font-size: 35px; 
  text-align: center; 
 }  

Output:

create the CSS module

4. Styled Components

Styled-components are nothing but a library to react. It generally uses enhance CSS to provide style to the React component systems in your application.

The styled-component gives:

  • No class name bugs
  • Automatic critical CSS
  • Easier deletion of CSS
  • Painless maintenance
  • Simple dynamic styling

Installation to Styled Components

First, you need to install the styled-components library in your react application by using a single command, which is given below:

npm install styled-components --save

Installation to Styled Components

Example:

import React from 'react';
import ReactDOM from 'react-dom';
import styled from 'styled-components';

class App extends React.Component {
  render() {
    const Div: any = styled.div`
      border: 6px dotted lightgreen;
      &:hover {
        background-color: ${(props: any) => props.hoverColor};
      }
    `;

    const Title = styled.h2`
      font-family: Calibri, sans-serif;
      font-size: 45px;
      text-align: center;
      color: green;
    `;

    const Para = styled.p`
      font-size: 25px;
      text-align: center;
      background-color: yellow;
      color: red;
    `;

    return (
      <Div hoverColor="lightblue">
        <Title>Hello World :) :)</Title>
        <Para>Welcome to React with styled-components!</Para>
      </Div>
    );
  }
}

export default App;

Output:

Styled Components

When you move the mouse pointer within the border, the hover gets activated, and the background color within the border will get change. You can understand it by the following image:

Installation to Styled Components

Related Topics

React State

React State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over time. The change...

3 minutes read.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

React Components

React Components Former, the developers used to write more than thousands of lines of code to develop a single-page application. These applications follow the structure of traditional DOM, and it was very challenging to make...

2 minutes read.

Features of ReactJS

ReactJS Features As of now, within the developers, ReactJS is gaining much popularity as the best JavaScript framework. It is crucial for the front-end ecosystem. There are some of the crucial features of ReactJS that are...

2 minutes read.

React Error Boundaries

React Error Boundaries In the past, the errors of JavaScript within the components were used to corrupt the internal state of react and cause it to emit the cryptic errors on the next...

3 minutes read.

Difference between Controlled and Uncontrolled component in ReactJs

Controlled Component In the controlled component, the input of the form element is handled by the component rather than the DOM. Controlled components have the functions that govern the data, which...

2 minutes read.

Difference Between State and Props in React

Comparison between State and Props State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over...

2 minutes read.

How to use React Context

React Context React Context is used to pass the data through the component tree without passing the props down manually at the every level. In React Application, the data is passed in a top-down approach...

4 minutes read.

ReactJS vs AngularJS

Difference between ReactJS and AngularJS AngularJS AngularJS is the JavaScript framework that is open source, and it is also used to build a dynamic web application. It is developed in 2009 by...

5 minutes read.

Pros and Cons of ReactJS

Pros and Cons of ReactJS There are various advantages, and disadvantages of ReactJS are as follows: Benefits of ReactJS 1. Easy to Learn and Use: ReactJS is very easy to use and learn. It has a good...

3 minutes read.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

3 minutes read.

ReactJS Versions

ReactJS Versions The Complete set of release history of ReactJS is elaborated as follows. The complete set of full documentation of recent releases is on GitHub. S.noVersionRelease DateExplanation1.0.3.029/05/2013Initially released for public.2.0.4.029/07/2013Supporting comments...

4 minutes read.

React Keys

React Keys A key can be defined as a unique identifier. In React, it is used for identifying the items that have changed, deleted, and updated from the lists. React Keys are helpful...

3 minutes read.

React Hooks

React Hooks The Hooks were introduced in React 16.8. By using React Hooks, you can use the state and other features of the React without writing a class. Hooks do not work within the classes....

5 minutes read.

React Higher-Order Components

React Higher-Order Components In short form, Higher-Order Components are represented as HOC. The Higher-order component is an advanced technique to use component logic. HOC is a function that takes the component and returns the new...

2 minutes read.

React JSX

React JSX As we know, all of the React components include a render function. The Render function specifies the HTML output of a React component. JSX is an acronym of JavaScript extension that...

3 minutes read.

React Redux

How to Use Redux with React Hooks It is an open-source library of JavaScript which is used for managing the application state. React uses the Redux to build the user interface....

5 minutes read.

React Bootstrap

React Bootstrap React has a widely used JavaScript framework for creating web applications, and Bootstrap has become the most popular CSS framework. Single-page apps have become popular from the last few...

9 minutes read.

React Animation

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

4 minutes read.