SUPERLAB - Spacing and clamps
Spacings reference
clamp(min, fluid, max)
How do clamps work?
An example clamp: clamp(5px, calc(5px + .1vw), 10px)
.
We set a minimum, a fluid value, and a maximum value, and let the spacings grow and shrink, in this case as a proportion of the viewport. Going left to right across these values, let's calculate the end value of this rule when the viewport is 1280px (container).
5px
means we never go smaller than 5pxcalc(5px + .1vw)
means that as the viewport gets larger, the size increases by .01 of the viewport. At 1280px, this value is 5px + 12.8px => 17.8px.10px
But we have clamped it so it will never go larger than 10px, so the end value is 10px at 1280px (or any viewport width after 500px).
It can be tricky to get a sense of how spacings behave on different viewports so this handy visualiser explains it all. Resize the viewport to see how the sizes change. Hover over a block to see the clamp rule controlling it, or choose it in Inspect.
Check this example against Inspect device sizes to see how we've chosen 5vw as our fluid width, and how the clamps apply. It will stop shrinking at less than 600px viewport and stop growing at greater than 1920px viewport. Unlike some older sizing approaches, these spaces can carry on getting bigger after the device width has exceeded the container width, unless you clamp them.
clamp(30px,5vw,96px)
In this example, notice the difference between a straight viewport value 2.5vw
and this calc calc(20px + 2.5vw)
. The left hand value acts as a floor. The width will never go below 20px, even if you set a lower number on the min. If you put this calc on the min, the minimum value will be 20px.
clamp(calc(5px + 1.5vw),2.5vw,168px)
clamp(10px,2.5vw,168px)
clamp(10px,calc(20px + 2.5vw),168px)
clamp(20px,calc(20px + 2.5vw),168px)
Here's a spacing scale, like our greys scale or our type hierarchy. We should work on this a bit.
clamp(50px,calc(40px + 4vw),110px)
clamp(40px,calc(35px + 3vw),100px)
clamp(35px,calc(30px + 2vw),80px)
clamp(30px,calc(25px + 1.5vw),60px)
clamp(25px,calc(20px + 1vw),50px)
clamp(20px,calc(15px + .75vw),40px)
clamp(15px,calc(10px + .5vw),30px)
clamp(10px,calc(7.5px + .25vw),20px)
clamp(5px,calc(4px + .1vw),10px)
Viewport to Pixel Breakpoints Rough Reckoning Guide
vw | 320px device | 600px device | 960px device | 1280px device | 1920px device | 2800px device |
---|---|---|---|---|---|---|
1 | 3.2px | 6px | 9.6px | 12.8px | 19.2px | 28px |
2 | 6.4px | 12px | 19.2px | 25.6px | 38.4px | 56px |
3 | 9.6px | 18px | 28.8px | 38.4px | 57.6px | 84px |
4 | 12.8px | 24px | 38.4px | 51.2px | 76.8px | 112px |
5 | 16px | 30px | 48px | 64px | 96px | 140px |
6 | 19.2px | 36px | 57.6px | 76.8px | 115.2px | 168px |
Pixel to Viewport Reckoning
px | 320px device | 600px device | 960px device | 1280px device | 1920px device | 2800px device |
---|---|---|---|---|---|---|
5 | 1.5vw | 0.8vw | 0.5vw | 0.3vw | 0.2vw | 0.1vw |
10 | 3.1vw | 1.6vw | 1vw | 0.7vw | 0.5vw | 0.3vw |
15 | 4.6vw | 2.5vw | 1.5vw | 1.1vw | 0.7vw | 0.5vw |
20 | 6.2vw | 3.3vw | 2vw | 1.5vw | 1vw | 0.7vw |
25 | 7.8vw | 4.1vw | 2.6vw | 1.9vw | 1.3vw | 0.8vw |
30 | 9.3vw | 5vw | 3.1vw | 2.3vw | 1.5vw | 1vw |