简体   繁体   English

渐变文字颜色,使用 Material-UI<typography />

[英]Gradient text color, using Material-UI <Typography />

How do I style a <Typography /> component so that its font-color becomes a gradient?如何设置<Typography />组件的样式,使其font-color变为渐变?

So far what I've tried:到目前为止,我已经尝试过:

const CustomColor = withStyles({
  root: {
    fontColor: "-webkit-linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
  },
})(Typography);

This did not work, so I tried to follow this tutorial, and did this implementation:这不起作用,所以我尝试按照教程进行操作,并进行了以下实现:

const CustomColor = withStyles({
  root: {
    fontSize: 20,
    background: "-webkit-linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
    webkitBackgroundClip: "text",
    WebkitTextFillColor: "transparent",
  },
})(Typography);

This also did not work as expected, but at least some sort of gradient showed up.这也没有按预期工作,但至少出现了某种渐变。 在此处输入图像描述

Another thing I've tried is:我尝试过的另一件事是:

<Typography style={{
    fontSize: 20,
    background: "-webkit-linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
    webkitBackgroundClip: "text",
    WebkitTextFillColor: "transparent",
  }}> Hello world! </Typography>

This kinda worked, but depending on the width of the element the gradient changes.这有点奏效,但根据元素的宽度,渐变会发生变化。 This is an unwanted behavior.这是不受欢迎的行为。 Also I would like to stik to a withStyles style solution.我也想坚持使用withStyles风格的解决方案。

Online demo: here在线演示:这里

Any tipps?任何提示? What have I missed?我错过了什么?

replace the webkitBackgroundClip with WebkitBackgroundClip .webkitBackgroundClip替换为WebkitBackgroundClip JSS takes the capital letters for webkit. JSS 采用 webkit 的大写字母。

const CustomColor = withStyles({
  root: {
    fontSize: 20,
    background: "-webkit-linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
    WebkitBackgroundClip: "text",
    WebkitTextFillColor: "transparent"
  }
})(Typography);

Here is the working demo:这是工作演示:
编辑悲伤之星-v8u37

After some time playing around, I come up with this solution...玩了一段时间后,我想出了这个解决方案......

  <Typography
  variant="h1"
  align="left"
  color="grey.700"
  sx={{
    backgroundcolor: "primary",
    backgroundImage: `linear-gradient(45deg, #5514B4, #FF80FF)`,
    backgroundSize: "100%",
    backgroundRepeat: "repeat",
    backgroundClip: "text",
    WebkitBackgroundClip: "text",
    WebkitTextFillColor: "transparent"
  }}
>
  GRADIENT TEXT
</Typography>   

Codesandbox Example 代码沙盒示例

I was having trouble getting this to work with MUI but i got it working with this approach.我无法让它与 MUI 一起使用,但我让它与这种方法一起使用。 The key was using backgroundImage instead of background关键是使用 backgroundImage 而不是背景

WebkitBackgroundClip: 'text',
WebkitTextFillColor: 'transparent',
backgroundImage: '-webkit-linear-gradient(rgba(222, 53, 76, 0.8), rgba(226, 123, 27, 0.8))',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM