Material UI Flashcards

1
Q

What’s one way to center an element in MU?

A

1. Use Box with minHeight: "100%"

This approach ensures the div expands to fill the parent container’s height dynamically.

```tsx
import { Box, TextField } from “@mui/material”;

return (
<Box
sx={{
display: “flex”,
justifyContent: “center”, // Centers horizontally
alignItems: “center”, // Centers vertically
minHeight: “100%”, // Takes full height of parent container
}}
>
<TextField></TextField>
</Box>
);
~~~

2. Use Grid with height: "100%"

If using MUI Grid, make sure the parent element (e.g., body or html) has a height set.

```tsx

import { Grid, TextField } from “@mui/material”;

return (
<Grid container justifyContent=”center” alignItems=”center” sx={{ height: “100%” }}>
<Grid>
<TextField></TextField>
</Grid>
</Grid>
);
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly