We start with a basic pattern that we correctly place and size to get the first result as illustrated in (1). It’s the first layer that we can create using one gradient. Then we place a second layer (illustrated in (2)) to fill the inner part and leave the border part visible.
We are done! Two layers each one made with one gradient and we get the first Zig-Zag box. When talking about gradients, it means background and the code will be as follows:
.zig-zag-box {
--s: 60px;
width: 360px;
aspect-ratio: 1;
background:
/* (2) */
conic-gradient(#774F38 0 0)
50%/calc(100% - var(--s)) calc(100% - var(--s)) no-repeat,
/* (1) */
repeating-conic-gradient(from 45deg,#774F38 0 25%,#0000 0 50%)
calc(var(--s)/2) 0/var(--s) var(--s);
}
The variable --s will control the size of the pattern (layer 1) and will also be used to size layer (2) as well. As for the positions, the top layer needs to be placed at the center as illustrated in the figure (using 50%) and the pattern needs to be shifted to the left to be correctly placed (using calc(var(--s)/2) 0).
Here is a more friendly syntax to better understand each value
.zig-zag-box {
--s: 60px;
width: 360px;
aspect-ratio: 1;
background-image:
conic-gradient(#774F38 0 0),
repeating-conic-gradient(from 45deg,#774F38 0 25%,#0000 0 50%);
background-position:
center,
calc(var(--s)/2) 0;
background-size:
calc(100% - var(--s)) calc(100% - var(--s)),
var(--s) var(--s);
background-repeat:
no-repeat,
repeat;
}
It should also be noted that the width needs to be a multiplier of the variable --s so to make it easy to adjust we can express it like below:
.zig-zag-box {
--n: 6;
width: calc(var(--n)*var(--s));
}
The variable --n is an integer you adjust to control the size.
You can also use the round() function and the browser will make sure the width is rounded to be a multiplier of --s.
.zig-zag-box {
--s: 60px;
--w: 350px;
width: round(var(--w),var(--s));
}
Another update we can make is changing background with mask. The use of mask allows us to have any background coloration and apply the shape to images for example.
.zig-zag-box {
--s: 60px;
--w: 350px;
width: round(var(--w),var(--s));
aspect-ratio: 1;
background: linear-gradient(#8C2318,#774F38);
mask:
conic-gradient(#000 0 0)
50%/calc(100% - var(--s)) calc(100% - var(--s)) no-repeat,
repeating-conic-gradient(from 45deg,#000 0 25%,#0000 0 50%)
calc(var(--s)/2) 0/var(--s) var(--s);
}
The mask property uses the same gradient configuration and now the background is controlling the coloration.
Here is a demo with the shape applied to different elements: