【问题标题】:CSS3 Text Gradients not working?CSS3 文本渐变不起作用?
【发布时间】:2012-08-23 18:01:07
【问题描述】:

我正在尝试在某些文本上应用纯 CSS3 渐变(无图像等),但文本保持不变。

我当前的代码是:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Text Gradients</title>
    <style>
    /* The gradient class */
    .gradient {
        -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(252,255,244,1)), color-stop(100%,rgba(233,233,206,1)));
    }
    </style>
</head>
<body>
     <!--The text with the gradient-->
     <h1 class="gradient"> Hello World </h1>
</body>
</html>    

【问题讨论】:

标签: html css text gradient


【解决方案1】:

我会推荐这个site,它适用于所有现代浏览器

background-image: linear-gradient(bottom, rgb(93,245,172) 22%, rgb(121,255,207) 61%, rgb(158,255,249) 81%);
background-image: -o-linear-gradient(bottom, rgb(93,245,172) 22%, rgb(121,255,207) 61%, rgb(158,255,249) 81%);
background-image: -moz-linear-gradient(bottom, rgb(93,245,172) 22%, rgb(121,255,207) 61%, rgb(158,255,249) 81%);
background-image: -webkit-linear-gradient(bottom, rgb(93,245,172) 22%, rgb(121,255,207) 61%, rgb(158,255,249) 81%);
background-image: -ms-linear-gradient(bottom, rgb(93,245,172) 22%, rgb(121,255,207) 61%, rgb(158,255,249) 81%);

background-image: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(0.22, rgb(93,245,172)),
    color-stop(0.61, rgb(121,255,207)),
    color-stop(0.81, rgb(158,255,249))
);

也可以尝试使用css3pie,它允许您添加一些代码使其在 IE 浏览器中运行。

【讨论】:

    【解决方案2】:

    我能够使用以下方法在 Chrome 中生成渐变文本:

    h1 {
      font-size: 72px;
      background: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#333));
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
    

    【讨论】:

      【解决方案3】:

      如果您使用大量 CSS3,我建议您使用 -prefix-free。这允许您跳过所有浏览器前缀,并且该库将在运行时添加所有必要的前缀。

      如果你使用它,你的风格会是这样的:

      .gradient {
              mask-image: gradient(linear, left top, left bottom, color-stop(0%,rgba(252,255,244,1)), color-stop(100%,rgba(233,233,206,1)));
          }
      

      【讨论】:

      • +1 我通常会生成我的渐变,但这会使 css 变得更好和更好的抽象(我认为添加到此工具比返回所有渐变并重新生成出现的任何全新浏览器):)
      【解决方案4】:

      这仅适用于 webkit 用户。要支持所有浏览器,您至少需要:

      filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000'); /* for IE */
      background: -webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000)); /* for webkit browsers */
      background: -moz-linear-gradient(top,  #ccc,  #000); /* for firefox 3.6+ */ 
      

      将颜色值更改为您需要的值。

      编辑:正如@Lokase 所说:您也可以使用他在评论中链接的生成器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-23
        • 1970-01-01
        • 2016-08-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多