背景应该占据整个 space 和

Background should take the entire space and

这就是我要实现的目标,目前的问题是:

import { useState, useEffect } from 'react';
import type { NextPage } from 'next';
import Container from '@mui/material/Container';
import Box from '@mui/material/Box';
import { DataGrid, GridColDef } from '@mui/x-data-grid';
import { Card, Paper } from '@mui/material';
import Skeleton from '@mui/material/Skeleton';
import { amber, orange } from '@mui/material/colors';

import FormOne from './../src/FormOne';

const columns: GridColDef[] = [
  { field: 'id', headerName: 'ID' },
  { field: 'title', headerName: 'Title', width: 300 },
  { field: 'body', headerName: 'Body', width: 600 },
];

const LoadingSkeleton = () => (
  <Box
    sx={{
      height: 'max-content',
    }}
  >
    {[...Array(10)].map((_) => (
      <Skeleton variant="rectangular" sx={{ my: 4, mx: 1 }} />
    ))}
  </Box>
);

const Home: NextPage = () => {
  const [posts, setPosts] = useState([]);
  const [loading, setLoading] = useState(true);

  // fetch data from fake API
  useEffect(() => {
    setInterval(
      () =>
        fetch('https://jsonplaceholder.typicode.com/posts')
          .then((response) => response.json())
          .then((data) => {
            setPosts(data);
            setLoading(false);
          }),
      3000
    );
  }, []);

  return (
    <Container
      maxWidth="lg"
      sx={{
        background: `linear-gradient(to right, ${amber[300]}, ${orange[500]})`,
      }}
    >
      <Card>
        <FormOne />
      </Card>

      <Card>
        <Paper sx={{ height: '300px', width: '100%' }}>
          <DataGrid
            rows={posts}
            columns={columns}
            pageSize={10}
            // autoHeight
            rowsPerPageOptions={[10]}
            disableSelectionOnClick
            disableColumnMenu
            disableColumnSelector
            components={{
              LoadingOverlay: LoadingSkeleton,
            }}
            loading={loading}
          />
        </Paper>
      </Card>
    </Container>
  );
};

export default Home;

首先,您需要添加删除边距并将 100% 的高度应用于 body#root 元素。我已经添加了这个所以 style.css imported inside index.tsx

body {
  margin: 0;
  height: 100%;
}

#root {
  height: 100%;
}

下一步是将 maxWidth 道具设置为 false,因此它将是全宽。

当然,我已经为您的示例添加了更多样式以实现所需的结果(我希望我按照您想象的方式做到了)。

您可以预览codesandbox here and edit the code here

p.s。我没有你的 FormOne 组件所以我现在用简单的输入

替换它