Home / Blog / HTML & CSS

HTML & CSS

Style Multiline Gradient Text Without Replacing the Font

Style gradient font color as a clipped CSS background, control the multiline area, and preserve selectable text, fallback colors, and readable print output.

Keep the font and change its painted fill

A gradient font color is usually a CSS background clipped to text, not a special gradient font file. Choose the typeface, weight, size and line height normally, then apply the gradient treatment to the specific text element. This keeps the words searchable and selectable and avoids exporting a heading as an image merely to get multicolored letters.

For multiline text, decide which box should define the gradient. One heading element creates a blend across that element’s background area. It does not automatically restart at the beginning of every line or give each letter the full set of colors. The line wrapping and element width therefore influence the visible result.

Use a bounded heading with a stable line height

This heading has a maximum width expressed in characters, which encourages a readable multiline composition without inserting manual line breaks into the wording. The actual wrap still depends on the font and available width. The gradient extends through the full heading box, while the fallback color remains a dark blue.

<h2 class="multiline-gradient">Build useful tools for the people who need them</h2>
.multiline-gradient {
  max-width: min(100%, 18ch);
  margin: 1rem 0;
  color: #1e40af;
  font: 750 clamp(2rem, 5vw, 3.5rem)/1.2 system-ui, sans-serif;
  overflow-wrap: anywhere;
}
@supports (background-clip: text) or (-webkit-background-clip: text) {
  .multiline-gradient {
    background-color: #1e40af;
    background-image: linear-gradient(180deg, #1e40af, #9d174d);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
  }
}
@media (forced-colors: active) {
  .multiline-gradient { background: none; color: CanvasText; }
}
@media print {
  .multiline-gradient { background: none; color: #000000; }
}

The vertical angle makes the top lines trend blue and the lower lines trend toward burgundy. Change it to ninety degrees to make the color progression horizontal across each line’s position in the same box. Neither option alters the source text, the selected font or the heading’s semantic level.

Choose wrapping based on content, not one screenshot

Test a short title and a longer replacement. If you insert hard line breaks just to match a desktop mockup, those breaks can create awkward fragments on phones or in another language. Letting the available width and font metrics determine wrapping is often more robust for dynamic headings.

If the design truly requires separate per-line gradients, give the intended lines separate wrappers and review how they reflow. That is a different composition with extra markup. Avoid promising automatic per-line restarts from a plain background-image declaration; the painting area must be chosen deliberately.

Test the actual font loading states

A fallback font can have different character widths and line heights from a downloaded font. The heading may change its wrap when the final face becomes available, which also changes the gradient’s visual distribution. Inspect both states when a webfont is used, especially if the heading is a prominent part of the initial page.

Do not hide the heading while waiting for a font simply to keep its gradient composition fixed. Choose a suitable fallback family and enough surrounding space for normal reflow. The gradient should remain decoration on readable content, not a reason to delay access to the words.

Use the local preview as a starting point

In CSS Gradient Text Color, enter #1e40af, #9d174d, angle 180 and a sample heading. The preview lets you compare two colors and their direction. It does not reproduce your page’s font loading, maximum width, line breaks, print styles or theme background.

Copy the CSS into your maintained stylesheet and keep the real text in the document. The tool’s Text field changes only its preview, so you still need to write the heading in HTML or provide it through your application. Test the same wording in the actual component after applying the declarations.

Verify legibility beyond the decorative state

Inspect the first and last lines against the real page background, then test forced colors and print preview. The example removes the decorative background in those modes and supplies an appropriate solid text color. MDN’s background-clip reference explains why a transparent fill is involved in the normal effect.

Finally, select and copy the heading to confirm that it remains ordinary text. Increase text size and check that long words wrap without clipping or horizontal scrolling. If a descendant link is added later, give it an independent visible focus style and review its contrast instead of assuming the heading’s decorative treatment automatically covers interactive states.