A new default spacing scale #12263
Replies: 4 comments 7 replies
-
A few things about the second point under the heading "Benefits." That means that a spacing system should be a geometric progression. We could use the common ratio So what we can do is use a common ratio of 2, and between every 2 numbers add 3 additional and equidistant numbers. I would do this for numbers from 8 onwards since for previous numbers you do not need as many divisions with decimal numbers. The list that remains and the one I am using is like this: spacing: {
'0': '0px', // instead of 0 (2^0)
'1': '1px', // instead of px (2^1)
'2': '0.125rem', // instead of 0.5
'4': '0.25rem', // instead of 1 (2^2)
'6': '0.375rem', // instead of 1.5
'8': '0.5rem', // instead of 2 (2^3)
'10': '0.625rem', // instead of 2.5
'12': '0.75rem', // instead of 3
'14': '0.875rem', // instead of 3.5
'16': '1rem', // instead of 4 (2^4)
'20': '1.25rem', // instead of 5
'24': '1.5rem', // instead of 6
'28': '1.75rem', // instead of 7
'32': '2rem', // instead of 8 (2^5)
'40': '2.5rem', // instead of 10
'48': '3rem', // instead of 12
'56': '3.5rem', // instead of 14
'64': '4rem', // instead of 16 (2^6)
'80': '5rem', // instead of 20
'96': '6rem', // instead of 24
'112': '7rem', // instead of 28
'128': '8rem', // instead of 32 (2^7)
'160': '10rem', // instead of 40
'192': '12rem', // instead of 48
'224': '14rem', // instead of 56
'256': '16rem', // instead of 64 (2^8)
'288': '18rem', // instead of 72
'320': '20rem', // instead of 80
'384': '24rem' // instead of 96
} which as you can see is exactly the same scale that tailwind currently has except removing the numbers Those 7 values seem to be "extra", and that is the reason why inconsistencies occur like the one I mentioned that the value after 16px is 20px (25% increase), when the next value of 240px is 256px (6.7% increase) . |
Beta Was this translation helpful? Give feedback.
-
Thanks for the thoughtful write-up! This is definitely something I'm thinking about for Tailwind CSS v4 but that's the earliest we could do it because it would be a very significant breaking change, and even then I'm not sure the benefit would be enough to warrant the damage it would do to the ecosystem 😕 Every video, blog post, example components, etc. would all be broken/out of date immediately, which is a really serious cost even if it's how I wish the spacing scale were designed in hindsight. Not totally out of the question though, will think through it more some time next year when v4 is closer 👍 |
Beta Was this translation helpful? Give feedback.
-
@adamwathan First of all, thanks for the amazing work with Tailwind v4! It is full of awesome new features. ❤️ If you'll allow me a little unsolicited feedback, there is one change that I consider a step backward: One of Tailwind's greatest strengths has always been the restriction on spacing tokens. While it is true that I can extend or overwrite the system as I wish, the problem now is that AI tools will start to be trained with arbitrary spacing values, such as mt-27.5, since that is now an accepted default. Having a default set of tokens offers several benefits:
The pattern of using brackets for custom values was already an elegant and sufficient solution. I am not sure if this decision is still subject to change, but, if possible, I would like to suggest the following: Keep a spacing system with no more tokens than v3.
I understand that it is not always possible to satisfy all requests, but I feel it is important to highlight these points. Thank you very much for taking the time to consider it, and again, congratulations on this great release! 🚀 EDIT: I found the PR where the new change was made: #14857
That's a fair point. But either way you can almost always use intellisense and ultimately I think it's still a tradeoff that doesn't outweigh the benefits of having a spacing system.
Yes, I don't always follow the standard measurements either, but using brackets once in a while is not death. +90% of the time the v3 tokens are sufficient. |
Beta Was this translation helpful? Give feedback.
-
I watched the design systems course by Figma As far as I understand, tailwind provides base tokens (all colors, all spacing values, no custom typography like headline fonts, etc) and ability to generate utility classes. Then you can build your application using those. After a while additional things may be needed. For example, if there is a font that can be used for headlines, it should be added to tailwind config. Once the app has a primary color (or multiple primary colors per section, for example), they can be added to tailwind config too. I think the same process would have to be applied to spacers from now on. Design tokens are like global constants in code: if a value means something and can be reused, it can be added to one place. Tailwind config viewer can be used to view custom values from tailwind config. GitLab pajamas has their own spacing scale. Or classes like I did a quick search. I think it could be useful to have a default tailwind config or a checklist on what to add somewhere for non-designers (like me) to be able to get started using a design system quickly, and then replace things if needed |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been thinking about the following tweet from @adamwathan about the default spacing scale:
And I think it's a great idea.
I open this thread to propose that this can be achieved (1) without failing to provide a subset of recommended measures and (2) without causing a large breaking change.
The idea would be that p-x generates padding in rem equivalent to the amount of px that x represents. That is, if you want to have a 16px padding you should make p-16 instead of p-4. If you want to use a "strange" value you should still use square brackets (e.g. p-[17px]).
In the case of continuing to use the same current scale*, the conversion would be equivalent of putting this in your
theme.spacing
:Responses to possible objections:
1. I want a set of recommended measurements, I don't want to have to decide between 12 or 13 pixels.
I think this is the main objection I found on Twitter.
As I mentioned, doing this wouldn't imply that you have infinite options for any length, like the
[ ]
brackets work. We could use a set of sizes as now without square brackets (for example: p-16), but we would require square brackets for values outside the recommended scale (for example: p-[17px]).2. This would be a breaking change
Yes, but it could be made much milder in two ways:
3. It is easier to choose a value that does not exist because the "gaps" or "distances" are greater.
I've been using tailwind for quite some time, and I still need to use intellisense to put valid values.
I think this argument would be more valid if the current scale had been completely 1 in 1 and without gaps (even after 12). It is 1 in 1 in the range 1-12, 2 in 2 in the range 12-16, and 4 in 4 in the range 16-96. And you're still going to need intellisense when typing the value, because I highly doubt people remember that 44 equals 176px, for example.
The current default spacing scale has the worst of both worlds, since it does not allow you to "guess" the next valid value without intelligense as it is not 1 in 1, nor does it allow you to know the value it represents without using intellisense as it is not a real representation, as I am proposing.
4. You can now do that, by overriding the spacing scale in the settings.
Yes, but that would mean using a different spacing system than the entire community. Any component in the entire ecosystem that I wanted to copy and paste wouldn't work.
Tailwind is increasingly becoming the de facto standard for styling. It would be a shame if such an important library didn't use the best spacing system simply because of the pain of the change.
Benefits:
Beta Was this translation helpful? Give feedback.
All reactions