在不使用背景图像的情况下,在纯CSS中跨浏览器文本渐变

Hit*_*n S 23 css linear-gradients css3

我已经尝试了很多文本渐变.我找到了safari和chrome的代码,但它在其他浏览器中不兼容.我想在不使用背景图像的情况下使其工作.如果你有任何适当的解决方案,请提供.

Har*_*345 34

我发现最好的方法是使用SVG渐变,它很容易做,并且不需要任何图像,下面是一些使用SVG创建简单文本渐变的代码.

    <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
          <stop offset="0%" style="stop-color:#FF6600;stop-opacity:1" />
          <stop offset="100%" style="stop-coloR:#FFF000;stop-opacity:1" />
        </linearGradient>
      </defs>
      <text fill="url(#grad1)" font-size="60" font-family="Verdana" x="50" y="100">
    SVG Text Gradient</text>
    </svg>
Run Code Online (Sandbox Code Playgroud)

您可以更改x和y值以创建水平/垂直或对角渐变,也可以使用CSS样式表将样式应用于它,只需在defs标记之间添加一行代码.

  <link href="style.css" type="text/css" rel="stylesheet" 
          xmlns="http://www.w3.org/1999/xhtml"/>
Run Code Online (Sandbox Code Playgroud)

此方法适用于最新版本的Chrome,IE,Firefox和Safari.您还可以应用径向渐变,模糊和滤镜,有关更多信息,请访问W3学校.


小智 26

	<style type="text/css">
		h1 {
			font-family: "Myriad Pro", sans-serif;
			font-size: 40px;
			font-weight: normal;
		}
		
		/* --- start of magic ;-) --- */
		.white-gradient {
			position: relative;
		}
		.white-gradient:after {
			content: '';
			display: block;
			position: absolute;
			top: 0;
			left: 0;
			height: 100%;
			width: 100%;
			filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#00ffffff',GradientType=0 );
			background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(255,255,255,1)), color-stop(100%, rgba(255,255,255,0)));
			background: -webkit-linear-gradient(top, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
			background:    -moz-linear-gradient(top, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%);
			background:     -ms-linear-gradient(top, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%);
			background:      -o-linear-gradient(top, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%);
			background:         linear-gradient(top, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%);
		}
		/* --- end of magic ;-) --- */
	</style>
	
	<h1 class="white-gradient">Pure CSS text gradient without any graphics</h1>
Run Code Online (Sandbox Code Playgroud)

  • 你应该指出,这只适用于白色背景,但很棒的解决方案! (7认同)

小智 6

你可以用jQuery插件做到这一点.

Cufon插件也可能有它,你应该检查出来.它也可以使用Raphael插件或SVG和VML完成,但很难找到纯CSS跨浏览器解决方案.

仅适用于Chrome和Safari:

-webkit-mask-image: -webkit-gradient(linear, left top, left bottom,
    from(rgba(0,0,0,1)), color-stop(50%, rgba(0,0,0,.5)), to(rgba(0,0,0,1)));
Run Code Online (Sandbox Code Playgroud)

对于其他浏览器,您必须使用一些JavaScript.