DEV Community

Cover image for How to create text gradients using CSS easily.
Tejashwa Rajvardhan
Tejashwa Rajvardhan

Posted on

20 5

How to create text gradients using CSS easily.

A gradient is a continuation of colors with a starting and stopping point and a linear-gradient gradually moves in a straight line to another color.

Gradients are the latest trends these days and most of the websites are designed using gradients. With the background-clip property of CSS, it's very easy to implement gradients in text.

Syntax: How to create Text gradients

First, we have to set up a font using h1 or any tag of our choice and place it at the desired location. I have placed my font in the center using a flexbox.

Alt Text

The result can be achieved with the help of

  • linear-gradient()
  • background-clip
  • text-fill-color

1.Linear-gradient() property:

As explained above, this property creates a background linear gradient on the text block.
In CSS, linear-gradient is implemented using:

background-image: linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%);
Enter fullscreen mode Exit fullscreen mode

And the result will be:

Alt Text

2.background-clip() property:

This CSS property sets whether an element's background extends underneath its border-box, padding-box, or content-box.

background-clip: text;
Enter fullscreen mode Exit fullscreen mode

And after applying this property, our text will look like:

Alt Text

Now you are wondering what happened to the linear-gradient we applied earlier, but after looking closely in the image you can see colors surrounding the text.

The background is painted within (clipped to) the foreground text.

Due to the text-color, we cannot see our linear gradient color, and then comes the use of our third and last property.

3.text-fill-color property:

Since we want to see the linear-gradient which is clipped to the foreground text, we have to make the color of the text transparent and we can achieve our result.

text-fill-color: transparent
Enter fullscreen mode Exit fullscreen mode

And the result will be something like this:

Alt Text

View full code in Codepen

Alt Text

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️