Description
This plugin helps replace the standard Tailwind spacing tokens
rems
to pixels
(but with the final output in the
CSS remaining in rems
).
As humans often specify spacing in terms of pixels, using this set up you
can see "padding top 40px" and then use .pt-40
in your HTML.
Tailwind like spacing classes are generated, eg. mt-
,
pt-
, h-
, max-h-
etc. by the Tailwind
core plugins for padding
, margin
,
width
, height
, maxHeight
,
gap
, inset
, space
, and
translate
.
Key to understand is that .mt-4
will
not produce margin-top: 1rem;
,
but instead will produce margin-top: 0.25rem;
which is
equivalent to 4px
.
Before v3.4.0
you had to provide your own scale, the typing of
which was tedious. After v3.4.0
, this plugin outputs a scale
based on multiples of 5
, to a maximum of 100
with
arbitrary values between 0 - 10px. Or, you can describe the scale and add
any arbitrary values and the function will generate your scale for you.
Setup
Default
With no options specified SpacingTokens
will generate a spacing
scale based on multiples of 5
, to a maximum of
100
with arbitrary values of 0 - 10px.
Scale defined:
With this config, SpacingTokens
will generate a spacing scale
based on multiples of 4
, to a maximum of 200
along
with arbitrary values of 0 - 10px and 400px as specified.
Unless you specify a scale manually, SpacingTokens
will always
output arbitrary values of 0 - 10px, because we always randomly need a
mt-1
or pl-3
regardless of your spacing scale.
Manual scale
Note before v3.4.0
you have to provide your
own token scale.
This manual scale is essentially what the defined scale above builds for you (although the manual scale stops at 100px and not 200px because typing it became tedious).
The curious case of minHeight
Tailwind documentation on
customising spacing
doesn't mention minHeight
and, by default, Tailwind does not
generate minHeight
classes based on the spacing tokens. If you
want to include minHeight
, you'll need to extend it:
Demo
Using:
We would get padding
, margin
, width
,
height
, maxHeight
, gap
,
inset
, space
, and translate
classes.
Demonstrated here are the width
and height
classes:
- 0px / 0rem
- 1px / 0.0625rem
- 2px / 0.125rem
- 3px / 0.1875rem
- 4px / 0.25rem
- 5px / 0.3125rem
- 6px / 0.375rem
- 7px / 0.4375rem
- 8px / 0.5rem
- 9px / 0.5625rem
- 10px / 0.625rem
- 12px / 0.75rem
- 16px / 1rem
- 20px / 1.25rem
- 24px / 1.5rem
-
2828px / 1.75rem
-
3232px / 2rem
-
3636px / 2.25rem
-
4040px / 2.5rem
-
4444px / 2.75rem
-
4848px / 3rem
-
5252px / 3.25rem
-
5656px / 3.5rem
-
6060px / 3.75rem
-
6464px / 4rem
-
6868px / 4.25rem
-
7272px / 4.5rem
-
7676px / 4.75rem
-
8080px / 5rem
-
8484px / 5.25rem
-
8888px / 5.5rem
-
9292px / 5.75rem
-
9696px / 6rem
-
100100px / 6.25rem
-
400400px / 25rem
-
600600px / 37.5rem