×

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.

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.

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

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

React Component Life-Cycle

React Component Life-Cycle In ReactJS, the creation process of every component includes several lifecycle methods. These methods, together stated as component's lifecycle. Four phases of the component's life cycle are as...

4 minutes read.

React Component API

React Component API It is a top-level API. It provides reusability to the code in the application and makes it completely individual. It has several methods for: Creating Elements.Transforming Elements.Fragments. Now, we are explaining the three...

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.

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.

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 Environment Setup Step by Step

React Environment Setup This section will provide you the information about how to set up an environment for the successful development of ReactJS application: Pre-Requisite for ReactJS NodeJS and NPMReact and React DOMWebpackBabel There are two...

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

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

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

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