Skip to content

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.

012345678910
Theme ≥ 4.0.00px4px8px12px16px24px32px48px64px96px128px

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.

jsx

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.

jsx

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

jsx
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

jsx
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)};
`;