Material-UI is a user interface framework that provides pre-defined and customizable React components for faster and easy web development. The Material-UI components are based on top of Material Design by Google. In this article let’s discuss the Display API in the Material-UI library.
MUI Display: The Display API provided by MUI allows us to control the visibility of the DOM elements on a need basis.
MUI Display props:
Import Name | Prop | CSS Property | Theme Key |
displayPrint | displayPrint | display | none |
displayRaw | display | display | none |
overflow | overflow | overflow | none |
textOverflow | textOverflow | text-overflow | none |
visibility | visibility | visibility | none |
whitespace | whitespace | white-space | none |
Creating 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 required module using the following command:
npm install @mui/material
Project Structure: It will look like the following.
Example 1: In this example, we will try to create a simple application that 2 DOM elements, one having display style as inline, and the other as block.
Now write down the following code in the App.js file. Here, App is our default component where we have written our code:
App.js
import * as React from 'react';
import Box from '@mui/material/Box';
export default function BasicButtonGroup() {
return (
<div>
<div
className="head"
style={{
width: "fit-content",
margin: "auto",
}}
>
<h1
style={{
color: "green",
}}
>
GeeksforGeeks
</h1>
<strong>React MUI display API</strong>
<br />
<br />
</div>
<div
style={{
margin: "auto",
}}
>
<Box sx={{ width: '100%', height: 100 }}>
<Box
component="div"
sx={{
display: 'inline',
p: 1,
m: 1,
bgcolor: '#101010',
color: 'grey.300',
border: '1px solid',
}}>inline</Box>
<Box
component="div"
sx={{
display: 'block',
p: 1,
m: 1,
bgcolor: '#101010',
color: 'grey.300',
border: '1px solid',
}}>block</Box>
</Box>
</div>
</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:
Example 2: In this example, let's change the visibility of a DOM element depending on the screen size. On small screen sizes, let's show the DOM element, and on bigger screen sizes, let's hide the element. Change your App.js like the one below.
App.js
import * as React from 'react';
import Box from '@mui/material/Box';
export default function BasicButtonGroup() {
return (
<div>
<div
className="head"
style={{
width: "fit-content",
margin: "auto",
}}
>
<h1
style={{
color: "green",
}}
>
GeeksforGeeks
</h1>
<strong>React MUI display API</strong>
<br />
<br />
</div>
<div
style={{
width: "fit-content",
margin: "auto",
}}
>
<Box sx={{ width: '100%', height: 100 }}>
<Box
component="div"
sx={{
display: { xs: 'block', sm: 'none' },
p: 1,
m: 1,
bgcolor: '#101010',
color: 'grey.300',
border: '1px solid',
}}>block</Box>
</Box>
</div>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
Reference: https://mui.com/system/display/
Similar Reads
React MUI Chip Display
React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based o
3 min read
React MUI Badge Display
React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based o
3 min read
React MUI Lists Display
React Lists are a continuous group of text or images. They are composed of items containing primary and supplemental actions, which are represented by icons and text. Material UI is a library of React UI components that implements Google's Material Design. It includes a comprehensive collection of
4 min read
React MUI Table Display
React Tables display information in a way that's easy to scan, so that users can look for patterns and insights. They can be embedded in primary content, such as cards. Material UI is a library of React UI components that implements Google's Material Design. The different types of React tables are:
7 min read
React MUI Icons Display
React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based o
4 min read
React MUI Divider Display
React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based o
3 min read
React MUI Tooltip Display
React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based o
4 min read
React MUI Typography Display
React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based o
4 min read
React.js displayName
The displayName in react is used to assign a custom name to a component for debugging purposes. It is a special property that helps in observing the component tree in developer tools by changing the default names to custom names.Syntax:Component.displayName = 'CustomDisplayName';The displayName prop
2 min read
React MUI Grid Layout
React MUI Grid layout is a responsive layout grid that adapts to screen size and orientation, ensuring consistency across layouts. React MUI Grid LayoutMaterial UI Grid Component gives a responsive layout to all the screens and orientations Syntax: <Grid> ... </Grid>MUI Grid Layout Varia
4 min read