I want to give circular opacity to an image using CSS.
I know I can achieve opacity on images like this:
.circle img {
opacity: 0.4;
filter: alpha(opacity=40);
}
I know I can achieve circular images like this:
.circle {
border-radius: 50%;
display: inline-block;
}
.circle img {
border-radius: 50%;
display: block;
}
I want somehow to merge the two things above and apply the opacity only for the edges of the image, so the center of the image gets almost nothing from the opacity tag. I have searched for hours but found nothing.
Without opacity: http://jsfiddle.net/8w6szf84/47
With opacity: http://jsfiddle.net/8w6szf84/48/ -> bad opacity, want only the edges to fade...
Do I need to use Javascript on this? Any suggestions?
Ok, so what we can do is create a
:after
element that will be equal to the size of the parent. Using this we can set a background gradient that will make it appear as the image is fading into the solid colour background.Note: In this example I have made the gradient element a little bigger and moved it over
1px
to stop a border from appearing around it. From here you can mess around to get the perfect effect that you want.Edit: Here is another version without setting a height or width and getting rid of the border that gets caused if using
100%
of parent. As Vucko pointed out we don't need the negative values for the position but can use aborder
around theimg
instead.JsFiddle Here
You could use two (of the same) images and have the top one smaller and transparent.
http://jsfiddle.net/joe_young/20y832r7/
I like simple, so the following might suffice:
Here is a modified version of Ruddy's answer except that the diameter of gradient matches the width (or height) of the image instead of diagonal. Border radius is not required. 80% of the image displays as-is, the remaining 20% fades from transparent to white.
Note that radial gradients are drawn from the specified center; while box shadows are drawn from the edges; therefore both produce different results.
you can also use a box-shadow
Although this approach isn't as clean as others (possibly due to lack of time), I believe it could be cleaned up. However, it's using just box shadows to achieve the look you're thinking of. (Radial gradients would possibly be preferred, although if you need a fallback, this could be an option)