Add background gradient support #2176
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces two new core plugins:
backgroundImagegradientColorStopsTL;DR, together they allow you to easily compose custom gradients on the fly, directly in your HTML:
They can even be combined with the new
backgroundCliputilites to do cool text gradient effects:The
backgroundImagepluginThe
backgroundImageplugin is a pretty vanilla core plugin that just bakes in support for thebackground-imageproperty, letting you add whatever values you want:By default we include 8 background image values, mapping to common linear gradient directions:
This generates classes like
bg-gradient-to-t,bg-gradient-to-br, etc.I considered a lot of options for these class names but ultimately decided on this convention for a few reasons:
gradientbecause we don't shorten words anywhere else (we either use the full word or an acronym, never something likegrad)tobecausebg-gradient-tis not clear to me — my mind thinks it's a gradient starting from the top, not ending at the topt,tr,l,bl, etc. to match the border-radius classes and keep things short, since those conventions are already known by Tailwind usersUnder the hood these classes are very simple, and all of the real magic lives in the
gradientColorStopsplugin that generates the--gradient-color-stopscustom property.By default, only
responsivevariants are generated:The
gradientColorStopspluginThe
gradientColorStopsplugin uses a clever custom-property-based implementation to support composable gradients, very similar to how we support composable transforms.It is configured under the
gradientColorStopskey and by default uses your entire color palette:It generates three sets of classes using the following naming convention:
from-{color}, for the gradient starting colorvia-{color}, for the gradient mid colorto-{color}, for the gradient ending colorThese classes work together to generate a final value for the
--gradient-color-stopscustom property that is used by the default set of background gradient utilities.So to create a gradient, you combine one of the background-image utilities, with 1-3 of these color stop utilities to produce the gradient you want on the fly, without pregenerating a combinatoric explosion of classes in advance:
By default,
responsive,hover, andfocusvariants are generated:These new utilities are implemented very carefully to support a handful of useful behaviors.
fromon its own fades to transparentIf you use a
from-{color}utility on its own, thetocolor defaults to transparent, so you fade to transparent without any extra classes.You might be familiar with a bug in Safari that makes fading to transparent very annoying: you can't simply fade to
transparent, you have to fade to a fully transparent version of thefromcolor usingrgbaorhslasyntax, otherwise things will look awful in Safari:Our implementation cleverly dodges that bug, by dynamically generating the right
tocolor by parsing thefromcolor and generating the correctrgbavalue for thetocolor.Here's the definition for
from-blue-500for example:Here,
rgba(66, 153, 225, 0)is the perfectly transparent version of#4299e1, so you can properly fade to transparent in Safari with no extra work.We only support this on
fromand nottofor technical reasons, if you'd like to fade from transparent to a color, simply reverse the direction of the gradient:fromandtowithoutviaworks with no extra CSSThe
via-{color}utilities override the--gradient-color-stopscustom property to include a middle color stop, but if novia-{color}utility is used, thefrom-{color}utilities default to a two-stop value.So both of these just automatically work:
Here's the CSS for one of the
viautilities:You can also fade
froma color,viaa color, to transparent by leaving off theto:Release strategy
These features introduce no breaking changes and will be part of the next minor release (v1.7).