css - react css 样式语法 - 在一行中使用变量和文本设置 css prop 的值

标签 css reactjs

我有这个代码:

const styles = theme => ({
  root: {
    paddingTop: theme.spacing.unit * 3,
    paddingBottom: theme.spacing.unit * 3,
    paddingLeft: 0,
    paddingRight: 0,
  }
});

是否可以只用一行设置填充? (我想做这样的事情:)

const styles = theme => ({
  root: {
    padding: 0 theme.spacing.unit * 3',
  }
});

最佳答案

是的,您可以像这样创建字符串(使用 template literals ):

const styles = theme => ({
  root: {
    padding: `${theme.spacing.unit * 3} 0`,
  }
});

如果主题间距单位是 10px 将导致 padding: 10px 0,它是 padding: 10px 0 10px 0;

重要的是使用反引号 (`) 而不是引号。

需要注意的一点是,如果您的 theme.spacing.unit 不包含度量单位,请将其添加到大括号之后,如下所示:

padding: `${theme.spacing.unit * 3}px 0`,

关于css - react css 样式语法 - 在一行中使用变量和文本设置 css prop 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51385603/

相关文章:

css - 如何将 Chrome DevTools 计算尺寸更改为毫米?

html - 有没有办法让 <input> 字段不可编辑

css - 通过外部 div 容器中的 css 更改图像的大小

html - 开权后的recentre网站设计 'frame'

reactjs - React Redux 状态在页面刷新时丢失

jquery - 不要在悬停时淡出

javascript - 如何使用 React Native 在 Android 上使用与 ios 相同的自定义字体

javascript - 如何使用reactjs使用相同的输入框存储不同的值?

javascript - 需要在 Antd Datepicker 中编辑 Moment 以显示月份的全名

reactjs - 在 setTimeout 内多次更新状态