Spacing
We have 10 spacing sizes. These are used for all margins and padding on any element and are defined in our Theme.
We recommend using our Layout components to control spacing of and between elements. The exported components and mixins have our allowed spacing scale values baked-in for more consistently spaced UIs.
Size scale
We can apply spacing to individual sides (top, right, bottom, left), or vertical and horizontal sides, as either margin or padding. In version 4.0.0
of our theme we updated our spacing scale to a more standard 16pt based scale.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
---|---|---|---|---|---|---|---|---|---|---|---|
Theme ≥ 4.0.0 | 0px | 4px | 8px | 12px | 16px | 24px | 32px | 48px | 64px | 96px | 128px |
Ways to apply spacing
Below are different ways provided by Mesh to apply spacing—using the above size scale—to your design. They include the padding
and margin
props using the Box component, the space
props as part of our other layout components or via the use of mixin utilities.
Layout components
Layout components are Mesh components that control spacing between elements. They abstract away the logic and provide a user-friendly and intuitive set of props to achieve accurate and flexible spacing for a number of layout designs.
For more information on Layout components, see our Layout docs.
Box component
The Box component acts as a container and accepts props for standard spacing. Combine padding and margin for sizing flexibility.
Stack and Inline components
Layout components such as Stack and Inline allow for consistent control over the vertical white space between elements. They allow you to apply a standard spacing size between direct children. Spacing can be responsive and layout components can be nested.
Mixins
In general, you should use the layout components over mixins. However, there are instances where spacing needs to be controlled in a more intricate way.
Padding mixins
import {p, pt, pr, pb, pl, px, py} from '@nib/layout';const Example1 = styled.div`${p(4)};`;const Example2 = styled(Box)`${pt(1)};${pr(6)};${pb(3)};${pl(5)};`;const Example3 = styled.div`${px(6)};${py(4)};`;
Margin mixins
import {m, mt, mr, mb, ml, mx, my} from '@nib/layout';const Example1 = styled.div`${m(4)};`;const Example2 = styled(Box)`${mt(1)};${mr(6)};${mb(3)};${ml(5)};`;const Example3 = styled.div`${mx(6)};${my(4)};`;