Import
import { Space } from '@dnb/eufemia'
Description
The Space component provides margins within the provided spacing patterns.
Relevant links
The reason this exists is to make your syntax as clean as possible. This way, you see directly in words what the spacing is for every affected component.
Spacing Table
| Pixel | Type | Rem | Custom Property |
|---|---|---|---|
| 8 | x-small | 0.5 | --spacing-x-small |
| 16 | small | 1 | --spacing-small |
| 24 | medium | 1.5 | --spacing-medium |
| 32 | large | 2 | --spacing-large |
| 48 | x-large | 3 | --spacing-x-large |
| 56 | xx-large | 3.5 | --spacing-xx-large |
NB: In some circumstances you may be in need of using 0.25rem (4px) - therefore xx-small also exists, but as a single type. So, combining xx-small and small would not result in 0.25rem, but still remain 1rem.
Value Format
There are a couple of different ways you can define the spacing types and values:
- Types:
small small x-small(combine types up to 10rem) - number:
2.5(equivalent torem) - string(rem):
2.5rem - string(px):
40px(gets converted torem) - boolean:
true(equivalent tosmall),false(equivalent tozero)
To get a spacing of e.g. 2.5rem (40px), you may combine types large and x-small.
With React, you can also use an object with the different directions:
Components and Spacing
Every component supports the spacing patterns, so it's possible to send in the top, right, bottom, left and space properties directly, like:
Spacing shorthands
A shorthand for getting 1rem (most used) is to simply send in a boolean set as true. No given value in JSX means true, so you only need the property key:
In order to set all four directions at once, you can provide a string as the space value:
Does it not work as expected?
Is margin not giving the expected spacing? That may be due to Margin Collapsing. Margins collapse in the following situations:
- Adjacent siblings
- Completely empty boxes
- Parent and first or last child element
The best solution is to only use one direction of margins, e.g. bottom. Or you can set the collapse property to false.
Margin collapsing
In order to help handle unwanted margin collapsing in typography elements, see this example.
Conditional Reset
For resetting spacing (margin: 0) only when no spacing is defined, you can make use of dnb-space__reset.
Style and Spacing
Every Eufemia component that supports spacing props uses CSS custom properties (e.g. --margin-t-s) on the style attribute to drive responsive margins. When you pass a style prop to a component, your styles and the spacing styles are merged together — spacing properties take precedence.
This means you can safely combine your own styles with spacing:
<Space style={{ color: 'var(--color-sea-green)' }} top="medium">...</Space>
If you work with raw DOM elements and set styles via setAttribute('style', ...), make sure you preserve any existing style values when adding new ones, so the spacing custom properties are not lost.
const existing = element.getAttribute('style')const merged = existing? `${existing.replace(/;?\s*$/, '')}; ${style}`: styleelement.setAttribute('style', merged)