Company
Date Published
Author
Connor Barker
Word count
1104
Language
English
Hacker News points
None

Summary

Gradient backgrounds in web design present a challenge in ensuring text legibility, particularly when specific brand colors are used, as text can become poorly contrasted depending on its position on the page. The issue is exacerbated on mobile devices or when resizing pages, where text may shift to areas with insufficient contrast. To address this, designers can adjust text color to meet the WCAG 2.0 contrast threshold of 4.5:1, using tools like the tinycolor2 library to calculate contrast ratios and adjust luminance. For gradient backgrounds, averaging the gradient's colors can help maintain consistency in contrast, but for pixel-perfect results, creating a contrasting gradient that matches each color in the background and applying it as a text clipping mask ensures optimal legibility across the entire page. This involves layering a calculated contrast gradient over the original gradient and using transparent text, which, while requiring some positioning adjustments, results in perfectly contrasted text regardless of its location on the background.