Skip to content

Instantly share code, notes, and snippets.

@rakr
Last active January 21, 2023 00:29
Show Gist options
  • Save rakr/3a841408424be4160d59fc08df1ad2bc to your computer and use it in GitHub Desktop.
Save rakr/3a841408424be4160d59fc08df1ad2bc to your computer and use it in GitHub Desktop.
SVG Pattern Backgrounds with TailwindCSS

Install "mini-svg-data-uri"

$ pnpm install mini-svg-data-uri

Modify your tailwind.config.js like so

** @type {import('tailwindcss').Config} */

const svgToDataUri = require('mini-svg-data-uri')

module.exports = {
  darkMode: 'class',
  content: [
    ...
  ],
  ...
  plugins: [
    ...,
    function ({ matchUtilities, theme }) {
      matchUtilities(
        {
          'bg-grid': (value) => ({
            backgroundImage: `url("${svgToDataUri(
              `<svg xmlns="http://www.w3.org/2000/svg" width="4" height="4" viewBox="0 0 4 4"><path fill="${value}" d="M1 3h1v1H1V3zm2-2h1v1H3V1z"></path></svg>`
              // Replace the above with your svg
            )}")`,
          }),
        },
        { values: flattenColorPalette(theme('backgroundColor')), type: 'color' }
      )

      matchUtilities(
      ...
    },
    ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment