創建變數
A guide to using variables to implement design tokens.
Overview
Variable | Type | Group | Value |
---|---|---|---|
full | string |
| "100%" |
fit | string |
| "fit-content" |
max | string |
| "max-content" |
min | string |
| "min-content" |
font-family-mono | string | font-family | "ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace" |
font-family-sans | string | font-family | "ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji" |
font-family-serif | string | font-family | "ui-serif,Georgia,Cambria,Times New Roman,Times,serif" |
font-weight-thin | number | font-weight | 100 |
font-weight-extralight | number | font-weight | 200 |
font-weight-light | number | font-weight | 300 |
font-weight-regular | number | font-weight | 400 |
font-weight-medium | number | font-weight | 500 |
font-weight-semibold | number | font-weight | 600 |
font-weight-bold | number | font-weight | 700 |
font-weight-extrabold | number | font-weight | 800 |
font-weight-heavy | number | font-weight | 900 |
flex-direction-col | string | flex-direction | "column" |
flex-direction-col-reverse | string | flex-direction | "column-reverse" |
box-sizing-content | string | box-sizing | "content-box" |
box-sizing-border | string | box-sizing | "border-box" |
position-abs | string | position | "absolute" |
position-rel | string | position | "relative" |
transform-box-content | string | transform-box | "content-box" |
transform-box-border | string | transform-box | "border-box" |
transform-box-padding | string | transform-box | "padding-box" |
transform-box-fill | string | transform-box | "fill-box" |
transform-box-stroke | string | transform-box | "stroke-box" |
transform-box-view | string | transform-box | "view-box" |
animation-direction-alt | string | animation-direction | "alternate" |
animation-direction-alt-reverse | string | animation-direction | "alternate-reverse" |
background-clip-content | string | background-clip | "content-box" |
background-clip-border | string | background-clip | "border-box" |
background-clip-padding | string | background-clip | "padding-box" |
background-origin-content | string | background-origin | "content-box" |
background-origin-border | string | background-origin | "border-box" |
background-origin-padding | string | background-origin | "padding-box" |
order-first | number | order | -999999 |
order-last | number | order | 999999 |
shape-outside-content | string | shape-outside | "content-box" |
shape-outside-border | string | shape-outside | "border-box" |
shape-outside-padding | string | shape-outside | "padding-box" |
shape-outside-margin | string | shape-outside | "margin-box" |
clip-path-content | string | clip-path | "content-box" |
clip-path-border | string | clip-path | "border-box" |
clip-path-padding | string | clip-path | "padding-box" |
clip-path-margin | string | clip-path | "margin-box" |
clip-path-fill | string | clip-path | "fill-box" |
clip-path-stroke | string | clip-path | "stroke-box" |
clip-path-view | string | clip-path | "view-box" |
current | string |
| "currentColor" |
screen-4xs | number | screen | 360 |
screen-3xs | number | screen | 480 |
screen-2xs | number | screen | 600 |
screen-xs | number | screen | 768 |
screen-sm | number | screen | 834 |
screen-md | number | screen | 1024 |
screen-lg | number | screen | 1280 |
screen-xl | number | screen | 1440 |
screen-2xl | number | screen | 1600 |
screen-3xl | number | screen | 1920 |
screen-4xl | number | screen | 2560 |
In Master CSS, variables are used to manage tokens in the design system, such as colors, font families, etc., even based on different theme modes.
import { variables } from '@master/css'export default { variables: { spacing: { sm: 10 }, primary: '#000000' }}
Apply custom variables using ambiguous shorthand:
<div class="font:sans max-w:screen-desktop m:sm bg:primary">…</div>
Design tokens are a fundamental part of design systems and are crucial for maintaining design consistency and efficiency in projects, especially in larger organizations or projects with complex design requirements. They help bridge the gap between design and development, promoting collaboration and ensuring a cohesive user experience.
Basic usage
Add a string variable
Create a string variable to reuse it in your markup.
export default { variables: { content: { hash: '" #"', external: '" ↗"' }, }}
Use the variable in your markup:
<div class="content:hash content:external">…</div>
Add a spacing variable
Add a spacing variable to reuse it in your markup.
export default { variables: { spacing: { md: 20 } }}
Apply the defined spacing
variables using the inherited rules margin-left
, margin-right
, margin-top
, margin-bottom
, margin-x
, margin-y
, margin
, margin-inline-start
, margin-inline-end
, margin-inline
, padding-left
, padding-right
, padding-top
, padding-bottom
, padding-x
, padding-y
, padding
, padding-inline-start
, padding-inline-end
, padding-inline
, text-underline-offset
, top
, bottom
, left
, right
, inset
, transform
, border-spacing
, stroke-dashoffset
, x
, y
, cx
, cy
, column-gap
, row-gap
, gap
, outline-offset
, scroll-margin-left
, scroll-margin-right
, scroll-margin-top
, scroll-margin-bottom
, scroll-margin-x
, scroll-margin-y
, scroll-margin
, scroll-padding-left
, scroll-padding-right
, scroll-padding-top
, scroll-padding-bottom
, scroll-padding-x
, scroll-padding-y
, scroll-padding
, shape-margin
.
<div class="mt:md p:md">…</div>
Do not use numerical values as tokens.
export default { variables: { spacing: { 1: 4, 2: 8 } }}
It can be confused with the unitless values.
Add a color variable
See more about color variables.
Spacing and Sizing
The spacing and sizing system is constructed using a base unit of 4 pixels. For visual consistency, you can use the multiplier unit x
to apply spacing with the same scale.
Token | Pixels | REM | Representation |
---|---|---|---|
1x | 4px | 0.25rem | |
2x | 8px | 0.5rem | |
3x | 12px | 0.75rem | |
4x | 16px | 1rem | |
5x | 20px | 1.25rem | |
6x | 24px | 1.5rem | |
7x | 28px | 1.75rem | |
8x | 32px | 2rem | |
9x | 36px | 2.25rem | |
10x | 40px | 2.5rem | |
11x | 44px | 2.75rem | |
12x | 48px | 3rem | |
13x | 52px | 3.25rem | |
14x | 56px | 3.5rem | |
15x | 60px | 3.75rem | |
16x | 64px | 4rem | |
17x | 68px | 4.25rem | |
18x | 72px | 4.5rem | |
19x | 76px | 4.75rem | |
20x | 80px | 5rem | |
21x | 84px | 5.25rem | |
22x | 88px | 5.5rem | |
23x | 92px | 5.75rem | |
24x | 96px | 6rem | |
25x | 100px | 6.25rem | |
26x | 104px | 6.5rem | |
27x | 108px | 6.75rem | |
28x | 112px | 7rem | |
29x | 116px | 7.25rem | |
30x | 120px | 7.5rem | |
31x | 124px | 7.75rem | |
32x | 128px | 8rem | |
... |
<div class="m:1x">4px, margin: 0.25rem</div><div class="p:2x">8px, padding: 0.5rem</div><div class="w:8x">32px, width: 2rem</div><div class="gap:3x">12px, gap: 0.75rem</div><div class="size:6x">24px, width: 1.5rem; height: 1.5rem</div>
It's no longer necessary to define the spacing scale one by one as in the traditional way.
export default { variables: { spacing: { '1x': 4, '2x': 8, ..., '100x': 400 } }}
For other intermediate values, 0
, 1
, 2
, ..., we tend to use unitless tokens:
<div class="m:0">0px, margin: 0rem</div><div class="p:1">1px, padding: 0.0625rem</div><div class="w:2">2px, width: 0.125rem</div>