How to use ScopedCssBaseline Component in ReactJS ?
Last Updated :
20 Nov, 2023
ScopedCssBaseline component helps in applying baseline only to the children. Material UI for React has this component available for us, and it is very easy to integrate. We can use the ScopedCssBaseline Component in ReactJS using the following approach.
Prerequisites:
Approach:
- Include Necessary Components:
- Start by bringing in the essential elements from the Material-UI library, such as the ScopedCssBaseline component.
- Construct the App Component:
- Construct a functional component, like 'App,' to function as the core element of your React application.
- Enclose Targeted Content:
- Envelop the JSX content requiring isolation by enclosing it within the ScopedCssBaseline component. This guarantees that any styles applied remain confined to this specific section, avoiding interference with global styles.
- Apply Styles:
- Take advantage of the encapsulated area to apply distinct styles without the concern of impacting the broader application. This method proves especially valuable for preserving style uniformity within a designated part of your React application.
Steps to Create React Application And Installing Module:
Step 1: Create a React application using the following command.
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command.
cd foldername
Step 3: After creating the ReactJS application, Install the material-ui modules using the following command.
npm install @material-ui/core
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"@material-ui/core": "^4.12.4",
"react": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example 1: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
JavaScript
import React from "react";
import ScopedCssBaseline
from "@material-ui/core/ScopedCssBaseline";
export default function App() {
return (
<div style={{ display: "block", padding: 30 }}>
<h4>How to use ScopedCssBaseline Component in ReactJS?</h4>
<ScopedCssBaseline>
<p>Greetings from GeeksforGeeks</p>
<p>
These lines are within
ScopedCssBaseline Component
</p>
<p>Stay Safe and Healthy</p>
</ScopedCssBaseline>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Now if we write the same example but without using the ScopedCssBaseline Component, the following will be the result.
Example 2: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
JavaScript
import React from "react";
export default function App() {
return (
<div style={{ display: "block", padding: 30 }}>
<h4>How to use ScopedCssBaseline Component in ReactJS?</h4>
<p>Greetings from GeeksforGeeks</p>
<p>
These lines are without
ScopedCssBaseline Component
</p>
<p>Stay Safe and Healthy</p>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.
Similar Reads
How to use Portal Component in ReactJS ? The portal component renders its children into a new subtree outside the current DOM hierarchy. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Portal Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReact JSMate
2 min read
How to use DataGrid Component in ReactJS ? A DataGrid Component helps in displaying the information in a grid-like format of rows and columns. Material UI for React has this component available for us, and it is very easy to integrate. We can use the following approach in ReactJS to use DataGrid Component.Creating React Application And Insta
2 min read
How to use CssBaseLine Component in ReactJS ? In React JS, A CssBaseline component is a collection of HTML elements and attribute style-normalizations. At the <head> of our document, it is added as CSS reset. This component is used to add some basic styling to our application like box-sizing, background color, etc like CSS reset for our a
3 min read
ReactJS Onsen UI Segment Component ReactJS Onsen-UI is a popular front-end library with a set of React components that are designed to developing HTML5 hybrid and mobile web apps in a beautiful and efficient way. Segment Component allows the user to toggle between a different number of options in a row. We can use the following appro
2 min read
How to Pass JSON Values into React Components ? In React, you can pass JSON values into components using props. Props allow us to send data from a parent component to its child components. When rendering a component, you can pass the JSON values as props. These props can then be accessed within the componentâs function or class, allowing you to d
3 min read
How to create components in ReactJS ? Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read