Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a default line height #14503

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

MartijnCuppens
Copy link
Contributor

This is a slimmer version of #14335, which only includes the default line heights & the <small> fix.

In this PR:

  • Fixed line heights are replaced by a default line height css function.
  • This removes the need of adding fixed line heights to each font size.
  • It's still possible to use fixed line heights the way it worked before.
  • Custom font sizes will now have a better line height by default. For example, text-[2.4rem] will have a calculated line height of 2.625rem, instead of 3.6rem.
  • The default values are calculated in CSS, which means you can use arbitrary values in combination with custom properties (eg text-[length:var(--title-font-size)]).
  • Relative font sizes like text-[1.5em] or text-[larger] will now use a recalculated line height.
  • Only breaking change is the line height of lg (also see Change line height of the lg font size to 1.625rem #14223).
  • The line heights are always rounded to even pixels (2px), so that you never have vertical alignment issues. Even if you use viewport relative units.

Here's have an overview of which line height will be used for each font size range:

Font size range Line height
0rem - .5rem * 1em
.5rem - 1rem 2em - .5rem
1rem - 2rem 1em + .5rem
2rem - 3rem 2em / 3 + 1rem
3rem 1em

* These font sizes are not used in Tailwind by default

Closes #14484, #14223

@adamwathan
Copy link
Member

Hey revisiting this again! The biggest blockers for me still are two things:

  1. I have no idea what happens when I change the numbers in the formula (round(max(min(2em - .5rem, 1em + .5rem, 2em / 3 + 1rem), 1em), 2px) — do you have any idea how we can get these to feel like labelled knobs that actually have an intuitive effect? It feels very opaque right now.
  2. I'm worried about any potential performance impact of making the browser do all this math on every text element.

What are your thoughts?

@MartijnCuppens
Copy link
Contributor Author

MartijnCuppens commented Nov 7, 2024

Complexity

Yeah, I realise this is a magic function. It’s not designed for easy adjustments but rather to serve as a smarter default than, say, 1.5em. If specific line-height tweaks are needed for individual font sizes, --text-*--line-height will still be the way to go.

There is an alternative function that’s much simpler and easier to understand: min(2em - .5rem, 1em + .5rem). This approach yields even line heights without needing the round() trick.

The only downside is that it does change more line heights for larger font sizes (although you can still set the --text-*--line-height if you notice this causes issues when upgrading from v3 to v4):

  Current line height (rem) min(2em - .5rem, 1em + .5rem) (rem)
text-xs 1 1
text-sm 1.25 1.25
text-base 1.5 1.5
text-lg 1.75 1.625
text-xl 1.75 1.75
text-2xl 2 2
text-3xl 2.25 2.375
text-4xl 2.5 2.75
text-5xl 3 3.5
text-6xl 3.75 4.25
text-7xl 4.5 5
text-8xl 6 6.5
text-9xl 8 8.5

Performance

To be honest, I'm not sure about the performance impact or even the best way to benchmark it. My intuition is that the function simply calculates a number, which likely isn't any more complex than the approach we're using with color-mix() to adjust background opacity.


What do you think about the simplified line height function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Line-height issue for <small>
2 participants