...

Custom Image Masks With Linear Gradients in CSS

Scroll down to test this effect in an interactive exercise!

This tutorial will teach you how to create custom feather image masks using linear gradients in CSS to make a cool fading image effect.

feather-effect-image-masks-with-linear-gradients-CSS

Add a linear gradient to the mask-image property

Start by defining your CSS class and adding a mask-image property. Then, you want to add a linear-gradient to this mask-image and define a few values: the angle of the gradient and the gradient stops.

To create gradient stops, we will use percentage values and black and transparent mask values. Transparent value means where image is completely faded and black value is where image is fully showing. In between those values is the transition – our linear gradient and the feather effect.

feather-effect-explained-css

Below is the CSS code used for the image placed in the container (or a div) with a CSS class “image”:

.image img{
	width: 400px;
	mask-image: linear-gradient(180deg,black 80%,transparent 100%);
	-webkit-mask-image: linear-gradient(180deg,black 80%,transparent 100%);
}

Note that we are using the mask-image AND -webkit-mask-image property with the same linear-gradient. Basically, we are just copying the whole line with the mask-image code and the whole linear gradient on a new line, just with adding ‘-webkit-mask-image’ instead of ‘mask-image’.

Use BOTH mask-image and -webkit-mask-image with the same linear gradient values to make this effect compatible with all browsers.

How to add a linear gradient on both sides of the image

If we want to add a linear gradient and feather effect on both sides of the image – for example both top and bottom edges (or left and right), we could adjust the CSS code a bit.

both-sided-linear-gradient-image-mask
.image img{
	width: 400px;
	mask-image: linear-gradient(180deg,transparent 0%,black 30%,black 70%,transparent 100%);
	-webkit-mask-image: linear-gradient(180deg,transparent 0%,black 30%,black 70%,transparent 100%);
}

Notice how we added another transparent gradient stop at the beginning and another black gradient stop to create a feather effect on both sides of the image. You can adjust these percentage values as necessary to control the gradient effect and create a more subtle or hard transition.

Adding a linear gradient on all four sides of the image

Now what if we wanted to add a feather effect on ALL FOUR SIDES of our image? And apply the linear gradient to every edge: top, bottom, left and right to create sort of a vignette effect?

linear-gradient-on-all-four-sides-image-masks-in-css

In that case, we would use two linear-gradients within the same mask-image property and intersect them.

Here is how that code would look like:

.image img{
	width: 400px;
	mask-image: 
        linear-gradient(180deg,transparent 0%,black 10%,black 80%,transparent 100%),
        linear-gradient(90deg,transparent 0%,black 30%,black 90%,transparent 100%);
        mask-composite: intersect;
	-webkit-mask-image:
        linear-gradient(180deg,transparent 0%,black 10%,black 80%,transparent 100%),
        linear-gradient(90deg,transparent 0%,black 30%,black 90%,transparent 100%);
        -webkit-mask-composite: destination-in;
}

In order to make this code compatible with all browsers, we are using both mask-image and -webkit-mask-image with the same linear-gradient.

Additionally, we are using -webkit-mask-composite property to create the intersecting effect and merge the two masks together in other browsers.

Remember to use both mask-image and -webkit-mask-image AND mask-composite and -webkit-mask-composite to make this compatible with all browsers. If you change the percentage values in mask-image, make sure to change it in -webkit-mask-image as well!

Try it out yourself!

Try to create this effect yourself using the interactive CSS exercise below!

Just click on the ‘CSS’ field in the CodePen below and type the CSS there to see the result. You can copy and paste the CSS codes from above and just change the percentage values to create different feather effects.

If you are changing the gradient stops (gradient percentage values), make sure to change them in BOTH mask-image and -webkit-mask-image properties! If you change one, you should change the other one too.

See the Pen Linear gradient – feather effect – CSS image masks by Anait (@velvetmade) on CodePen.

This exercise is part of our online course CSS Basics.

To access more than 50 interactive exercises like this and practice CSS, enroll today!

Blog

Code Snippets

General Instructions

Step One: What I will be making

First click on the green ‘What I will be making’ button. This is how your end result should look like.

Step Two: What I need

Next, click on the ‘What I need’ button to get some basic parameters to use in the code. Such as the font family, colors, image URLs etc. These are just recommendations. Use your own judgement for the rest of the values, such as paddings, margins, spacing and size.

Step Three: Read the given HTML code

You don’t need to know how to write HTML for these exercises. However, you WILL need to take a look at the ‘HTML’ code in the exercise. You can switch from HTML to CSS tab in the Codepen screen. Then read the built HTML code for each exercise to understand the structure and find the classes that are defined for the elements that you will be editing. Use those predefined classes to style them in CSS.

Step Four: Click on the ‘CSS’ tab and start coding

Click on the ‘CSS’ tab in the Codepen screen and start typing your CSS code here, just below the ‘Write CSS code here’ comment. You can also delete this comment if you wish.

For example, if a ‘div’ has a class “text-content” in HTML, you can then style this class in CSS tab. Just don’t forget to add a dot before calling a class out (.text-content)

You should see a live preview of the CSS code that you are editing in the ‘Result’ tab to the right of the Codepen screen.

Step Five: Check the solution with the CSS code

If you are stuck, click on the ‘Show me the code’ button. This will reveal the code that you can also completely copy and paste in the CSS tab of the exercise to see the result.

Or you can just go back to the lessons and watch them again.

Have fun and feel free to reach out!

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.