【问题标题】:Add circular fading opacity (vignette effect) to images in CSS向 CSS 中的图像添加圆形渐变不透明度(晕影效果)
【发布时间】:2015-02-22 09:38:45
【问题描述】:

我想使用 CSS 为图像赋予圆形不透明度

我知道我可以在这样的图像上实现不透明度:

.circle img {
    opacity: 0.4;
    filter: alpha(opacity=40);
}

我知道我可以实现这样的圆形图像:

.circle {
    border-radius: 50%;
    display: inline-block;
}
.circle img {
    border-radius: 50%;
    display: block;
}

我想以某种方式合并上面的两件事并仅对图像的边缘应用不透明度,因此图像的中心几乎不会从不透明度标签中获得任何信息。我已经搜索了几个小时,但一无所获。

没有不透明度:http://jsfiddle.net/8w6szf84/47

不透明度:http://jsfiddle.net/8w6szf84/48/ -> 不透明,只希望边缘褪色...

我需要在这方面使用 Javascript 吗?有什么建议吗?

【问题讨论】:

标签: html css opacity


【解决方案1】:

好的,我们可以做的是创建一个:after 元素,它的大小将等于父元素的大小。使用它,我们可以设置背景渐变,使其在图像淡入 solid 颜色背景时显示。

注意:在本例中,我将渐变元素放大了一点,并将其移到了1px 上,以阻止边框出现在它周围。从这里你可以乱来获得你想要的完美效果。

.circle {
    border-radius: 50%;
    display: inline-block;
    position: relative;
}
.circle img {
    border-radius: 50%;
    display: block;
    border:1px solid #fff;
}
.circle:after {
    content: "";
    display: block;
    width: 100%;
    height: 100%;
    background: radial-gradient(ellipse at center, rgba(255,255,255,0) 0%,rgba(255,255,255,1) 70%,rgba(255,255,255,1) 100%);
    border-radius: 50%;
    position: absolute;
    top: 0; left: 0;
}
<div class="circle">
    <img src="http://placeimg.com/200/200/any" />
</div>

编辑:这是另一个版本,没有设置高度或宽度,并消除了使用父级100% 时导致的边框。正如 Vucko 指出的那样,我们不需要该位置的负值,而是可以在 img 周围使用 border

JsFiddle Here

【讨论】:

  • 我不知道这是否是 OP 要求的,但我喜欢这个 :)
  • 不错的一个 - 但我会稍微改变一下,只是为了删除负值,所以检查fiddle
  • @Vucko 啊太棒了!改用边框,不知道为什么我不认为这样做。已更新答案以包含此内容。
  • @Ruddy 是的,border 消除了“故障”。而且,您不必在伪元素 (202px) 上使用固定的 width/height - 只需使用 100%
  • @Vucko 是的,对不起,我更专注于我所做的编辑,因为这个答案更好(不需要宽度或高度)。我只是忘了改变宽度和高度。感谢您的帮助,干得好!
【解决方案2】:

您可以使用两张(相同的)图像,并让顶部的一张更小且透明。

.circle-opaque {
    border-radius: 50%;          /* Make it a circle */
    display: inline-block;       
    position: absolute;          /* Able to position it, overlaying the other image */
    left:0px;                    /* Customise the position, but make sure it */
    top:0px;                     /* is the same as .circle-transparent */
    z-index: -1;                 /* Makes the image sit *behind* .circle-transparent */
}
.circle-opaque img {
    border-radius: 50%;          /* Make it a circle */
    z-index: -1;
}
.circle-transparent {
    border-radius: 50%;          /* Make it a circle */
    display: inline-block;       
    position: absolute;          /* Able to position it, overlaying the other image */
    left: 0px;                   /* Customise the position, but make sure it */
    top: 0px;                    /* is the same as .circle-transparent */
    z-index: 1;                  /* Makes the image sit *on top of* .circle-transparent */
}
.circle-transparent img {
    width: 200px;
    opacity: 0.4;                /* Make the second image transparent */
    filter: alpha(opacity=40);   /* For IE8 and earlier; optional */
    z-index: 1;                  /* And on top of the first */
}
<div class="circle-opaque">
    <img src="http://www.catchannel.com/images/articles/0805/orange-kitten-200px.jpg" />
</div>
<div class="circle-transparent">
    <img src="http://www.catchannel.com/images/articles/0805/orange-kitten-200px.jpg" />
</div>

http://jsfiddle.net/joe_young/20y832r7/

【讨论】:

    【解决方案3】:

    你也可以使用盒子阴影

    .shadow {
      border-radius: 50%;
      display: inline-block;
      position: relative;
    }
    .shadow img {
      border-radius: 50%;
      display: block;
      border: 1px solid #fff;
    }
    .shadow:after {
      content: "";
      display: block;
      width: 100%;
      height: 100%;
      border-radius: 50%;
      position: absolute;
      top: 0;
      left: 0;
      box-shadow: inset 10px 24px 40px 0px white, 
        inset -10px -24px 40px 0px white, 
        inset 20px -10px 40px 0px white, 
        inset -20px 10px 40px 0px white;
    }
    <div class="shadow">
      <img src="http://placeimg.com/200/200/any" />
    </div>

    【讨论】:

    • 您需要在 src attr 末尾添加 "
    【解决方案4】:

    虽然这种方法不如其他方法干净(可能是由于时间不够),但我相信它可以被清除。但是,它仅使用框阴影来实现您想要的外观。 (径向渐变可能是首选,但如果您需要后备,这可能是一个选项)

    div {
      height: 300px;
      width: 300px;
      background: url(http://placekitten.com/g/300/400);
      border-radius: 50%;
      box-shadow: 
        inset -5px -5px 100px white, 
        inset 0 0 90px white, 
        inset 0 0 80px white, 
        inset 0 0 70px white, 
        inset 0 0 60px white, 
        inset 0 0 50px white, 
        inset 0 0 40px white, 
        inset 0 0 30px white, 
        inset 0 0 20px white, 
        inset 0 0 10px red; 
    }
    &lt;div&gt;&lt;/div&gt;

    【讨论】:

    • 嗯,这有点像维托里诺的回答,但这也很好。
    • 我实际上可以说我没有看到他们的。虽然,在看时我可以看到他们的内容涉及我喜欢的太多元素。我尽量将标记保持在最低限度恕我直言:P
    【解决方案5】:

    这是Ruddy's答案的修改版本,除了渐变的直径与图像的宽度(或高度)匹配,而不是对角线。边界半径不是必需的。 80% 的图像按原样显示,剩余的 20% 会从透明变为白色。

    .circle {
      display: inline-block;
      position: relative;
    }
    .circle img {
      display: block;
    }
    .circle:after {
      content: "";
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-image: radial-gradient(circle closest-side at center,
        rgba(255, 255, 255, 0) 0,
        rgba(255, 255, 255, 0) 80%, 
        rgba(255, 255, 255, 1) 100%
      );
    }
    <div class="circle">
      <img src="http://www.catchannel.com/images/articles/0805/orange-kitten-200px.jpg">
    </div>

    注意径向渐变是从指定的中心开始绘制的;而盒子阴影是从边缘绘制的;因此两者都会产生不同的结果。

    【讨论】:

      【解决方案6】:

      我喜欢简单,所以以下可能就足够了:

      .circle {
        background-image: radial-gradient(ellipse at center center, rgba(0, 0, 0, 0) 0%, rgba(255, 255, 255, 1) 70%, rgba(255, 255, 255, 1) 100%);
        display: inline-block;
      }
      .circle img {
        border-radius: 50%;
        mix-blend-mode: lighten;
      }
      <div class="circle">
        <img src="http://www.catchannel.com/images/articles/0805/orange-kitten-200px.jpg" />
      </div>

      【讨论】:

        猜你喜欢
        • 2021-04-04
        • 2012-07-20
        • 2016-10-28
        • 1970-01-01
        • 2011-02-07
        • 2014-07-21
        • 2015-09-20
        • 1970-01-01
        • 2020-01-24
        相关资源
        最近更新 更多