Text Filled with a Gradient or Image

Text with a Background-Gradient

Text can be filled with a background-gradient. This is actually a bit more complex than it sounds, but creates a really nice effect. Several things must happen.

WARNING: There is an odd bug in iOS devices. Do NOT combine this technique with flexbox. Rather, create a separate class to contain flexbox or other formatting options; limit the class containing the gradient text or image background to only the required CSS.

.gradient-text {
    
    font-size: clamp(1em, 10vw, 8em);
    background-image: linear-gradient(to right, red, black);
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
}

If there's insufficient text, the user may not notice the gradient effect.

Gradient Text

Repeating-Linear-Gradient Text

This can create some really unusual effects.

.repeating-gradient-text {

    font-size: clamp(1em, 10vw, 8em);
    background-image: repeating-linear-gradient(to right, red, black 20px);
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
} 

Repeating Gradient Text

.repeating-gradient-text2 {

    font-size: clamp(1em, 10vw, 8em);
    background-image: repeating-linear-gradient(-60deg, teal, black, teal 10px);
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
}     

Repeating Gradient Text

Text with an Image Background

There are at least three approaches to creating text filled with an image!

  1. Use photo editing software.
  2. Use the mask image attribute.
  3. Use the background-clip attribute.

Creating text with an image background using background-clip uses an approach very similar to creating text with a gradient fill!

.image-text {

    font-size: 8em;
    background-image: url(header.jpg);
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
}

Text filled with an image

See Also