3

我已经使用 React 和Material-UI for React 创建了一个项目。

来自 Bootstrap 背景,我注意到这些组件中没有一个在其组件周围有任何边距。

在 Bootstrap 中,我可以像这样添加间距:

<div class="row">
    <div class="col-xs-12">
    ...
    </div>
</div>

但我不知道使用什么组件来创建这样的间距。

我目前正在使用自定义类来创建某种间距,但感觉不正确。

应用程序.tsx:

<Container maxWidth="lg" className="container-padding">
    ...
</Container>

应用程序.css:

.container-padding {
  padding: 30px;
}

例如,使用现有组件在这些元素之间添加间距:

在此处输入图像描述

我愿意接受建议。

4

2 回答 2

3

有一个@material-ui类似于 Bootstrap 网格的网格布局组件。两者都基于 12 列网格。

下面的例子演示了它,

import Box from '@material-ui/core/Box';
import Grid from "@material-ui/core/Grid";
import Paper from "@material-ui/core/Paper";

return (
<Box m={4}>
 <Grid container spacing={3}>
  <Grid item xs={6}>
    <Paper>xs=6</Paper>
  </Grid>
  <Grid item xs={6}>
    <Paper>xs=6</Paper>
  </Grid>
  <Grid item xs={3}>
    <Paper>xs=3</Paper>
  </Grid>
  <Grid item xs={3}>
    <Paper>xs=3</Paper>
  </Grid>
  <Grid item xs={3}>
    <Paper>xs=3</Paper>
  </Grid>
  <Grid item xs={3}>
    <Paper>xs=3</Paper>
  </Grid>
 </Grid>
</Box>
<Box mx={3}>
  Box 2 content
</Box>
<Box my={3}>
  Box 3 content
</Box>
);

所以总结一下,

m - 所有边距

mx - 水平间距

我的 - 垂直间距

于 2019-11-07T10:42:09.713 回答
0

我使用了Heydon Pickering的“ Lobotomized Owl* + * ”选择器: 。

我创建了一个“容器”组件Vertical.js

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { Box } from '@material-ui/core';

const useStyles = makeStyles((theme) => ({
  vertical: {
    '& > *+*': {
      marginTop: '1.5rem',
    },
  },
}));

const Vertical = ({ children }) => {
  const classes = useStyles();
  return <Box className={classes.vertical}>{children}</Box>;
};

export default Vertical;

然后在任何其他组件中使用它,例如Example.js

import React from 'react';
import Vertical from './Vertical';

const Example = () => {
  return (
    <Vertical>
      <Component/>
      <Component />
      <Another />
      <AnotherComponent />
    </Vertical>
  );
};

export default Example;
于 2021-03-03T13:30:40.467 回答