跳到内容
+

间距

使用 theme.spacing() 助手在 UI 元素之间创建一致的间距。

Material UI 默认使用推荐的 8px 缩放因子

const theme = createTheme();

theme.spacing(2); // `${8 * 2}px` = '16px'

自定义间距

您可以通过提供以下方式更改间距转换

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

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

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

theme.spacing(2); // = '8px'

多重参数

theme.spacing() 助手最多接受 4 个参数。您可以使用这些参数来减少样板代码。

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

也支持混合字符串值

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