首页 文章

CSS - 图像两侧的线性渐变透明度

提问于
浏览
10

tl;博士

是否有任何方法可以在图像的左侧和右侧 -webkit-linear-gradient 为CSS中的图像添加透明度?

长版

我有一个图像,我想添加透明度 - 纯CSS - 避免使用任何图像编辑器,如Photoshop,Gimp等 . 我试图使用 -webkit-linear-gradient 但它使用rgba()函数来定义颜色停止 .

所以这个片段

height: 200px;
background: -webkit-linear-gradient(left, rgba(255,0,0,0), rgba(255,0,0,1));

做这个:

enter image description here

在此示例中,rgba()中的最后一个参数定义颜色的透明度 . 到现在为止还挺好 . 如果我将 right 放在 -webkit-linear-gradient 中,则上图将显示相反的情况 . (你不说?!)

我想以某种方式将两者合并,并创建一个两边透明的渐变 . 只有不用渐变 . 带图像 .

我也尝试使用 box-shadowradial-gradient ,但我无法理解 .

Is there any possible way to add transparency on left, and right side of an image using only CSS?

编辑:

示例:
enter image description here

1 回答

  • 17

    您可以使用包装器div然后使用颜色停止:

    div {
      position: relative;
      display: inline-block;
    }
    div:before {
      content: "";
      top: 0;
      left: 0;
      position: absolute;
      height: 100%;
      width: 100%;
      background: -moz-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 49%, rgba(255, 255, 255, 1) 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 1)), color-stop(49%, rgba(255, 255, 255, 0)), color-stop(100%, rgba(255, 255, 255, 1)));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 49%, rgba(255, 255, 255, 1) 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 49%, rgba(255, 255, 255, 1) 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 49%, rgba(255, 255, 255, 1) 100%);
      /* IE10+ */
      background: linear-gradient(to right, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 49%, rgba(255, 255, 255, 1) 100%);
      /* W3C */
      filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ffffff', GradientType=1);
      /* IE6-9 */
    }
    
    <div>
      <img src="http://placekitten.com/g/300/300" alt="" />
    </div>
    

    Resources


相关问题