Don't override explicit leading-*
, tracking-*
, or font-{weight}
utilities with font-size utility defaults
#14403
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 improves how the
text-{size}
utilities interact with theleading-*
,tracking-*
, andfont-{weight}
utilities, ensuring that if the user explicitly uses any of those utilities that those values are not squashed by any defaults baked into thetext-{size}
utilities.Prior to this PR, if you wrote something like this:
…the
leading-none
class would be overridden by the default line-height value baked into thetext-2xl
utility at themd
breakpoint. This has been a point of confusion and frustration for people in the past who are annoyed they have to keep repeating their customleading-*
value like this:This PR lets you write this HTML instead but get the same behavior as above:
It's important to note that this change only applies to line-height values set explicitly with a
leading-*
utility, and does not apply to the line-height modifier.In this example, the line-height set by
text-sm/6
does not override the default line-height included in themd:text-lg
utility:That means these two code snippets behave differently:
In the top one, the line-height
md:text-lg
overrides the line-height set bytext-sm/6
, but in the bottom one, the explicitleading-6
utility takes precedence.This PR applies the same improvements to
tracking-*
andfont-{weight}
as well, since all font size utilities can also optionally specify defaultletter-spacing
andfont-weight
values.We achieve this using new semi-private CSS variables like we do for things like transforms, shadows, etc., which are set by the
leading-*
,tracking-*
, andfont-{weight}
utilities respectively. Thetext-{size}
utilities always use these values first if they are defined, and the default values become fallbacks for those variables if they aren't present.We use
@property
to make sure these variables are reset toinitial
on a per element basis so that they are never inherited, like with every other variable we define.This PR does slightly increase the amount of CSS generated, because now utilities like
leading-5
look like this:.leading-5 { + --tw-leading: 1.25rem; line-height: 1.25rem; }
…and utilites like
text-sm
include avar(…)
lookup that they didn't include before:If this extra CSS doesn't feel worth it for the small improvement in behavior, we may consider just closing this PR and keeping things as they are.
This PR is also a breaking change for anyone who was depending on the old behavior, and expected the line-height baked into the
md:text-lg
class to take precedence over the explicitleading-6
class:Personally I am comfortable with this because of the fact that you can still get the old behavior by preferring a line-height modifier: