×

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 Refs

React Refs The word ‘Refs’ is an abbreviation of ‘References’ in React. It is as similar as Keys in React. Refs are a function in React which is used for accessing the DOM element and...

7 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.

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.

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.

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.

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...

5 minutes read.

React Events

React Events An event is an action that triggers a response of the user action or system-generated event. As HTML, React can also perform actions based on user events. React has the...

2 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

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 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 Native vs ReactJS

Difference between React Native and ReactJS React Native React Native is also an open-source JavaScript framework that is used for the development of a mobile application for iOS Android and Windows. It...

6 minutes read.

React Lists

React Lists Lists are used for displaying the data in an ordered format and mainly used to display the menus on websites. The creating of lists in React is as similar...

2 minutes read.

React Flux

React Flux Introduction It is an application architecture that is internally used by Facebook to build the client-web application with react. It is useful when the project includes dynamic data, and...

2 minutes read.

React Router

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 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.

React Props Validation

React Props Validation As we know, props are a suitable mechanism for passing the read-only attributes to react components. The props are required to be correctly used in the component. If it is...

5 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 Code Splitting

React Code Splitting React Code Splitting is the procedure of splitting the bundle files by which the files get easily loaded on the webpage. The react application bundles the files by using...

4 minutes read.

React Map

React Map The map() is the standard function of JavaScript, which can be called at any array. The map() method is used for traversing and displaying a list of the similar objects of...

2 minutes read.