简体   繁体   English

如何在 material-ui 中对齐网格项目中心?

[英]how to align a grid item center in material-ui in react?

I have a grid element with maxWidth so that it has horizontal margin when displayed in a big screen.我有一个带有 maxWidth 的网格元素,因此它在大屏幕上显示时具有水平边距。 I want the grid element to be centered and a long paragraph should be aligned to the left side.我希望网格元素居中,并且长段落应与左侧对齐。 How would you do?你会怎么做? I'm using MUI v5.我正在使用 MUI v5。 Thank you.谢谢你。

 <Box
      style={{
        backgroundColor:"rgb(234, 237, 242)"
      }}      
    >
      <Grid container alignItems='center' justifyContent='center' maxWidth='md'>
        <Grid item xs={12} md={12} justifyContent="center">   
            <Typography align="center" variant="h4" style={{ fontWeight: 800 }} sx={{mb:4}}>
              Nice title
          </Typography>
            <Typography sx={{ px: 4 }} paragraph>very very long line. very very long line. very very long line. very very long line. very very long line. very very long line. very very long line. very very long line. very very long line. very very long line. very very long line.</Typography>
          </Grid>
        </Grid> 
    </Box>

在此处输入图像描述

It's simple enough using system properties .使用系统属性很简单。

    <Box
      display="flex"
      justifyContent="center"
      style={{
        backgroundColor:"rgb(234, 237, 242)"
      }}
    >
      ...

You can try the following code.你可以试试下面的代码。 I have checked this code it is working perfectly.我已经检查了这段代码,它运行良好。

import * as React from 'react';
import { styled } from '@mui/material/styles';
import Box from '@mui/material/Box';
import Paper from '@mui/material/Paper';
import Grid from '@mui/material/Grid';
import Typography from '@mui/material/Typography';

const Item = styled(Paper)(({ theme }) => ({
  ...theme.typography.body2,
  padding: theme.spacing(1),
  textAlign: 'center',
  color: theme.palette.text.secondary,
}));

export default function BasicGrid() {
  return (
    <Box sx={{ flexGrow: 1, backgroundColor: 'rgb(234, 237, 242)' }}>
      <Grid container spacing={2}>
        <Grid item xs={12}>
          <Item>
            <Typography align="center" variant="h4" style={{ fontWeight: 800 }} sx={{ mb: 4 }}>
              Hello
            </Typography>
            <Typography align="center" sx={{ px: 4 }} paragraph>
              very very long line
            </Typography>
          </Item>
        </Grid>
      </Grid>
    </Box>
  );
}

you can add text-align: center;您可以添加text-align: center; to Box tag like this:像这样的Box标签:

 <Box
   style={{
    backgroundColor:"rgb(234, 237, 242)";
    textAlign: "center";
   }}
 > 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM