Skip to content

Instantly share code, notes, and snippets.

@gerhynes
Last active September 16, 2020 20:24
Show Gist options
  • Save gerhynes/0293af9a32b3ccade4107139171c0ba4 to your computer and use it in GitHub Desktop.
Save gerhynes/0293af9a32b3ccade4107139171c0ba4 to your computer and use it in GitHub Desktop.
Tailwind CSS background images

Since Tailwind 1.7, you don't need to do background images with custom CSS

tailwind.css

.header {
  background: hsl(180, 29%, 50%) center/cover;
  background-image: url(/images/bg-header-mobile.svg);
}

@media (min-width: 640px) {
  .header {
    background-image: url(/images/bg-header-desktop.svg);
  }
}

Now you can do this, and use bg-{key}, such as bg-header-mobile or bg-header-desktop.

tailwind.js

module.exports = {
    theme: {
      extend: {
        backgroundImage: theme => ({
        "header-desktop": "url('/images/bg-header-desktop.svg')",
        "header-mobile": "url('/images/bg-header-mobile.svg')"
        })
      }
    }
  }

App.js

<header className="w-full h-40 bg-desaturated-dark-cyan bg-header-mobile sm:bg-header-desktop bg-center bg-cover"></header>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment