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

标签 css transparency linear-gradients

tl;dr

有什么方法可以在 CSS 中通过图像左侧和右侧的 -webkit-linear-gradient 为图像添加透明度?

长版

我有一张图片我想添加透明度 - 使用纯 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 但我无法弄明白。

有没有可能只使用 CSS 在图像的左侧和右侧添加透明度的方法?

编辑:

例子: enter image description here

最佳答案

您可以使用包装 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>


资源


关于CSS - 图像两侧的线性渐变透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28409395/

相关文章:

css - 使用 CSS 在标题中滚动渐变

css - 如何使用 CSS 创建实心彩虹边框?

html - CSS 文本尊重边框半径

c++ - win32 c++ 所有者绘制带有透明图像的按钮

javascript - 有没有办法显示浏览器不支持的字体?

.net - .NET 中用户控制的透明 Web 浏览器

html - 将鼠标悬停在图像上使其透明并显示文本

html - 如何在已经具有渐变背景的容器顶部添加背景?

html - 有没有一种安全的方法来使用 max-height CSS 媒体查询

css - 如何将 bootstrap-vue 图标包含到 nuxtjs 中?导航栏向下箭头的问题