Skip to content

Instantly share code, notes, and snippets.

@ErnWong
Created July 21, 2019 09:21
Show Gist options
  • Save ErnWong/847dedc5c973a6d2dac372c55fb06dd1 to your computer and use it in GitHub Desktop.
Save ErnWong/847dedc5c973a6d2dac372c55fb06dd1 to your computer and use it in GitHub Desktop.
Zigzag CSS pattern
function generateZigZag(size, thickness) {
const ratio = (thickness / size / 2) * 100;
const quadrantMain = `transparent ${25 - ratio}%, black ${25 - ratio}%, black 25%, transparent 25%`;
const quadrantTip = `black ${ratio}%, transparent ${ratio}%`;
return {
background: [
`linear-gradient(-45deg, ${quadrantTip}) ${-size}px ${size}px`,
`linear-gradient(-45deg, ${quadrantTip}) 0 ${size}px`,
`linear-gradient(-135deg, ${quadrantTip}) ${-size}px ${-size}px`,
`linear-gradient(-135deg, ${quadrantTip}) 0 ${-size}px`,
`linear-gradient(135deg, ${quadrantMain}) 0 0`,
`linear-gradient(135deg, ${quadrantMain}) ${size}px 0`,
`linear-gradient(45deg, ${quadrantMain}) 0 0`,
`linear-gradient(45deg, ${quadrantMain}) ${size}px 0`,
].join(', '),
backgroundSize: `${2 * size}px ${2 * size}px`,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment