Skip to content
tezvyn:

How do you create gradient text in CSS?

Source: developer.mozilla.orgHardHow cards are made

How do you create gradient text in CSS?
Summary

Mastery of background-clip and CSS box model.

Key points

Set a gradient background-image, use background-clip: text to clip to glyphs, and color: transparent to reveal it.

What's really being asked

This question probes whether you understand the relationship between an element's background layer and its foreground text content. Senior engineers should know that text is painted on top of backgrounds by default, and that background-clip can redefine the painting area. It also checks if you grasp the critical third step: making the text itself transparent so the clipped background becomes visible. The interviewer wants to hear precise property interaction, not just a list of tricks.

The full answer

A strong answer walks through three properties in order. First, set background-image to a linear-gradient or radial-gradient so there is an actual background to display. Second, set background-clip to text so the browser restricts the background painting to the shape of the glyphs rather than the entire border or padding box. Third, set color to transparent because the foreground text color sits above the background in the stacking order; if the text remains opaque, it completely obscures the clipped gradient. Mentioning that background-clip: text is widely supported since mid-2015 but still benefits from a -webkit- prefix in some legacy contexts shows production awareness.

The mistakes people make

Candidates often forget color: transparent and only mention background-clip: text, which produces no visible effect because the solid text fill hides the gradient. Another red flag is immediately jumping to SVG masks or mix-blend-mode hacks without demonstrating knowledge of the standard CSS approach. Some also confuse background-clip with background-origin, claiming the latter controls clipping when it actually controls positioning. Suggesting that background-clip: text works on the root element is also incorrect, as the spec explicitly excludes it.

What usually comes next

An interviewer might ask how you would add a fallback solid color for older browsers, which you can do by declaring a standard color before color: transparent and letting the cascade override it where supported. They might also ask about performance implications of using gradients on large headings, or how this technique behaves with text-decoration and text-shadow. You could also be asked to explain why the gradient repeats if background-size is not set, or how to animate the gradient position.

A concrete example

Imagine a marketing site hero heading that needs a gold-to-purple gradient. You would write a rule targeting the H1: set background-image to linear-gradient from gold to purple, set background-clip to text, and set color to transparent. If the design also requires a subtle shadow, you must note that text-shadow paints independently and will still appear around the transparent glyphs, which may or may not be desired. For accessibility, you should ensure the underlying HTML text remains selectable and readable by screen readers because this technique only affects visual rendering.

Interview question

A heading has background-image: linear-gradient(gold, purple) and background-clip: text applied, yet the text still appears solid black. What is the root cause?

  • a.SVG masking is required because CSS cannot clip backgrounds to glyph shapes
  • b.The foreground text color remains opaque and paints over the clipped background layerCorrect
  • c.background-origin defaults to padding-box, preventing the gradient from reaching the glyph shapes
  • d.The -webkit- prefix is missing, causing the browser to ignore the clip rule entirely
Why?

The card states that foreground text color sits above the background in stacking order, so an opaque text fill completely obscures the clipped gradient unless color is transparent. Option C is a tempting distractor because candidates often confuse background-origin, which controls positioning, with background-clip, which controls the painting area.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles