Skip to content

Grid Pattern

A background grid pattern made with SVGs, fully customizable using Tailwind CSS.

Installation

Copy and paste the following code into your project:

vue
<script setup lang="ts">
import { useId } from "vue";
import { cn } from "@/lib/utils";

interface GridPatternProps {
  width?: number;
  height?: number;
  x?: number;
  y?: number;
  squares?: Array<[x: number, y: number]>;
  strokeDasharray?: string;
  class?: string;
  [key: string]: any;
}

const props = withDefaults(defineProps<GridPatternProps>(), {
  width: 40,
  height: 40,
  x: -1,
  y: -1,
  strokeDasharray: "0",
});

const id = `grid-pattern-${useId()}`;
</script>

<template>
  <svg
    aria-hidden="true"
    :class="
      cn(
        'pointer-events-none absolute inset-0 h-full w-full fill-gray-400/30 stroke-gray-400/30',
        props.class,
      )
    "
  >
    <defs>
      <pattern
        :id="id"
        :width="props.width"
        :height="props.height"
        patternUnits="userSpaceOnUse"
        :x="props.x"
        :y="props.y"
      >
        <path
          :d="`M.5 ${props.height}V.5H${props.width}`"
          fill="none"
          :stroke-dasharray="props.strokeDasharray"
        />
      </pattern>
    </defs>
    <rect width="100%" height="100%" stroke-width="0" :fill="`url(#${id})`" />
    <svg v-if="props.squares" :x="props.x" :y="props.y" class="overflow-visible">
      <rect
        v-for="[squareX, squareY] in props.squares"
        :key="`${squareX}-${squareY}`"
        stroke-width="0"
        :width="props.width - 1"
        :height="props.height - 1"
        :x="squareX * props.width + 1"
        :y="squareY * props.height + 1"
      />
    </svg>
  </svg>
</template>

Examples

Linear Gradient

Dashed Stroke

Props

PropTypeDefaultDescription
widthnumber40Width of the pattern
heightnumber40Height of the pattern
xnumber-1X offset of the pattern
ynumber-1Y offset of the pattern
squaresArray<[x: number, y: number]>-X Y coordinates of filled squares as 2D array
strokeDasharraystring0Stroke dash array for the pattern
classstring-Additional classes for the pattern

Released under the MIT License.