渐变与CSS褪色

时间:2016-01-16 08:05:01

标签: html css css3 svg linear-gradients

我的问题是关于渐变的渐变:渐变 - 从上到下,以及渐变 - 从左到右。

示例:

enter image description here

代码是:

background-image: 
  linear-gradient(0deg, rgba(198,83,165,.95) 0%, rgba(198,86,51,.95) 100%),
  linear-gradient(90deg, transparent 50%, rgba(0,0,0,.95) 100%);
opacity: 0.949;

我的结果如下。

enter image description here

如您所见,它不会淡化渐变,它看起来像这个渐变后面的单独图层。有没有其他方法可以实现这个?

4 个答案:

答案 0 :(得分:11)

正如我在评论中提到的,当您在另一个渐变的顶部添加透明图层时,它将仅通过其下方的彩色渐变图层(而不是容器中存在的图像)显示。因此,使用渐变来实现这一目标非常困难(几乎是不可能的)。

您必须使用蒙版图像来实现它。以下是使用 SVG掩码的片段。



div {
  position: relative;
  height: 300px;
  width: 500px;
}
div svg {
  position: absolute;
  height: 100%;
  width: 100%;
}
div .grad-fill {
  fill: url(#grad);
  mask: url(#masker);
}

<div>
  <svg viewBox="0 0 500 300" preserveAspectRatio="none">
    <defs>
      <linearGradient id="grad" gradientUnits="objectBoundingBox" gradientTransform="rotate(270,0.5,0.5)">
        <stop offset="0%" stop-color="rgba(198,83,165,.95)" />
        <stop offset="100%" stop-color="rgba(198,86,51,.95)" />
      </linearGradient>
      <linearGradient id="mask-grad" gradientUnits="objectBoundingBox">
        <stop offset="40%" stop-color="black" />
        <stop offset="100%" stop-color="white" />
      </linearGradient>
      <mask id="masker" x="0" y="0" width="500" height="300">
        <rect x="0" y="0" width="500" height="300" fill="url(#mask-grad)" />
      </mask>
    </defs>
    <rect x="0" y="0" width="500" height="300" class="grad-fill" />
  </svg>
  <img src="http://lorempixel.com/500/300/animals/8" />
</div>
&#13;
&#13;
&#13;

您可以在以下链接中找到有关 SVG蒙版的更多信息:

这也可以用纯CSS完成,但不幸的是mask-image property is currently supported only by WebKit browsers所以不推荐这种方法。

&#13;
&#13;
div {
  position: relative;
  height: 300px;
  width: 500px;
  color: white;
}
div:after,
img {
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0px;
  left: 0px;
  z-index: -1;
}
div:after {
  content: '';
  background-image: linear-gradient(0deg, rgba(198, 83, 165, .95) 0%, rgba(198, 86, 51, .95) 100%);
  -webkit-mask-image: linear-gradient(90deg, transparent 40%, rgb(0, 0, 0) 100%);
}
&#13;
<div>Some text
  <img src="http://lorempixel.com/500/300/animals/8" />
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:5)

使用纯CSS创建此效果的一种方法是使用多个径向渐变,并使用每个

的透明度

在径向渐变后面,它们非常透明,允许它们在它们之间淡入淡出,有一个线性渐变,可以向右侧提供不透明度(白色)。

如果您想要更准确,可以设置更多的径向渐变,每个渐变渐变都覆盖图像的一部分。

&#13;
&#13;
.test {
  width: 500px;
  height: 400px;
  border: solid 1px green;
  background: 
  radial-gradient(ellipse 70% 100% at top right, rgba(255,0,0,0.5), transparent),   
               radial-gradient(ellipse 70% 100% at bottom right, rgba(255,0,80,0.5), transparent),
               linear-gradient(to left, white 20%, transparent);
  background-size: 100% 80%, 100% 80%, 100% 100%;
  background-position: top left, left 100%, top left;
  background-repeat: no-repeat;
}

body {
  background-image: url(http://lorempixel.com/500/400/nature);
  background-repeat: no-repeat;
  background-position: 8px 8px;
}
&#13;
<div class="test"></div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您仍然可以为彩色背景使用垂直渐变,但使用png-mask进行淡化。

请看这些使用PNG蒙版的示例:https://css-tricks.com/clipping-masking-css/

其余的很简单。

答案 3 :(得分:0)

另一种方法是在background-size: contain的背景中使用png作为渐变。

优势:

  • 适用于所有浏览器(不是&lt; = IE8)

不便之:

  • 需要图像编辑器来改变颜色
  • PNG可能很重(取决于其大小)
  • 你不能很好地控制渐变的应用方式

.test > div {
  background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAADICAYAAADGFbfiAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAADipJREFUeNrs3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ufgBRFII/H7ulIe1YKfDz6a1LNdPaRmVuzKwS+Or5n9tVfbf9ygqpZXRF1r55tqzjHq22ftu+ugfXr9t//0tr+a+hq+zE2Q2vyjn9673+qwfXpnEutXEhACgVkr6gczRXflER+fiMQkOP5LxPdsAGwbp+t7PcEpPvXISDeaAkV1u2zlX2RgNAPBIkIcDcISC8SCQHgT37zFXir9e8C9E8RSEnv2oY7Ze/Y5FXH1fkF13vgNv2TgGDHzTKxIuG29r0+UlH32f3Wnevxbf7/p+Nnc06Oqp4bvS0zz2Oouu/W3+bP7bProfe28ejYPnLeT/sIiAgE3viu/RYOUSMByeDLV+CBathD39Zf/4CJPtiB4IGKe97b+rYIZLyD/RjC2gY6Zgvq4Dow4AWRgNyoewUvSwug4E53v73HEBbgTRZe1ggI4AFEhFGHISxdGiDCJ3r6rPSdiEAAeIUjqEP8T4ABACvWpklfltTOAAAAAElFTkSuQmCC');
  background-size: contain;
  width: 300px;
  height: 150px;
}

.test p {
  color: #fff;
  font-weight: bold;
  text-align: right;
  margin: 10px;
}

.test {
  width: 300px;
  height: 150px;
  background: url(http://lorempixel.com/300/150/nature) no-repeat;
}
<div class="test">
  <div>
    <p>Lorem ipsum</p>
  </div>
</div>

(我使用的是base64图像而不是PNG文件图像,不必查找服务器,但当然,它可以正常使用文件图像)