Gradients and Mask Layers
CSS Gradients and effects
The Linear-gradient() function creates a smooth color transition as a background-image, which can include a starting point, direction, and angle.
background: linear-gradient( <Direction>, color1, color2, etc...);
A radial-gradient() function radiates from the center, on an ellipse(default) or circle shape:
//with no keyword (circle at) values are x/y radius values,
//with keyword values are x/y pèosition of the radial
background-image:
radial-gradient(<shape> <size> at <starting-point>, color1, color2, etc );
radial-gradient(circle 60px at 50% 50%, blue 50%, purple 60%);
A conic-gradient is a circular gradient that rotates on a center point
We can add repeating- for all of the gradients.
We can use background-size (and background-position) to multiply the background effect:
The same works for radial-gradients:
There is a difference between the gradient position and the background-position occupied by the gradient. The first can be set with % relative to the container, the second needs this custom values on 100% background-size.
////The gradient position is not animatable, unlike the background-position.
.prototipo1{
height: 200px; width: 200px; background: purple;
mask: radial-gradient(circle 30px at 100% 100%, #0000 100%, #000);
}
//The first moves the gradient origin, the second moves a central gradient at 50% 50%
.prototipo2{
height: 200px; width: 200px; background: blue;
mask: radial-gradient(circle 30px at 50% 50%, #0000 100%, #000)
10000% 10000%/ 99.5% 99.5%; //position/ size
}

We can find many more gradient designs HERE and Here.
Mask layers and images
The mask property hides portions of the background based on its opacity values. Transparent areas of the mask make the background visible, while opaque areas hide it.
//A mask-image, gradient or other graphical element, as long as transparent.
.masked{
width: 300px; height: 200px;
background-image: url("https://live.staticflickr.com/2673/___.jpg");
background-size: cover;
mask-image: url("https://cdn-icons-png.freepik.com/512/16611/16611607.png");
mask-repeat: no-repeat;
mask-size: contain;
}

The mask layer varies based on the transparency, defined with rgba() opacity values.
There can be multiple masks on the same container, and the background will be visible only where all the masks are opaque, regardless of the mask size, while leaving the remaining areas transparent.
//It's part of the CSS rules on masks, make sure to mask-repeat or use 100% size.
//Multiple sizes and position values for masks with a (,) comma
.maskGrad{
...
mask: radial-gradient(circle at 50% 50%, #000 20%, rgba(0, 0, 0, 0.5) 25%);
}
.multiMask{
width: 300px; height: 200px
background-color: blue;
mask-image: url("https://cdn-icons-png.freepik.com/512/11809/11809122.png"),
radial-gradient(transparent 50%, black);
mask-repeat: no-repeat;
mask-position: 0% 50%, 0% 0%; mask-size: 50% 50%;
}

The mask-size/position/repeat properties are the same as background. Mask-clip defines the padding/border/content-box area of the mask.
The mask-compose defines how to combine multiple mask layers:
add: The mask layers are overlayed.
subtract: The second mask is subtracted from the first, creating a hole in it.
intersect: Only the areas where all the masks overlap will be visible.
exclude: The first mask is excluded from the second, like substract but in reverse order
//Only 2 masks can be composite at the time, if more only the last 2 will be taken
.maskComp{
width: 300px; height: 200px;
background-color: blue;
mask: url("https://cdn-icons-png.freepik.com/512/11809/11809122.png"),
radial-gradient(circle at 30% 25%,#0000 10%,#000 60%) 0% 0%/ 100% 100% no-repeat;
mask-size: 100% 100%, 100% 100%;
mask-composite: add; //subtract, intersect, exclude
}

Gradients on mask layers
We create custom borders by using transparent maks on backgrounds.
We control the size and position of no-repeat gradients to create a specific background.
//After radial() comes background-position/ size
.three{
width: 200px; height: 170px;
background:
radial-gradient(30px at 0 0, #0000 100%, red) 0% 0%/ 50% 50%,
radial-gradient(30px at 0 100%, #0000 100%, green) 0% 100%/ 50% 50%,
radial-gradient(30px at 100% 100%, #0000 100%, purple) 100% 100%/ 50% 50%;
background-position: no-repeat;
}

We create simil-border effects by overlaying transparent gradients on the background.
The radial-gradients have double transparent steps for a color-border effect. We center a conic-gradient to the top left and set the container to a transparent section (180-270deg) to let only the offset center visible, also we cut background-size for the radials.
//The gradient order decides their overlay order
//The 100%-10px sets the visible gradient.
.miocubo{
width: 200px; height: 200px;
background:
radial-gradient(
40px at 0% 0%,#0000 calc(100% - 10px) ,red calc(100% - 10px) 100%,#0000)
0 0/40px 40px no-repeat,
radial-gradient(
40px at 100% 100%, #0000 calc(100% - 10px),red calc(100% - 10px) 100%,#0000)
100% 100%/ 40px 40px no-repeat,
conic-gradient(
from 0deg at right 10px top 10px,green 0 180deg,#0000 180deg 270deg,green 0)
100% 0%/calc(100% - 40px + 10px) calc(100% - 40px + 10px) no-repeat;
}

Here are more examples on custom corner effects and streamlined borders.
The conic-gradients() cuts angles from the corners, and the center offset makes them visible. We use the transparent degree for the color step and on the starting angle origin, the calc() function depends on the gradient background position.
//The offset degrees used in calc() must have 90deg of difference
.angoloprimo{
width: 250px; height: 250px;
background:
conic-gradient(from calc(130deg/-2 - 45deg) at top 30px left 30px,
#0000 130deg,red 0) 0 0,
conic-gradient(from calc(90deg/-2 + 45deg) at top 30px right 30px,
#0000 90deg,blue 0) 100% 0,
conic-gradient(from calc(45deg/-2 - 135deg) at bottom 30px left 30px,
#0000 45deg,green 0) 0 100%,
conic-gradient(from calc(160deg/-2 + 135deg) at bottom 30px right 30px,
#0000 160deg,orange 0) 100% 100%;
background-size: 50% 50%;
background-repeat: no-repeat;
}

Last updated
Was this helpful?