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...);
chevron-rightGuide to linear-gradients()hashtag

We use Degrees as starting point:

//by default is 0deg horizontal top-bottom
.nel div:nth-child(1){
  width: 120px;
  height: 120px;
  background: linear-gradient(yellow, blue);
}

If we use keywords it's the to ending point:

.nel div:nth-child(2){
  width: 120px;
  height: 120px;
  background: linear-gradient(to right bottom , yellow, blue, pink );
}

We can also use background-image and transparent gradients;

//Transparent on blue background
.nel div:nth-child(4) {
  width: 120px;
  height: 120px;

  background-image:
    linear-gradient(110deg,
      yellow,
      transparent 15%,
      pink 40%);
  background-color: blue;
}

We can use % or px for color-stops on the gradient:

//we set where the color will change
.nel div:nth-child(4) {
  width: 120px;
  height: 120px;

  background-image:
    linear-gradient(110deg,
      yellow,
      transparent 15%,
      pink 40%);
  background-color: blue;
}

if we want rigid color transition we can:

//repeated to avoid blue gradient
.nel div:nth-child(5) {
  width: 120px;
  height: 120px;

  background-image:
    linear-gradient(90deg,
      orange 30px,
      blue 30px,
      blue 70px,
      orange 70px);
}
differet linear-gradient

A radial-gradient() function radiates from the center, on an ellipse(default) or circle shape:

chevron-rightRadial gradient guidehashtag

We can change the gradient's ending areas with:

We set the starting point for the radial:

And use rigid gradients:

radial on different shapes, areas and positions

A conic-gradient is a circular gradient that rotates on a center point

chevron-rightConic-gradient guidehashtag

We can set AT its center (with both % and px):

We can also set FROM which degree they start and color stops(only in deg and %)

conic gradient position, degree and color stop

We can add repeating- for all of the gradients.

chevron-rightRepeatng gradient lineshashtag

To have repeating gradients we need to set an ending point in %, deg, and px:

for repeating-radial-gradient we have:

for repeating-conic-gradient we:

repeating linear, radial, conic

We can use background-size (and background-position) to multiply the background effect:

chevron-rightCheckboard conic-gradient effect hashtag

We use color-stop to set each color for 1/4 of the area, then we set this gradient for 1/4 (50%, 50%) of the size available:

we use background-position to display it in a different order:

different background-size and background position

The same works for radial-gradients:

chevron-rightRadial-radient circles effectshashtag

Background-size defines colors-stops size:

Only even-numbered gradients will we "cut" on the sides by position:

We can use percentages for position:

10%, 25% and 20% sizes

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.

Gradient with angle origin vs gradient moved on angle

We can find many more gradient designs HEREarrow-up-right and Herearrow-up-right.

chevron-rightConic Metal shiny effect with 6 HSL colorshashtag

We use a conic-gradient as background, It works on any HSL arrow-up-rightcolor; Hue, Saturation, and Lightess:

In order to add more internal colors we don't use borders (harder to color) but :before/:after:

multiple shiny layers

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.

SVG icon mask with image

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.

Transparent and limited background on mask

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

mask-composite property on 2 layers

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.

Radials used as mask layers

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.

Double transparent radiants for borders effect

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.

Conic colored sections and parallel linear-gradients

Last updated