跳转到内容

🎉 Material UI v5 is out! Head to the migration guide to get started.

间距 Spacing

使用 theme.spacing() 助手来打造 UI 元素之间的一致的间距。

Material-UI 默认使用的是 设计指南上建议的 8px 缩放系数

const theme = createTheme();

theme.spacing(2) // = 8 * 2

自定义间距

您可以通过提供以下内容来更改间距转换值:

  • 一个数字
const theme = createTheme({
  spacing: 4,
});

theme.spacing(2) // = 4 * 2
  • 一个函数
const theme = createTheme({
  spacing: factor => `${0.25 * factor}rem`, // (Bootstrap 的策略)
});

theme.spacing(2); // = 0.25 * 2rem = 0.5rem = 8px
  • 一个数组
const theme = createTheme({
  spacing: [0, 4, 8, 16, 32, 64],
});

theme.spacing(2); // = 8

多个参数

theme.spacing() 最多接受 4 个参数。 你可以使用参数来减少样板的代码。

-padding: `${theme.spacing(1)}px ${theme.spacing(2)}px`, // '8px 16px'
+padding: theme.spacing(1, 2), // '8px 16px'

它也支持混合字符串值:

margin: theme.spacing(1, 'auto'), // '8px auto'