在CSS中为图像添加圆形淡化不透明度(晕影效果)

bal*_*ker 30 html css opacity css3

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

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

.circle img {
    opacity: 0.4;
    filter: alpha(opacity=40);
}
Run Code Online (Sandbox Code Playgroud)

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

.circle {
    border-radius: 50%;
    display: inline-block;
}
.circle img {
    border-radius: 50%;
    display: block;
}
Run Code Online (Sandbox Code Playgroud)

我想以某种方式合并上面的两件事并仅对图像的边缘应用不透明度,因此图像的中心几乎不会从不透明度标签中获取任何内容.我搜索了几个小时但什么也没找到.

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

不透明度:http://jsfiddle.net/8w6szf84/48/ - >不透明度差,只想边缘消失...

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

Rud*_*ddy 34

好的,我们可以做的是创建一个:after等于父级大小的元素.使用此功能,我们可以设置背景渐变,使其在图像淡入纯色背景时显示.

注意:在这个例子中,我将渐变元素放大了一点,然后移动它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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="circle">
    <img src="http://placeimg.com/200/200/any" />
</div>
Run Code Online (Sandbox Code Playgroud)


编辑:这是另一个没有设置高度或宽度的版本,并摆脱了使用100%父级时引起的边框.正如Vucko指出的那样,我们不需要该位置的负值,但可以使用border围绕该位置img.

JsFiddle在这里


Vit*_*des 7

你也可以使用盒子阴影

.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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="shadow">
  <img src="http://placeimg.com/200/200/any" />
</div>
Run Code Online (Sandbox Code Playgroud)


joe*_*ung 6

您可以使用两个(相同的)图像,并使顶部的图像更小且更透明.

.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 */
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

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