如何为 MUI Chip 背景添加线性渐变颜色?

IT技术 css reactjs material-ui
2021-03-30 06:17:16

我想将颜色下方的线性渐变添加到 MUIChip作为背景颜色。是否可以?

linear-gradient(to right bottom, #430089, #82ffa1)

我正在使用 MUI v0.18.7。

<Chip backgroundColor={indigo400} style={{width: 120}}>
     <Avatar size={32} color={white} backgroundColor={indigo900}>A</Avatar>
     This is a Chip
</Chip>
3个回答

只需background在您的样式中将 设置为所需的渐变:

<Chip style={{width: 120, background: 'linear-gradient(to right bottom, #430089, #82ffa1)'}}>
     <Avatar size={32} color={white} backgroundColor={indigo900}>A</Avatar>
     This is a Chip
</Chip>

请注意,这linear-gradient是一个返回图像而不是颜色的 CSS 函数。因此,您必须设置background属性(获取图像)而不是backgroundColor属性(仅获取颜色)。这是Mozilla 文档中的引述,更彻底地解释了这一点

因为<gradient>s属于<image>数据类型,所以只能在可以使用<image>s的地方使用。出于这个原因,linear-gradient()不会对background-color使用该<color>数据类型的其他属性起作用

伟大的朱尔斯!愚蠢的我应该自己尝试的。谢谢 :)
2021-06-08 06:17:16
很高兴我能帮上忙,赫马德里!总是一种乐趣。
2021-06-19 06:17:16

您可以使用类覆盖 material-ui 中的任何组件。而不是 backgroundColor 试试这个代码。

//After imports statements
const style={
  chip:{
    background: 'linear-gradient(to right bottom, #430089, #82ffa1)',
  }
}
class Chips extends ...{
  render(){
const classes=this.props.classes;
  return(
    <Chip className={classes.chip}>
      <!--Content-->
     </Chip>
  );
  }
  }

谢谢你的回答
2021-06-07 06:17:16

MUI v5 中,您可以使用sxstyled()

sx 支柱

<Chip
  label="Chip Filled"
  sx={{
    color: 'white',
    background: 'linear-gradient(to right bottom, #36EAEF, #6B0AC9)',
  }}
/>

styled()

type GradientChipProps = {
  colors?: string[];
};
const GradientChip = styled(Chip)<GradientChipProps>(({ colors }) => ({
  color: 'white',
  ...(colors && {
    background: `linear-gradient(to right bottom, ${colors.join(',')})`,
  }),
}));
<GradientChip
  label="Chip Outlined"
  variant="outlined"
  colors={['red', 'pink', 'purple']}
/>

现场演示

代码沙盒演示

相关答案