One of the recurring annoyances when designing websites is ensuring alignment of elements on a web page.
Most design applications have the provision of an overlay grid. With very little effort we can add a simple grid to any web page and use it to ensure alignment of elements.
As illustrated in this codepen we can create a grid using a few lines of SVG:
1 2 3 4 5 6 7 8 9 10 11 12 |
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg"> <defs> <pattern id="smallGrid" width="10" height="10" patternUnits="userSpaceOnUse"> <path d="M 10 0 L 0 0 0 10" fill="none" stroke="gray" stroke-width="0.5"/> </pattern> <pattern id="grid" width="100" height="100" patternUnits="userSpaceOnUse"> <rect width="100" height="100" fill="url(#smallGrid)"/> <path d="M 100 0 L 0 0 0 100" fill="none" stroke="gray" stroke-width="1"/> </pattern> </defs> <rect width="100%" height="100%" fill="url(#grid)" /> </svg> |
We can use an extension like stylish and save this snippet to apply the above css grid as a background image to html.
1 2 3 4 5 6 |
html.grid-backdrop-enabled { background: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiB4bWxuc…IxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JpZCkiPjwvcmVjdD4KICA8L3N2Zz4="); } html.grid-backdrop-enabled body { opacity: 0.8; } |
So now whenever we activate this style and add grid-backdrop-enabled to html, the grid will show up as a backdrop against the transparent body and we can use it to check for alignment issues.
We can always adjust the SVG to tweak the appearance of the grid. Here is a slightly adapted one which highlights 5px, 10px, 50px and 100px boundaries with different colors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg"> <defs> <pattern id="smallGrid" width="5" height="5" patternUnits="userSpaceOnUse"> <path d="M 10 0 L 0 0 0 10" fill="none" stroke="gray" stroke-width="1"/> </pattern> <pattern id="medSmallGrid" width="10" height="10" patternUnits="userSpaceOnUse"> <rect width="100" height="100" fill="url(#smallGrid)"/> <path d="M 10 0 L 0 0 0 10" fill="none" stroke="black" stroke-width="1" /> </pattern> <pattern id="medLargeGrid" width="50" height="50" patternUnits="userSpaceOnUse"> <rect width="100" height="100" fill="url(#medSmallGrid)"/> <path d="M 10 0 L 0 0 0 10" fill="none" stroke="violet" stroke-width="1" /> </pattern> <pattern id="largeGrid" width="100" height="100" patternUnits="userSpaceOnUse"> <rect width="100" height="100" fill="url(#medLargeGrid)"/> <path d="M 100 0 L 0 0 0 100" fill="none" stroke="red" stroke-width="1"/> </pattern> </defs> <rect width="100%" height="100%" fill="url(#largeGrid)" /> </svg> |