tl;dr
Is there any way to add transparency to images in CSS with -webkit-linear-gradient
on left, and right side of the image?
Long Version
I have an image I want to add transparency – with pure CSS – on both side of it avoiding using any image editor like Photoshop, Gimp, etc. I have tried to use -webkit-linear-gradient
but it uses rgba() function to define color stops.
So this snippet
height: 200px;
background: -webkit-linear-gradient(left, rgba(255,0,0,0), rgba(255,0,0,1));
does this:
In this example the last parameter in the rgba() defines the transparency of the color. So far so good. If I put right
in the -webkit-linear-gradient
the image above would show the opposite. (You don’t say?!)
I want somehow to merge the two, and create a gradient that goes to transparent on both side. Only not with gradient. With an image.
I also tried to work around with box-shadow
and radial-gradient
but I couldn’t figure it out.
Is there any possible way to add transparency on left, and right side of an image using only CSS?
EDIT:
Example:
5
Answers
You could use a wrapper div and then use color stops:
Resources
One way I’ve found to do this is to use pseudo classes and to stack them on top of each other. Make sure that the image container is relatively positioned and it should work for you (see example).
Use the
mask-image
and-webkit-mask-image
property, notbackground
:The above code puts color stops at 0, 10, 90, and 100 percent, leaving 10% of each side of any
img
tag semi-transparent. Themask-image
property only uses the alpha channel, you can use any color you want for the non-transparent parts.Late answer but others can make use of it. You can put a gradient with transparency on both sides with just one line: