React MUI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.
In this article, we are going to discuss the React MUI Divider API. The Alert component allows the user to show important tips or messages on the page. The divider allows the user to separate UI components from each other by adding a new line.
Importing the Divider API:
// Import Statement import Divider from '@mui/material/Divider';
Props list:
- children: It is used to denote the content of the divider.
- classes: It is to override or extend the styles applied to the component.
- absolute: It is the Boolean value of the absolute position of the divider.
- light: It is the Boolean value to display a lighter color divider or not.
- flexItem: It is the Boolean value to give the correct height to the divider in the flex container,
- orientation: It is used to make the divider separate horizontal or vertical.
- textAlign: It is used to align the text: center | left | right.
- sx: It is used to add custom CSS styles to the divider.
- variant : It is used to choose different variants of dividers.
CSS Rules:
- root (MuiDivider-root): It is the style applied to the root element.
- absolute (MuiDivider-absolute): It applies styles when the true value to the absolute position of the divider.
- inset (MuiDivider-inset): It applies styles when the divider variant is to be inset.
- fullWidth (MuiDivider-fullWidth): It applies styles when the divider variant is full width.
- middle (MuiDivider-middle): It applies styles when the divider variant is middle.
- light (MuiDivider-light): It applies styles when the divider color is light,
- vertical (MuiDivider-vertical): It applies styles when the divider orientation is vertical.
- flexItem (MuiDivider-flexItem): It applies styles when the divider has the correct height in the flex container.
- withChildren (MuiDivider-withChildren): It applies styles when the horizontal divider has text.
- withChildrenVertical (MuiDivider-withChildrenVertical): It applies styles when the vertical divider has text.
- textAlignRight (MuiDivider-textAlignRight) : It applies styles when horizontal divider has text aligned right.
- textAlignLeft (MuiDivider-textAlignLeft) : It applies styles when horizontal divider has text aligned left.
- wrapper (MuiDivider-wrapper): It applies styles to span children if horizontal divider.
- wrapperVertical (MuiDivider-wrapperVertical): It applies styles to span children if vertical divider.
Approach: Let us create a React project and install React MUI module. Then we will create a UI that will showcase React MUI Divider API.
Creating React Project:
Step 1: To create a react app, you need to install react modules through npx command. “npx” is used instead of “npm” because you will be needing this command in your app’s lifecycle only once.
npx create-react-app project_name
Step 2: After creating your react project, move into the folder to perform different operations.
cd project_name
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install @mui/material @emotion/react @emotion/styled npm install @mui/lab
Project Structure: After running the commands mentioned in the above steps, if you open the project in an editor you can see a similar project structure as shown below. The new component user makes or the code changes, we will be performing will be done in the source folder.
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Example 1: We are creating a UI that shows React MUI Divider.
App.js
import React from 'react' ; import Divider from '@mui/material/Divider' ; const myComponentStyle = { height: 30, paddingTop: 10, backgroundColor: "green" , color: "white" , fontWeight: "bold" , textAlign: "center" } export default function App() { return ( <div className= "App" style={{ textAlign: "center" }}> <h1 style={{ color: 'green' }}> neveropen</h1> <h3><u>React MUI Divider API</u></h3> <p style={myComponentStyle}>Section</p> <Divider>Divider 1</Divider> <p style={myComponentStyle}>Section</p> <Divider>Divider 2</Divider> <p style={myComponentStyle}>Section</p> <Divider>Divider 3</Divider> <p style={myComponentStyle}>Section</p> </div> ); } |
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Example 2: We are creating a UI that shows React MUI Divider.
App.js
import React from 'react' ; import Divider from '@mui/material/Divider' ; import Grid from '@mui/material/Grid' ; export default function App() { return ( <div className= "App" style={{ textAlign: "center" }}> <h1 style={{ color: 'green' }}>neveropen</h1> <h3><u>React MUI Divider API</u></h3> <p>GFG courses:</p> <Grid><b style={{ color: "green" }}> DSA Self Paced </b> </Grid> <Divider flexItem>Divider 1</Divider> <Grid><b style={{ color: "green" }}> Amazon SDE Preparation Test Series</b> </Grid> <Divider flexItem>Divider 2</Divider> <Grid><b style={{ color: "green" }}> Complete Interview Preparation</b> </Grid> </div> ); } |
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://mui.com/material-ui/api/divider/